Home > Enterprise >  How can I create a commit that rolls back another one?
How can I create a commit that rolls back another one?

Time:02-16

For simplicity, let's create a repository with two commits only:

mkdir somedir
cd somedir
git init
echo hello > somefile
git add somefile
git commit -m 'first'
echo world >> somefile
git commit -am 'second'

For clarity, at this point git log --oneline returns

fd5b1ce (HEAD -> master) second
f621ab9 first

What I want to get to is the following

xxxxxxx (HEAD -> master) third
fd5b1ce second
f621ab9 first

with somefile in third having the same content as somefile in first.

I could certainly do git cherry-pick master~ (here master is fd5b1ce) and resolve the conflict totally in favour of first, but maybe there's a cleaner approach?

CodePudding user response:

commit that rolls back another one?

That seems a good fit for git revert

git revert <second commit>

But that apply to all files from second commit (being reverted)

with somefile in third having the same content as somefile in first.

If you need to revert only one file, use git restore.

git restore -SW -s=<first commit> -- somefile
  • Related