Skip to content

Git

Common Commands

git log-short 10
git config --global alias.tree 'log --graph --abbrev-commit --oneline' 
git log --all --decorate --oneline --graph
git log branch1 branch2
git show (hashid)
git merge (branch.to.merge.from)
git diff branch1 branch2 --name-only
git webdiff branch1 branch2
git config --global alias.unstage 'reset HEAD --'
git unstage filename
git config --global alias.difflast 'diff --cached HEAD^'
git log -G"textToFind" --online
git log -n
git diff HEAD~2 HEAD 
cat .git/HEAD   # to find name of current branch
git checkout -t origin/branch_name
git checkout -t remote/branch_name  # checkout remote branch as local, track

Squash

Compress multiple commits into one commit

Before
    (master)v1-->v2-->v3-->v4

After
    (master)v1-->v4

All changes in v2, v3 and v4 from Before are compressed into v4 in After.

To do this Squash operation:

1) git rebase -i (v1)
2) pick v2, squash (s) v3 and v4, save
3) comment out the comment to remove, save

Git merge

Merge copies all feature branch changes to another branch.


Before
    (master)v1-->v2 
                  \ 
                   \(br)-->b1-->b2

Git merge moves changes b1 and b2 from branch br to the master branch.

After
    (master)v1-->v2-->b1-->b2 
                  \
                   \(br)-->b1-->b2

git tree shows

(base) quantboy@ThinkPad:~/dev/test$ git tree
*   49bf8d4 (HEAD -> master) Merge branch 'br'
|\  
| * 7c5741d (br) b2
| * 3335395 b1
* | 8545eca v3
|/  
* 11378de v2
* e97f722 v1


==========================
Steps to reproduce
==========================

mkdir test
cd test
git init
echo v1 >> file.txt
git add .
git commit -m "v1"
echo v2 >> file.txt
git add .
git commit -m "v2"
git checkout -b br
echo b1 >> fileb.txt
git add .
git commit -m "b1"
echo b2 >> fileb.txt
git add .
git commit -m "b2"
git checkout master
echo v3 >> file.txt
git add .
git commit -m "v3"
git merge br

Git rebase

Before

    (master)v1-->v2-->v3 
                  \ 
                   \(br)-->b1-->b2

git tree shows

* 75f7798 (HEAD -> br) b2
* 53cbe04 b1
* 11a44e7 v2
* 067f3ca v1

In branch br, 

git rebase master

After

    (master)v1-->v2-->v3 
                       \ 
                        \(br)-->b1-->b2

git tree shows

* af3ee5e (HEAD -> br) b2
* 6112718 b1
* 89dc959 (master) v3
* 11a44e7 v2
* 067f3ca v1

git checkout master
git merge br

git tree shows

* 75f7798 (HEAD -> br) b2
* 53cbe04 b1
* 89dc959 v3
* 11a44e7 v2
* 067f3ca v1



==========================
Steps to reproduce
==========================

mkdir test
cd test
git init
echo v1 >> file.txt
git add .
git commit -m "v1"
echo v2 >> file.txt
git add .
git commit -m "v2"
git checkout -b br
echo b1 >> fileb.txt
git add .
git commit -m "b1"
echo b2 >> fileb.txt
git add .
git commit -m "b2"
git checkout master
echo v3 >> file.txt
git add .
git commit -m "v3"
git checkout br
git rebase master
git checkout master
git merge br

------------------------------------------------

Before

    (master)v1-->v2-->v3 
                       \ 
                        \(br)-->b1-->b2

After

    (master)v1-->v2-->v3-->b1-->b2 
                       \ 
                        \(br)-->b1-->b2

In master branch, 

git rebase br

git tree shows

* af3ee5e (HEAD -> master, br) b2
* 6112718 b1
* 89dc959 v3
* 11a44e7 v2
* 067f3ca v1

The upside is that there is less branches to show in the tree log.
The downside is that this rewrites the history- so be super careful 
with remote repositories shared with teammates.