cvwiki

Useful Git Commands

Nov 5, 2022

# Stash Commands

1
git stash save "stash message"

1
git stash save -u "stash message"

1
git stash list

1
git stash show -p stash@{0}

1
git stash apply stash@{0}

1
git diff stash@{0}^1 stash@{0} -- <filename>

1
git stash save -p -u

# Cleaning / Deletion

1
git rm --cached mylogfile.log
1
git rm --cached mylogfile.log
1
git reset HEAD~1 --soft   

1
git clean -n -d

1
git clean -f

1
git reset --hard

1
git branch -d <local-branch>

# I committed the deletion and then I did more commits

If you deleted a file, committed, then continued work and did more commits, only to find that deleting the file was a mistake, Git still has you covered! To find the right commit, first check the history for the deleted file:

1
$ git log -- <filename>

You can either work with the last commit that still had the file, or the commit that deleted the file. In the first case, just checkout the file from that commit:

1
$ git checkout <commit hash> -- <filename>

In the second case, checkout the file from one commit before that:

1
$ git checkout <deletion commit hash>~1 -- <filename>

# Merging

1
2
3
4
5
git checkout main
git pull
git checkout feature
git merge main
git push

# Rebasing

1
git rebase -i HEAD~N

# Misc. Commands

1
git checkout $COMMIT_SHA -- file1/to/restore file2/to/restore
1
git branch -a

1
git branch -a -v

1
git diff --cached

or

1
git diff HEAD

1
git shortlog -sne 

1
- git shortlog -s -n --since "JAN 1 2022"

# Resources