Git: useful commands

Comparing the current branch to the master – git diff command

You can use git diff command with various flags to see either detailed difference between the branches or just a summary.

For example, after modifying file2.txt and adding new file3.txt we commit this change on wip2 branch. Then we view the diff summary:

/repo1 (wip2)
$ git diff master --name-status
    M       file2.txt
    A       file3.txt
/repo1 (wip2)
$ git diff master --stat
 file2.txt | 2 --
 file3.txt | 4 ++++
 2 files changed, 4 insertions(+), 2 deletions(-)

ANOther useful command: rebase

When you want to integrate commits from one branch onto another, you can rebase – either by issuing

$git rebase master featureBranch

or you can break this into two steps:

$git checkout featureBranch
$git rebase master

Critical to understand: once you have pushed your feature branch commits to the remote, you should not rebase them later on.

But if it’s really necessary, it can be done on one condition only: if you 100% sure nobody pulled from the remote yet.

Changing git history by squashing remote commits:

Step 1. Squash commits locally with

 git rebase -i origin/master~4 master

Step 2. and then force push only one branch (To force a push to only one branch, use a + in front of the refspec to push (e.g git push origin +master to force a push to the master branch))

 git push origin +master

Or, for easier understanding: Steps 1,2,3

    git checkout master
    git rebase origin/master~4
    git push origin +master

Or if you are on a feature branch
Step 1. Checkout our feature branch

    git checkout feature

Step 2a. Reapply it onto origin’s feature

    git rebase -i origin/feature~5

Step 2b. Fix conflicts, then git rebase --continue, repeat until done

Step 3. Push to our origin

    git push origin +feature

Walkthrough: changing history by squashing remote commits

/repo3 (master)
$ git log --oneline --graph --all
* 348fd43 (HEAD -> master, remote1/master, wip2) add file3, edit file2
* e82cd25 single commit from a squashed merge
* 919c27f edited notes at 10:41
* 7fe6713 edited notes at 10:41
* ebfca7d initial
$ git rebase -i remote1/master~4 master
    pick 7fe6713 edited notes at 10:41
    pick 919c27f edited notes at 10:41
    pick e82cd25 single commit from a squashed merge
    pick 348fd43 add file3, edit file2

    # Rebase ebfca7d..348fd43 onto ebfca7d (4 commands)

We will change editor’s content to indicate that we want to fixup (like “squash”, but discard this commit’s log message), save and close the editor.

    pick 7fe6713 edited notes at 10:41
    f 919c27f edited notes at 10:41
    f e82cd25 single commit from a squashed merge
    f 348fd43 add file3, edit file2

    # Rebase ebfca7d..348fd43 onto ebfca7d (4 commands)

Git confirms the rebase:

Successfully rebased and updated refs/heads/master.
/repo3 (master)
$ git log --oneline --graph --all
* 2498c22 (HEAD -> master) edited notes at 10:41
| * 348fd43 (remote1/master, wip2) add file3, edit file2
| * e82cd25 single commit from a squashed merge
| * 919c27f edited notes at 10:41
| * 7fe6713 edited notes at 10:41
|/
* ebfca7d initial

Now we can push the forced update to the remote

/repo3 (master)
$ git push remote1 +master
    Counting objects: 4, done.
    Delta compression using up to 6 threads.
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (4/4), 403 bytes | 403.00 KiB/s, done.
    Total 4 (delta 0), reused 0 (delta 0)
    To D:/temp/t2
     + 348fd43...2498c22 master -> master (forced update)

and the remote history got much shorter now:

/repo3 (master)
$ git log --oneline --graph
* 2498c22 (HEAD -> master, remote1/master) edited notes at 10:41
* ebfca7d initial