Eduard Florinescu
Eduard Florinescu

Reputation: 17541

How can I locally cancel all the changes made in a commit?

I have a specific directory where the sources where modified in a specific commit 0x0x0x0x0. After that commit have been many other commits all on the master.

I want to cancel that commit 0x0x0x0x0 and locally(not upstream) in this specific directory have the code prior to when this commit was made. What git commands can I run for this?

Later edit: I want just that specific commit canceled not the subsequent changes. As code it doesn't affect me since it is an independent module. Also I will not use this local repository for committing further upstream.

Upvotes: 1

Views: 264

Answers (2)

Sergio Trapiello
Sergio Trapiello

Reputation: 758

It's possible to return to any commit with the following command:

git reset --hard commit_identifier

Note: to obtain the commit identifier use git log command

You can also reset the last commit with command:

git commit --amend

Take a look to the next link for more information:

http://www.kernel.org/pub/software/scm/git/docs/git-reset.html

Upvotes: 1

poke
poke

Reputation: 388303

If you want to go back to before the commit was introduced, so effectively removing its changes and all changes that happened afterwards (in other commits), you can do the following:

  • Check out a new branch to the old version, so you don’t lose any information but can just restart from the old state: git checkout -b <newbranchname> commithash~1
  • Reset your current branch to the old version, effectively removing all the commits from the history if no other branch points to them: git reset --hard commithash~1

But if you just want to undo said commit while keeping all the changes that were made afterwards, you can just do this:

git revert commithash

Upvotes: 5

Related Questions