Git Solutions Collection

Reference commands for common Git situations.

Amend a commit

1
git commit --amend

Git pull till a particular commit

1
2
git fetch remote <branch_name>
git merge <commit_hash>

Your branch and ‘origin/master’ have diverged

Use origin/main if your default branch is main.

1
2
git fetch origin
git reset --hard origin/master # or origin/main

Remove all unreachable objects

Warning

This command will remove all stashed objects.

1
2
git reflog expire --expire-unreachable=now --all
git gc --prune=now

Commit case sensitive only filename changes

1
2
3
git config --global core.ignorecase false
git mv filename.txt Filename.txt
git commit -m "Case sensitive file rename"

Remove tracked files in .gitignore

Remove a file

1
git rm --cached <file>

Remove a folder recursively

1
git rm -r --cached <folder>

Recover a dropped stash entry

If you run the following by mistake

1
2
git stash pop
git checkout -- .

First, use gitk to find the corresponding hash value

1
gitk --all $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )

Then, run git stash to apply the stash entry

1
git stash apply <stash_hash>