Git: Diff staged changes
Saying git diff
only shows unstaged changes relative to the index (or HEAD
if the index is empty, alternatively any hash or branch you supplied) but leaves out files you already staged for the next commit.
To diff your added changes with HEAD
, say:
git diff --cached
Effectively, this gives you the changes you will commit when you run git commit
without the -a
switch.
Related cards:
Git: See more context in a diff
Using the -U
parameter you can change how many lines are shown above and below a changed section.
E.g. git diff -U10
will show 10 lines above and below.
Git: Diff changes in a long line efficiently
Instead of showing you two lines for each change, Git allows you to highlight changes in a line explicitly:
git diff --word-diff some_file
Hello [-world-]{+universe+}!
(The result is usually colored nicely, the removed part being red...
Git: How to add changes matching a regular expression
When you have many changes, and you want to spread them across different commits, here is a way to stage all changes matching a given regular expression for a single commit.
Example
Consider the following git diff
output.
diff --gi...
Git: When committing, check the diff
When committing, you should always check the diff of your changes so you don't include any leftovers or irrelevant/bad changes. Any time spent on that is well invested as it saves you rejected merge requests or any mess you need to clean up later....
Git commands to discard local changes
Use case
You have uncommited changes (you can always check by using git status
), which you want to discard.
Context
Now there are several options to discard these depending on your exact situation.
The headlines will differentiate the c...
Git: Splitting up changes into several commits
Splitting up commits makes the process of reviewing often easier, since you can create several merge requests or review every commit one by one.
So when you find out that you have portions of the code that you initially didn't intend to change o...
Git: Parsing large diffs as a human
I just finished migrating a project from the Asset Pipeline to Webpacker, this is what my diff to master looks like:
5.825 files changed, 44.805 insertio...
Git: Change author of a commit
Using git rebase
can be painful but luckily you can resort to cheating with git reset
and committing anew.
Now what if you wanted to rebase commits of ot...
Git diff: Deemphasizing code that was only moved around
In long diffs, it can become impossible to spot small changes in larger blocks of moved code. This may be either a method that was moved from the top to the bottom of a file, or a long test file that was split in many.
Fortunately, Git offers a s...
How to "git diff" with a graphical diff tool
If you are fine with the default console diff most of the time but only sometimes want to use an external tool for viewing a diff, you can use git difftool
.
E.g. viewing git diff
with meld:
git difftool --tool=meld
For each file in ...