Good Git!

Most used (but yet unremembered) git commands

TLDR;

1
2
3
gl #git log --oneline --decorate --graph --all
gll #git log --graph --pretty='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --all
tig #A commandline git history viewer

Push your currently detached HEAD

git push origin HEAD:master

Delete remote branches only

git push origin --delete branch_to_delete

Changes in git index vs. local filesystem

1
2
3
4
5
git commit -m "Something terribly misguided"
git reset --soft HEAD~
#<< edit files as necessary >>
git add ...
git commit -c ORIG_HEAD
  • git reset --soft HEAD~1 doesn’t remove the changes from index (i.e. git commit adds them again, no need to git add.).
  • git reset HEAD~1 undoes the commit, removes changes from index but keeps the changes locally (working tree)
  • git reset --hard HEAD~1 removes the changes from the filesystem (working tree) too.

Remove git tracked files without removing them from filesystem

git rm --cached --force ".idea"

Specific Usecases

1
2
3
4
5
6
#To see diff for a particular commit https://stackoverflow.com/a/17563740/1233476
git diff COMMIT^ COMMIT

#To remove untracked files from working tree
git clean -n; #To check
git clean -f -d; #To delete

Submodules

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#to checkout already existing submodules in an existing project
git submodule update --init --recursive

#To clone a project along with its submodules
git clone --recursive <project url>

#add a submodule in a repo
`git submodule add https://github.com/<user>/rock rock`

#remove a submodule
git submodule deinit rock

#list existing submodules
git submodule status

Tags

1
2
3
4
5
6
7
8
9
npm version patch/minor/major #Changes packages.json and applies git tag
git push origin v1.5 (or) git push origin --tags
git tag v1.5 (lightweight tag)
git tag -a -f -m "my version 1.4" v1.6 (annotated tag)
git push --tags (or) git push upstream v2.3.3
git tag -l "v1.8.5*" (list tags)
git show v1.4
git push --delete origin tagname #For remote
git tag --delete tagname #To remove local tag

More awesome references:

Author

Sahil Ahuja

Posted on

2016-11-22

Updated on

2020-12-21

Licensed under

Comments