Undoing Changes using Git
Git is by far an excellent tool for software developers across the globe, which keeps the track of our projects by taking snapshots of them when we commit the changes. But many a time while using it we feel that the changes we made are not relevant or will not improve our project, but yet by mistake we staged them or even committed them, what do we do now?
Tejeshwer Singh Sachdeva
Git is by far an excellent tool for software developers across the globe, which keeps the track of our projects by taking snapshots of them when we commit the changes.
But many a time while using it we feel that the changes we made are not relevant or will not improve our project, but yet by mistake we staged them or even committed them, what do we do now?
Git provides us with mechanisms or simply ways to undo our changes our revert back to the previous version of our project. Let’s discuss some of those ways
1.) Using the “git restore” command
Let’s say you’ve made some changes in one of the files in your project but you haven’t staged those changes yet using the “git add” command. Now, if you want that those changes should be removed from your file you’ll use the “git restore FILE_NAME” command.
This command will change the file’s content to the previous commit that you’ve made. But while doing so beware that once you’ve restored your file the changes that you’ve made are gone, you can then cannot demand those. If you want to discard the changes in all the files that you’ve made you can use the “git restore .” command, where ‘.’ represents all the files that will be discarded.
Another use-case to the restore command is that if you had deleted a file but now you need it(remember that the changes aren’t yet staged) so in that case too you can run the “git restore FILE_NAME” command to get your file back on your local repository.
One more use-case can be let’s say you’ve made changes at multiple places in your file and now you want to discard the changes at one place while keeping all the other changes intact. So in this case we’ll use the “git restore -p FILE_NAME” command. Here the -p flag is used to represent that the changes will be discarded on the patch/granular level. After the command, you’ll have to choose which changes to keep by typing ’n’ and which changes to discard by typing ‘y’.
Let’s discuss one more use case of the “git restore” command. Imagine a scenario where you’d want that for a certain commit only one of your files changes/reverts back to its previous versions, while the other files remain the same.
And that is possible using the “git revert --source KEY FILE_NAME” command, where KEY is the commit you want your file to revert to and the FILE_NAME is the name of that particular file you’d like to revert to its previous state.
2.) Using the “git commit --amend” command
Let’s say you’ve committed your files now but later on you make a change that was supposed to be in the previous change you made. Now you can amend that change with your previous commit using the “git commit --amend” command.
In the above example, in the previous commit, the commit message was not suitable. So to change that we just amended the commit with a new commit message. In this case, what will really happen behind the scenes is that git will create an altogether new commit with the previous versions of the files and amend it with new changes we’ve made and swap this commit with our previous commit.
Using the “git revert” command
The “git revert” command allows us to go back to any commit we’ve made in the past and then git analyzes the changes we had made in that commit. Once done analyzing it creates a new commit altogether with the changes opposite to what we had made. For example, if we had deleted a file in one of our previous commits the “git revert” command will create that file with the content that it already had.
To use the git revert command we will need the commit keys to revert back to a particular commit and we can get those keys by running the “git log” command. Once we’ve decided which commit to being reverted we can run the “git revert KEY” command to revert back.
Using the “git reset” command
Let’s say there comes a time in our project development when we want to reset the previous commits we made(look closely that’s commits on commit), and go to a commit we’ve made earlier. We can do that using the “git revert --hard KEY” command. Where the key is the commit key of the commit you want to go back to while deleting all the commits done in between, the ‘--hard’ key is used to tell that the commits in-between will be permanently deleted.
Let’s now imagine another scenario where you’ve deleted your changes but now you think that deleting the changes was a mistake. Worry not you can use the “git reflog” command to view the changes you’ve made till now. Go to the change you want to revert back to and then copy the key for the respective change you want to go back to, then run the command “git branch BRANCH_NAME KEY”, your changes will be reverted on the BRANCH_NAME you’ve specified.
One last command to discuss is “git rebase”, with this command we need to decide how much behind we want to go with our commits, let’s say in a certain case we wanna go 4 commits behind to make some changes. We can now run the “git rebase -i HEAD~4” command which will open up an editor window where you can decide what change you want to implement from the given options.
This was all about what I know about undoing changes in git. But remember one thing don’t rewrite the commits that have already been pushed to the remote repository.
Tejeshwer Singh Sachdeva
I am a Front-End Developer based in India. I am currently pursuing my Bachelors in Computer Science and Engineering from ITM University, Gwalior. Apart from that, I am an all-time explorer in the field of web & app development👨💻.