Git reflog and git force push

Git reflog saves you from all screw-ups you do while using git.

Here is a sample output of git reflog.

hari@hari-desktop:/var/www/recruit$ git reflog
e458f54 HEAD@{0}: checkout: moving from test1 to e458f54fe99202c82c2690be4fa2fe2d7aa7be32^0
a3281d1 HEAD@{1}: checkout: moving from master to test1
f265802 HEAD@{2}: checkout: moving from test1 to master
ef08492 HEAD@{3}: ORIG_HEAD: updating HEAD
e458f54 HEAD@{4}: commit: Implement phpunit testcase.
1ecf2c3 HEAD@{5}: commit: Bug#1 : MCQ Choices displayed in questions.
f265802 HEAD@{6}: checkout: moving from test1 to f265802d1d65a9ac92bf9f6414d8f468444cce66^0
ec280c6 HEAD@{7}: checkout: moving from release to test1
f265802 HEAD@{8}: merge master: Fast-forward
ccec6c4 HEAD@{9}: checkout: moving from master to release

All your commits, checkouts, pull, merges are logged in here. To, reset the git to any point in there, we use the following command.

git reset --hard <commit-id>

But if you push this to the git, you will find that the commit is rejected now. To forcefully push your new HEAD to server, we use the following command.

git push origin +<branch_name>

+ before the branch name forcefully pushes the branch (even if your current branch is behind the origin).
NOTE: Please be careful when you use the command above. You might lose all the changes made by others (temporarily) if you use it. Of course, using reflog + reset, you can get back the changes.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.