[英]Git: Make auto-merge files into conflicted ones
我有一個包含一些代碼的 github 存儲庫。 另一個人分叉了該存儲庫並克隆了它。 他們更改了一些代碼。 我想用他們的代碼替換我所有的代碼(接受他們的所有更改)。 我跑了
git pull -X theirs https://github.com/epicmanmoo/JavaStudyBuddies.git master
這對於沖突的文件工作正常,它刪除了我的代碼,留下了他們的代碼。 問題是只有 ====== >>>>> <<<< 內容(沖突文件)的文件才如此。
問題是像Column.java
這樣的文件沒有被標記為沖突。
列.java :
package javastudybuddies.discordbots.entities;
public enum Column {
NAME("name", "name", "projects"),
DESCRIPTION("description", "description", "projects"),
STATUS("status", "status", "projects"),
COMPLETED("completed", "completed", "projects"),
DIFFICULTY("difficulty", "difficulty", "projects"),
TYPE("type", "type", "projects"),
USERNAME("username", "username", "users"), LEVEL("level", "level", "users"),
COUNTRY("country", "country", "users"), TIMEZONE("timezone", "timezone", "users"),
AGE("age", "age", "users"), IDSTRING("id_string", "id", "users"), TAGSTRING("tag_string", "tag", "users"),
GOAL("goal", "goal", "users"), TECH("tech", "tech", "users");
public final String databaseLabel;
public final String userLabel;
public final String table;
...
它是
public enum Column {
NAME("name", "name", "projects"),
DESCRIPTION("description", "description", "projects"),
STATUS("status", "status", "projects"),
TYPE("type", "type", "projects"),
COMPLETED("completed", "completed", "projects"),
DIFFICULTY("difficulty", "difficulty", "projects"),
USERNAME("username", "username", "users"), LEVEL("level", "level", "users"),
COUNTRY("country", "country", "users"), TIMEZONE("timezone", "timezone", "users"),
AGE("age", "age", "users"), IDSTRING("id_string", "id", "users"), TAGSTRING("tag_string", "tag", "users"),
GOAL("goal", "goal", "users"), TECH("tech", "tech", "users");
public final String databaseLabel;
public final String userLabel;
public final String table;
...
看到不同? 在我的版本中 TYPE 是最后一個,在他的版本中 TYPE 在中間,因此它甚至不會注冊為合並沖突。
為了調試,我運行了通常版本的git pull
(沒有-X theirs
):
git pull https://github.com/epicmanmoo/JavaStudyBuddies.git master
該版本也這樣做:
public enum Column {
NAME("name", "name", "projects"),
DESCRIPTION("description", "description", "projects"),
STATUS("status", "status", "projects"),
TYPE("type", "type", "projects"),
COMPLETED("completed", "completed", "projects"),
DIFFICULTY("difficulty", "difficulty", "projects"),
TYPE("type", "type", "projects"),
USERNAME("username", "username", "users"), LEVEL("level", "level", "users"),
COUNTRY("country", "country", "users"), TIMEZONE("timezone", "timezone", "users"),
AGE("age", "age", "users"), IDSTRING("id_string", "id", "users"), TAGSTRING("tag_string", "tag", "users"),
GOAL("goal", "goal", "users"), TECH("tech", "tech", "users");
public final String databaseLabel;
public final String userLabel;
public final String table;
並且不包含該文件
git diff --name-only --diff-filter=U
(沖突文件列表)
而另一個文件,比如 ProjectsBot.java 被標記為沖突,當我打開它時,我看到了這個
<<<<<<< HEAD
public class ProjectsBot extends ListenerAdapter {
private enum Command {
=======
public class ProjectsBot extends ListenerAdapter {
private enum Command {
>>>>>>> b858d8ab9c06ee2645a0dda716d9b5e14a6db11d
CREATE_PROJECT("create project \"name\", \"description\", \"difficulty\""),
JOIN_PROJECT("join project 'name\"");
public final String syntax;
<<<<<<< HEAD
Command(String syntax) {
=======
Command(String syntax) {
>>>>>>> b858d8ab9c06ee2645a0dda716d9b5e14a6db11d
this.syntax = syntax;
}
}
所以,我的問題是,如果不是像Column.java
這樣的文件,
git pull -X theirs https://github.com/epicmanmoo/JavaStudyBuddies.git master
將是理想的解決方案 - 它會自動接受他們的更改,我可以立即推動他們。 有沒有辦法讓它適用於Column.java
文件? 讓它們在內心看起來有點像這樣:
>>>>HEAD
NAME("name", "name", "projects"),
DESCRIPTION("description", "description", "projects"),
STATUS("status", "status", "projects"),
COMPLETED("completed", "completed", "projects"),
DIFFICULTY("difficulty", "difficulty", "projects"),
TYPE("type", "type", "projects"),
=====
NAME("name", "name", "projects"),
DESCRIPTION("description", "description", "projects"),
STATUS("status", "status", "projects"),
TYPE("type", "type", "projects"),
COMPLETED("completed", "completed", "projects"),
DIFFICULTY("difficulty", "difficulty", "projects"),
>>>>theircommit
這樣-X theirs
選項就可以用它下面的所有內容替換=====
上面的所有內容?
簡短的回答是否定的,你不能那樣做。
長的答復是,你可以這樣做,但你必須編寫自己的合並策略:不只是合並的驅動程序,而不是像一個戰略選擇-X theirs
,但是就像一個戰略-s my-new-strategy
,這將有Git 調用git-merge-my-new-strategy
,這意味着您必須在$PATH
以這種方式拼寫命令。 編寫策略非常困難(一種開始方法是克隆 Git 的副本,然后將現有的遞歸策略復制到一個新的策略中,並在新策略中編寫一堆新的 C 代碼,使用遞歸策略作為初始點)。
如果涉及的文件不多,我建議改為使用:
git checkout <branch>
git merge --no-commit <options> <commit-specifier>
然后使用您認為最好的任何技術手動解析每個文件。 --no-commit
意味着無論 Git 是否認為合並成功——使用-X theirs
通常會認為合並成功——合並將停止,就好像存在沖突一樣,這使您可以編輯和git add
文件.
當你全部完成后—— git diff --cached HEAD
使用git diff HEAD
或git diff --cached HEAD
將你在工作樹(第一個命令)或索引(第二個命令)中的內容與HEAD
提交進行比較是很有用的,看看你的最終合並結果會有什么不同——運行:
git merge --continue
提交合並,或:
git commit
如果您的 Git 太舊而無法支持git merge --continue
。 (該git merge --continue
命令只運行git commit
,而是使那里有一個合並運行前提交git commit
,所以它作為一個檢查,以確保沒有別的出了問題。例如,如果你不小心做了git reset
在您解決問題時,您可能已經刪除了合並狀態。在這種情況下,最終的git commit
只會進行普通的非合並提交,而不是進行合並提交。這也可以恢復,但更煩人修復它而不是首先不犯錯誤 - 所以git merge --continue
是一件好事。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.