Git Solutions Collection

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

1
2
git fetch origin
git reset --hard origin/master

Remove all unreachable objects

Warning

This command would remove all stashed objects.

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

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 the following code is executed accidentally

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>