I have been using Git for a long time, but recently I came across an interesting trick that allows you to revert the history of file changes during merge. Here are the steps to play:
I have a Git repository with two files and one commit:
$ git branch * master $ git log --oneline 80c8d5a Initial commit $ git log --oneline -- README 80c8d5a Initial commit $ ls README conflict_file
I create a new experimental branch and modify the README files and contact_file:
$ checkout -b experiment Switched to branch 'experiment' $ vim README $ vim conflict_file $ git commit -am "Experimental changes" [experiment cdbc988] Experimental changes 2 files changed, 2 insertions(+) $ git log --oneline -- README cdbc988 Experimental changes 80c8d5a Initial commit
Returning to the master branch and editing the conflict file (for conflict during merge):
$ git checkout master Switched to branch 'master' $ vim conflict_file $ git commit -am "Master changes" [master ad8b68e] Master changes 1 file changed, 1 insertion(+)
The status of my repository is as follows:
$ git log --oneline --decorate --graph --all * ad8b68e (HEAD, master) Master changes | * cdbc988 (experiment) Experimental changes |/ * 80c8d5a Initial commit
Attempt to merge with the experimental branch:
$ git merge experiment Auto-merging conflict_file CONFLICT (content): Merge conflict in conflict_file Automatic merge failed; fix conflicts and then commit the result. $ git mergetool <merging conflict in conflict_file>
Here is the trick:
$ git reset HEAD README Unstaged changes after reset: M README $ git commit [master 909139f] Merge branch 'experiment' $ git log
I lost the changes and the history of the README file that was introduced into the experimental branches in Experimental changes . Can anyone comment on how this relates to the idea that Git knows about all the changes? Is it possible to avoid this scenario? This can be a problem in our company, because the merger is allowed for developers, but they can accidentally delete some changes.
erkfel
source share