6 most useful git commands

· 1 min read
6 most useful git commands

If you prefer to use commands through git, here are the 6 most useful git commands you can use in the project.

Check unmerged branches

If we have multiple branches and want to find out which branches haven't merged master yet, we can use:

git branch --no-merged master

Use vim as the default commit message editor

We only need to run this once, if you have any other favorite editor, make sure you have the the program name linked backed to /usr/local/bin.

git config --global core.editor "vim"

Discard unstaged changes and go back to last commit

Warning: this is an irrevocable operation

git reset --hard HEAD^1

Checkout last used branch

"-" variable means last used branch name

git checkout -

Find all the branch names which has a certain commit

git branch --contains adf8038

Find all the branch names which has a certain tag

git branch --contains v1.1.3

Check the change history of a certain file

Which will list the author name, change date time, commit hash and etc

git blame AppDelegate.swift


7af68a64 (Ben LIU 2019-11-08 17:02:13 +1000 35) Change A
35cc7b19 (Ben LIU 2020-06-23 06:53:28 +1000 36) Change B
7af68a64 (Ben LIU 2019-11-08 17:02:13 +1000 37) Change C

Change the author name

git commit --amend --author='Author Name <[email protected]>'

Delete all the stash

git stash clear

Export a branch to a file

git bundle create <file> <branch-name>

Import bundle as a new branch

Will import and create a new branch from the repo.bundle created above.

git clone repo.bundle <repo-dir> -b <branch-name>