All Articles

Git Revert Trap

1. See an example

Let’s see an example

Step 1. Init dir git_test and git repo

cd ~/Downloads && mkdir -p git_test && cd git_test && \
git init && \
echo 'console.log("This is init");' > index.js && \
git add . && git commit -m "init commit"

Step 2. Create branches: develop, feat-x, release

git branch develop && git checkout develop && \
echo 'console.log("This is commit in develop");' >> index.js && \
git add . && git commit -m "develop commit"

git branch feat-x && git branch release

Step 3. In branch feat-x, add one file x.js and append some codes in previous index.js, then merge it to develop

git checkout feat-x && \
echo 'console.log("code x file");' > x.js && \
echo 'console.log("feat x changes");' | cat - index.js > /tmp/out && mv /tmp/out index.js && \
git add . && git commit -m "feat: x commit" && \
git checkout develop && git merge feat-x --no-ff

Step 4. Now revert this merged commit on develop

git checkout develop && \
git revert 6aa7c6550c182605a24c95a41b7d32939f9e346f

Step 5. Go to release, make some commits

git checkout release && \
echo 'console.log("release changes1");' >> index.js && \
git add . && git commit -m "release: change1" && \
echo 'console.log("release changes2");' >> index.js && \
git add . && git commit -m "release: change2"

Step 6. Merge feat-x to release

git checkout release && git merge feat-x --no-ff

Step 7. Merge release to develop

git checkout develop && git merge release --no-ff

See the branch develop, you will find there is no changes belong to feat-x. We have this branch merged to develop previously, and we reverted that commit. Later we have commit merged from feat-x to release then release to develop. But as you can see, even this commit merged with release branch to develop later (means this commit merged to develop twice), it still not in the develop.

How to fix this:

  • make a new branch fix
  • cherry-pick the commit belongs to feat-x
  • merge fix to develop
git checkout develop && \
git branch fix && git checkout fix && \
git cherry-pick 6aa7c6550c182605a24c95a41b7d32939f9e346f

git checkout develop && git merge fix --no-ff

See the result:

EOF

Published 2022/9/5

Some tech & personal blog posts