开发者

Why does git merge sometimes remove changes when it shouldn't?

Every so often, I've been experiencing some strange behavior in git. where changes I make to a file in one branch are removed when I merge in another branch in which unrelated changes have been made to the same file.

Let's say I start in branch master. Here's the rough outline of what happens:

vim foo.txt
git add foo.txt
git commit

git checkout -b test
vim foo.txt
git commit -a -m added a new line to foo.txt

git checkout master
vim foo.txt
git commit -a -m开发者_StackOverflow中文版 made some unrelated change

git merge test

At this point, I will discover that the change I made in foo.txt in the master branch has been removed.

I am making many other changes and performing other git operations in the middle of all of this. Since merges like this are the entire point of git, I feel like I am probably doing something wrong, at some point.

Does anyone have any idea what?


because the commit on the test branch was made last and test has a commit that can resolve a common ancestor commit, then the default behaviour is to use the new information from test as the most up-to-date information. you can force the behaviour by using the -s option. See this link for examples: http://www.kernel.org/pub/software/scm/git/docs/git-merge.html

EDITED with workflow example

mkdir showoff_git
cd showoff_git
git init
touch file_a
echo "line 1" >> file_a
git add .
git commit -m "initial commit"
git checkout -b test
sed -i='' s/1/2/ file_a
git add .
git commit -m "bluffing"
git checkout master
git merge -s ours test
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜