git 如何使用Git和命令行在合并期间保留本地文件或远程文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6650215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to keep the local file or the remote file during merge using Git and the command line?
提问by e-satis
I know how to merge modification using vimdiff, but, assuming I just know that the entire file is good to keep or to throw away, how do I do that?
我知道如何使用 vimdiff 合并修改,但是,假设我只知道整个文件可以保留或丢弃,我该怎么做?
I don't want to open vimdiff for each of them, I change want a command that says 'keep local' or 'keep remote'.
我不想为每个人打开 vimdiff,我想要一个显示“保持本地”或“保持远程”的命令。
E.G: I got a merge with files marked as changed because somebody opened it under windows, changing the EOL, and then commited. When merging, I want to just keep my own version and discard his.
EG:我合并了标记为已更改的文件,因为有人在 Windows 下打开它,更改 EOL,然后提交。合并时,我只想保留我自己的版本并丢弃他的。
I'm also interested in the contrary: I screwed up big time and want to accept the remote file, discarding my changes.
我也对相反的情况感兴趣:我把时间搞砸了,想接受远程文件,放弃我的更改。
回答by Waiting for Dev...
You can as well do:
你也可以这样做:
git checkout --theirs /path/to/file
to keep the remote file, and:
保留远程文件,并且:
git checkout --ours /path/to/file
to keep local file.
保留本地文件。
Then git add
them and everything is done.
然后git add
他们和一切都完成了。
Edition:
Keep in mind that this is for a merge
scenario. During a rebase
--theirs
refers to the branch where you've been working.
版本:请记住,这是针对merge
场景的。在 a 期间是rebase
--theirs
指您一直工作的分支。
回答by keflavich
This approach seems more straightforward, avoiding the need to individually select each file:
这种方法看起来更直接,避免了单独选择每个文件的需要:
# keep remote files
git merge --strategy-option theirs
# keep local files
git merge --strategy-option ours
or
或者
# keep remote files
git pull -Xtheirs
# keep local files
git pull -Xours
Copied directly from: Resolve Git merge conflicts in favor of their changes during a pull
直接复制自: 在拉取期间解决 Git 合并冲突以支持其更改
回答by Ben Wilde
git checkout {branch-name} -- {file-name}
git checkout {branch-name} -- {file-name}
This will use the file from the branch of choice.
这将使用来自所选分支的文件。
I like this because posh-git
autocomplete works great with this. It also removes any ambiguity as to which branch is remote and which is local.
And --theirs
didn't work for me anyways.
我喜欢这个,因为posh-git
自动完成很适合这个。它还消除了关于哪个分支是远程的和哪个是本地的任何歧义。并--theirs
没有为我反正工作。
回答by sehe
For the line-end thingie, refer to man git-merge
:
对于线端的东西,请参考man git-merge
:
--ignore-space-change
--ignore-all-space
--ignore-space-at-eol
Be sure to add autocrlf = false
and/or safecrlf = false
to the windows clone (.git/config)
确保添加autocrlf = false
和/或添加safecrlf = false
到 Windows 克隆 (.git/config)
Using git mergetool
使用 git 合并工具
If you configure a mergetool like this:
如果您像这样配置合并工具:
git config mergetool.cp.cmd '/bin/cp -v "$REMOTE" "$MERGED"'
git config mergetool.cp.trustExitCode true
Then a simple
然后一个简单的
git mergetool --tool=cp
git mergetool --tool=cp -- paths/to/files.txt
git mergetool --tool=cp -y -- paths/to/files.txt # without prompting
Will do the job
会做这份工作
Using simple git commands
使用简单的 git 命令
In other cases, I assume
在其他情况下,我假设
git checkout HEAD -- path/to/myfile.txt
should do the trick
应该做的伎俩
Editto do the reverse (because you screwed up):
编辑做相反的事情(因为你搞砸了):
git checkout remote/branch_to_merge -- path/to/myfile.txt