Git

git merge and git merge — squash

My story

I had a problem today when I merged my feature branch into master branch. This is the story. After I had finished implementing a certain feature, my boss odered me to merge it into master branch. There were a few difference between master branch and my feature branch. On the feature branch I pull master branch in order to merge the differences into feature branch.

After that, I switched to master branch then merged the feature branch into master branch successfully.

I checked again carefully in case occurring errors.

Obviously there was no room involving me. So I pushed the merged master branch to remote server.

-> % git merge feature/merge
Updating ab1bedb..4fd8f53
Fast-forward
merge.md | 1 +
test.txt | 1 +
2 files changed, 2 insertions(+)
create mode 100644 merge.md

Revert merge commit

Suddenly my boss decided that we won’t update my feature branch today. I’m afraid that I had to revert my working code. Since I thought that what only I had to do is just revert previous commit that indicated to my feature branch, I reverted it like the following command.

git revert 4fd8f533e85f630bc44a955d87b934b465c175d9 <- previous commit number

I got an error…

error: commit 4fd8f533e85f630bc44a955d87b934b465c175d9 is a merge but no -m option was given.
fatal: revert failed

Apparently reverting merge commit required -m option with number 1 or 2. So I retried by the following command

-> % git revert -m 1 4fd8f533e85f630bc44a955d87b934b465c175d9
[master 6c184aa] Revert “Merge branch ‘master’ of github.com:mitsuyacider/testGitMerge into feature/merge”
1 file changed, 3 deletions(-)
delete mode 100644 master.md

Apparently this command was successful. But this was the start to the despair and tragedy. What I had done in the revert command was just only reset 2 commits before. My working code that I wrote in the feature branch, with commits except the 2 commits, still exist in master branch.

What was happening??

Merge with squash option

To tell the truth the merge command has several options. If I executed the command with — squash option, the merging files would have shown in the list when I command git status but I just executed git merge.

Once I pull the master branch on the feature branch, there is no difference between master and feature branch except merge commit, which means that the only merge commit exists between two.

Therefore as I switch the branch from feature to master, all commits on feature branch were merged straightway. If I took it with — squash option, the result would have got the following.

-> % git merge feature/merge --squash
Updating ab1bedb..4fd8f53
Fast-forward
Squash commit -- not updating HEAD
 merge.md | 1 +
 test.txt | 1 +
 2 files changed, 2 insertions(+)
 create mode 100644 merge.md

You can see the message Squash commit — not updating HEAD. This message means you must commit all of your making code on the feature branch manually. The commit will turn to be merge commit as a result.