Push Errors shouldn't Git you down

Push Errors shouldn't Git you down

Using git reflog to get previous commits

As a beginner, working with git can be the bane of your existence, have you ever found yourself in a situation where you tried to push your changes to a certain repo, but you eventually...mysteriously found all your changes gone?...No? Well I have, I don't know what I did, but I lost a day's worth of work.

Thankfully I already committed my changes, and that's what helped me get it back. I'm going to show you how I was helped to get my work back, using a not so popular git command known as git reflog

Git keeps track of updates to the tip of branches using a mechanism called reference logs, or "reflogs". Reference logs records when the tips of branches and other references were updated in the local repository. It allows returning to commits, even to the ones that are not referenced by any branch or any tag. After rewriting history, the reflog includes information about the previous state of branches and makes it possible to go back to that state if needed.

For me, I used this command to go back to a certain reference in my local repo. But git reflog can help you with a whole lot more. With that being said, I'm going to show you the basic usage of the reflog command.

the first thing to do is type git reflog in your terminal, it shows you your previous commits, with their hash codes

Users-MacBook-Pro-3:portfolio user$ git reflog
1553da9 (HEAD -> master) HEAD@{0}: commit: works
2746a90 HEAD@{1}: commit: About
d6c7607 HEAD@{2}: commit (initial): portfolio start
Users-MacBook-Pro-3:portfolio user$

The hash codes are those set of numbers and alphabet that starts each reflog, for the reflog of the commit called portfolio start, its hash code is d6c7607. That's what we use to go to that particular commit, and this brings us to the next step.

git reset --hard d6c7607

This takes the changes in your files and folder back to the state it was when you made the commit with the commit message portfolio start. Then you push to your branch origin. When you push your might encounter this error failed to push some refs, all you have to do is

 git push origin <branch> --force

P.S- If you made changes in this branch, you'll have to do a commit again.

And that's all guys, if you find yourself in this situation, I hope this short article can help you get what you want as it did for me. Check out other uses of git reflog here

If you find this article interesting, kindly follow me on Linkedin, Twitter, Hashnode, Medium