dcp dcp - 1 month ago 5
Git Question

clarification on git revert and rebase

Consider the following steps:

git init
touch foo.txt
git add .
git commit -m 'initial commit'

Now, we'll create a new branch and add a file called foo2.txt:

git checkout -b new_branch
touch foo2.txt
git add .
git commit -m 'added foo2.txt'

Now we'll go back to the master branch, and revert the 'initial commit' we did (note: you can find out commit hash of 'initial commit' using git log command).

git checkout master
git revert <commit hash of initial commit>

At this point, we have neither foo.txt nor foo2.txt in the master branch. Now, let's merge new_branch to master

git merge new_branch

At this point, foo2.txt is now merged into master, but foo.txt is not there.

So the question is, how do I get foo.txt back to master? I tried doing this:

git rebase new_branch

But that does not bring it back. So in general, if we revert a commit on master, then we need to merge another branch back to master, how would we force it to bring over any 'missing' files (foo.txt in this example)?

The only workaround I found, which was rather drastic, was to delete master and rename new_branch to master.


Make a new commit to bring foo.txt back. You could either git revert <revert commit> or git cherry-pick <init commit> or add foo.txt by hand and commit.

Or remove <revert commit> from the history. Reset master to <add foo2 commit> via git reset <add foo2 commit> --hard. It could also be made via git rebase --onto <init commit> <init commit> <add foo2 commit>, but it ends in detached HEAD state so you need to reset master to this new HEAD.