I'm lacking some fundamental understanding of git revert and the commit history here. Reproducible example:
mkdir git_test
cd git_test
git init
touch test.txt
echo "asdf" >> test.txt
git add test.txt
git commit -m "added file"
echo "asdf2" >> test.txt
git add -u
git commit -m "added another line in main"
git checkout -b feature
echo "asdf3 in feature" >> test.txt
git add -u
git commit -m "added a third line in feature"
echo "asdf4 in feature" >> test.txt
git add -u
git commit -m "added 4th line in feature"
git checkout master
echo "adding line 3 in main" >> test.txt
git add -u
git commit -m "added line 3 in main"
I have two branches, feature
and master
(which I call main above, sorry)!
Now I can merge feature into main, and resolve conflicts:
git merge feature # accept incoming changes in conflict
git add -u
git commit -m "merge feature into main"
If I regretted this merge I can do git revert -m 1 be62f73
. Since this is mimicking a public branch, I don't want to do a git reset
.
So, this works alright, but normally I can't merge anything directly into master
, so I'd have to create a PR
, where I'd have to first merge master
into feature
instead:
git checkout feature
git merge master # accept currenct changes in conflict
git add -u
git commit -m "merge master into feature"
git checkout master
git merge feature
If I now try git revert -m 1 225747f
I get Already up to date! On branch master \n nothing to commit, working tree clean
.
So, why can't I revert it in the second instance? There must be some fundamental concept I'm not understanding here?
If HEAD
is a merge commit, what does HEAD
really contain? What does it mean to revert a merge commit? When I merge two branches, isn't the merge commmit just signaling that "here some merge happened", but the commits from the other branch is mixed into the commit history?
If I regretted a merge of feature
into master
, is there a way of regretting just parts of the commits that were moved from feature
, instead of caring about the actual merge commit? I really don't understand how this merge commit relates to regular commits that is incoming into the new branch when merging.
EDIT: adding a screenshot to show that the last commit that I can't revert, actually is a merge commit: