UNDO Mistakes with GIT #2

Bennison J
YavarTechWorks
Published in
5 min readJan 11, 2024
UNDO Mistakes with GIT #2

Hey Everyone, welcome to part ✌️ of the “UNDO Mistakes with GIT” series! In this article, we will learn more about undoing mistakes in Git. If you haven’t read my previous article on “UNDO mistakes with Git” 📚, please take a look before moving on to this one. 🔄

Undoing RESET with git reflog

First of all, if you don’t know about git reset, kindly look at my previous article, which will help you to understand this command.

  • If you are using the git reset with the — hard option, it will rewrite the commit history.
git rest --hard <commit-hash>
  • Running this command with --hard the option will delete all commits before the commit that we mentioned with the git reset command.
  • In case, you ran this git reset command with the option--hard the commit history after this commit won’t be available in the git log. we won’t see those commit histories using the command git log .
  • But we can get the commit hash of the deleted commits using the command git reflog.
  • I want to explain this with an example, suppose you have a commit history as follows.
A -- B -- C -- D -- HEAD
  • And let’s assume that you have run the following git command antecedently.
git reset --hard B # Here "B" is a commit hash
  • Here in this command, you reset the commit back to commit B. After this command, the commit history looks as follows
A -- B -- HEAD

Now our scenario is that need to recover the mistakenly rest commits (Commit C and Commit D).

Here in this step one is, we need to check the git reflog.

git reflog
  • This command shows the log of the recent changes to the HEAD (In this case HEAD is changed from pointing D to pointing B).
  • Now as a second step need to identify the commit hashs associated with the commit C and D in the ref log history.

Now it is time to recover the last commit. Here we need to use the commit hashes obtained from the git reflog to recover the last commit in git branch the command.

git branch <new-branch-name> <commit-hash>
  • Here the above command will create a new branch, that points to commit D (In our case commit D), and allows you to recover the mistakenly reset commit.

Recovering Deleted Branch

  • If you accidentally 🚫 delete a branch, but in some cases, you need that, you can get that deleted branch with the help of the ref log. 🔄

Here in the below reflog, the first one is the commit-hash, basically git reflog tracks every single action git. So it mostly looks like as follows

12a5ea5 (HEAD -> demo, origin/main, main) HEAD@{0}: checkout: moving from main to demo
12a5ea5 (HEAD -> demo, origin/main, main) HEAD@{1}: reset: moving to 12a5ea5
12a5ea5 (HEAD -> demo, origin/main, main) HEAD@{2}: checkout: moving from main to main
12a5ea5 (HEAD -> demo, origin/main, main) HEAD@{3}: checkout: moving from demo to main
12a5ea5 (HEAD -> demo, origin/main, main) HEAD@{4}: checkout: moving from main to demo
  • First what we just need to do is, just run the git reflog and get the commit hash of the deleted branch.
  • We can recover the deleted branch by using the commandgit branch (by creating a new branch).
git branch <new-branch-name> <commit-hash>
  • I would like to explain it more elaborately with real-time examples.
  • Imagine we start by creating a project directory called git-demo using the command git init, with the default branch named master (or main in some cases).
  • First, we create a file, add content to it, and commit these changes with the message “First Commit”. If you check the reflog at this point using git reflog, it might look something like this
34fc699 (HEAD -> master) HEAD@{0}: commit (initial): First Commit
  • Then, we are going to create a new branch named ‘test’ using the command.
git checkout -b test
  • After creating the ‘test’ branch and moving the HEAD to ‘test’ using the checkout command with the -b option, the reflog shows this action as the movement from ‘master’ to test. So, the reflog might look like this
34fc699 (HEAD -> test, master) HEAD@{0}: checkout: moving from master to test
34fc699 (HEAD -> test, master) HEAD@{1}: commit (initial): First Commit
  • Then, you perform some changes or commits on the test branch. Suppose you decide to switch back to the ‘master’ branch. When you move back to masterfrom the ‘test’ branch, the reflog will update, and it might appear as follows.
34fc699 (HEAD -> master, test) HEAD@{0}: checkout: moving from test to master
34fc699 (HEAD -> master, test) HEAD@{1}: checkout: moving from master to test
34fc699 (HEAD -> master, test) HEAD@{2}: commit (initial): First Commit
  • And now the HEAD is in the branch master , and now I am going to delete the branch with the following command
git branch -D <branch> # In our case test instead of <branch>
  • Once you have done this if you look at the reflog there is nothing different, it looks like the same in the above.
34fc699 (HEAD -> master) HEAD@{0}: checkout: moving from test to master
34fc699 (HEAD -> master) HEAD@{1}: checkout: moving from master to test
34fc699 (HEAD -> master) HEAD@{2}: commit (initial): First Commit
  • To recover the deleted ‘test’ branch, we need to refer to the specific entry that captures the last commit or action made on the test branch before deletion. In this case, it could be identified using
34fc699 (HEAD -> master) HEAD@{1}: checkout: moving from master to test
  • This reference (HEAD@{1}) points to the commit hash (34fc699) just before the ‘test’ branch was deleted. Using this commit hash, you can recreate the ‘test’ branch
  • The above reference just says that this is the commit hash value (34c699) after you moved to the branch test from the master. so you can use this 34c99 to recover the deleted branch.
git branch recoverd-test-branch 34c99
  • Executing this command will recreate the ‘test’ branch from the specified commit hash (34fc699).

Git has tools like git reset and git reflog to fix mistakes, but be careful with git reset — hard as it can permanently change your work. Using git reflog helps bring back lost work, keeping your project safe, and helping to correct errors. 🔧⚠️🔄

--

--

Bennison J
YavarTechWorks

👩‍💻 Software Engineer 🚀 UNIX/Linux ♥️ | JavaScript/Node.js 🔥 | SQL 📊 | Backend Developer 💻 | Tech Blogger ✍️ | Tech Enthusiast 🌟 |Continuous Learner 📚