git

git basic commands and tips

Branch

Create a new branch “feature_x” and switch to that branch:

1
git checkout -b feature_x

Switch back to master:

1
git checkout master

Push to remote server:

1
git push -u origin feature_branch_name

Merge

1
2
git checkout <branch_merge_to>
git merge <branch_merge_from>

Diff branches

1
git diff <source_branch> <target_branch>

Delete a branch both locally and remotely

1
2
git branch -D <branch_name>
git push origin --delete <branch_name>

Rollback a merge

1
2
git log # find the commit/merge ID
git reset --hard ID

Compare file in 2 branches

1
git diff mybranch..master -- myfile.cs

Tag

Create a new tag

1
git tag -a v1.4 -m "my version 1.4"

List tags

1
git tag -l

Update git to latest version

1
2
3
4
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:git-core/ppa
sudo apt-get update
sudo apt-get install git

Reset all changes after last commit to git

First reset the changes

1
git reset HEAD --hard

then clean out everything untracked

1
git clean -fd

If .gitignore is not working

If you didn’t create .gitignore before running git init, then you have to remove folders from the cache, for example,

.gitignore content

1
2
/target
/.idea

Just run:

1
2
3
4
5
git rm -r target --cached
git rm -r .idea --cached
git add .
git commit -m "XXX"
git push

Then, any further changes to target / .idea will be ignored

Statistics

List authors ordered by the number of commits

1
git shortlog -s -n

List files being changed between SHAs

1
git diff --name-only SHA1 SHA2

List files being changed between the tenth latest commit and the fifth latest.

1
git diff --name-only HEAD~10 HEAD~5

List files being changed between days

1
git diff --name-only "@{3 days ago}" "@{0 days ago}"

Tree view of git commits

1
git log --graph --oneline

Share