By Ran Bar-Zik | 10/18/2018 | General |Beginners

Undo Locally in Git

Undo Locally in Git

When working in Git, there always comes that bitter moment when we actually need to undo something. An example you ask? We’ve got plenty. For instance, we ran commit but realized we did it on the wrong branch. Or maybe we ran our commit too early. Perhaps we added a file that we need to remove from a specific commit, and so on. So in general, we can divide the types of undos into two categories. The easy one is to things we did locally, meaning everything we did up to the commit. The more complex one is to undo something that was done after a push to a remote repo. In this article, I’ll cover undoing things that were done locally.

Restore a file to the local version

If, for example, we made a change locally and we want to go back to how it was before the change (meaning back to how it was after the most recent commit) it’s quite easy—git checkout and the file name or a dot and the entire file.

git checkout .

git checkout

Fix the Commit

Let’s say I ran a commit but now I want to fix it, for instance I want to add more files or make other changes or change the message. How can we do this? We fix what is needed and add the changes (if there are any) with git add, just as if we were getting ready for a new commit. But instead of running git commit we use:

git commit --amend

What happens then? I’ll be able to change the commit message, and then the files and/or changes that I want to make will be added to the latest commit as if they had been apart of it from the get-go.

git ammend

Removing a file from staging

If I ran git add to a file that’s in staging (in the status of ‘ready for commit’), but I’m debating whether or not I want to really have it in there, how can I take it out? Just use the command git reset:

git reset FILENAME

git file name

Undo a commit that’s already been made

If we ran a commit but changed our minds and now want to do a revert, this too we can do with git reset using the following command:

git reset --soft HEAD~

This command fixed the commit (before push was run, obviously) and all its changes and will return it to stage (the stage right before the commit). From here we can decide what to do with it.

git undo

Wiping out the last commit

Careful! This is something that will erase things. If you want to wipe something out, really get totally rid of the last commit, you can run the following command:

git reset --hard HEAD~

That’s it for now. In the next article, we’ll talk about changes we can make after a push.

 

Previous article: Git Hooks

Next article: Undo after a Push

 

About the author: Ran Bar-Zik is an experienced web developer whose personal blog, Internet Israel, features articles and guides on Node.js, MongoDB, Git, SASS, jQuery, HTML 5, MySQL, and more. Translation of the original article by Aaron Raizen.

By Ran Bar-Zik | 10/18/2018 | General

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
3220
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
3060
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now