How to Uncommit Changes in Git

In this tutorial we are going to discuss how to uncommit changes in git, we will see those git commands but first, let’s have a quick introduction to git.

Introduction

GIT is an open source application that is used for managing source codes, git enables multiple developers to share their code in an online repository and it also allows other developers to perform any changes or can do editions in the source code which developers can upload(push) or download(pull) the source codes in the repository by executing the git commands in the command prompt.

By using git commands we can perform many operations like creating repositories and pushing and pulling the source codes etc. but sometimes as we all are human we make mistakes so in case, you did a mistake so there is no need to worry because git allows us to uncommit those changes which we mistakenly commit.

There are some commands that we can use to uncommit the changes in git, so let’s start with the first one.

Reset last changes in Commit

Use the following command to reset the last changes you did in git.

git reset --soft HEAD~1

The above command with a soft parameter will transfer a branch in the last commit.

Check git status

Use the following command to check the status.

git status

Discard commit changes permanently.

You can permanently discard all the changes in a commit by using the following command.

git reset --hard 

You can use the above command with a hard parameter, it will undo all the changes you did to the previous commit. Before using this command just make sure that you really want to discard all the changes because once you executed the command there is no way to reverse the process.

Revert specific commit changes

If you just want to revert a specific commit that you commit lastly, so Check out the next command below.

git reset –hard 532570d

The above command will revert the commit with the specified hash commit id.

Edit commit message

In case of change your mind, you don’t want to discard the changes in commit instead you want to edit so there is a way to do it, see the following command.

git commit –amend –m “new commit message”

The above command will amend the last message.

Discard changes from a single repo file

If you want to discard the changes to the single file only in the repository so try the following command.

git checkout -- path/to/the/file.txt

Type checkout with two dashes and then specify the path, the above command will revert that file into its last state.

Remove a whole commit from history

Instead of discarding changes from a commit if you want to discard the whole commit from history so you can use the following command.

git rebase -i [revision number] 

Be careful while using the above command it will disappear your whole commit and maybe this will create more complications in your git repository so just think twice then use the above command.

Conclusion

In this tutorial, we discussed the commands to uncommitted changes from git. At first, we discussed an introduction to git and then we saw how to uncommit changes using the different commands, we saw the command to permanently uncommit the changes, and then we saw the command to revert last commit changes.

We also saw the command to edit a commit message, we saw the command to Discard changes from a single repository file as well.

In the end, we saw how to disappear the whole commit by using the git rebase command.

That’s all for today hope you like the tutorial if you have any queries regarding this tutorial so feel free to contact us below. Thanks for reading.

Suggested Article:

Leave a Comment