简体   繁体   中英

How do I keep merge in Git from overwriting all my master's changes since the branch was first created?

I create a branch, "test1," change a file, and do not merge it back in master. I forget about the test for a week. I made lots of changes in master since that test1 branch.

But, then I decide I still like that one feature in test1 that I did, a single file I changed.

When I merge the branch test1 into master, what gets merged? Just the test1 file? I don't want to overwrite all my new master files with the old test1 branch files (except that one file I changed).

The default merge strategy, recursive , will find the common ancestor and attempt to apply the changes that occurred in the test1 branch to the current branch (ie master ). So the command git merge test1 will preserve the changes that occurred in the master branch, and apply the one change that you performed on the test branch.

And fear not! If things do somehow go awry (eg there is a conflict but you resolve it incorrectly) git makes it easy to reset a branch back to an earlier commit and try the merge again.

You'll want to ensure you check out the branch you wish to merge into (the master branch in your case), and then run the git merge command.

git checkout master
git merge test1

Also check the 'Basic Merging' section on the official documentation here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM