检测冲突:通过提交(Commit)和推送(Push)以及拉取(Pull)

huangapple go评论50阅读模式
英文:

jGIT Detect Conflicts by Commit&Push and Pull

问题

GIT除了合并分支外,还可以在以下情况下出现冲突:

  • 提交/推送(如果GIT中的文件在此期间被修改)
  • 拉取(如果本地文件被我修改)

在推送或拉取之前,如何列出这些冲突并标记它们为已解决?

我尝试使用Git.status(),期望在冲突下看到这些文件,但没有列出任何内容。

Status status = git.status().call();
status.getConflicting();

以下是我使用的一些代码片段:

git.pull().setContentMergeStrategy(ContentMergeStrategy.CONFLICT).setCredentialsProvider(new FxGitCredentialsProvider(getDialogScene())).call();
final AddCommand addCommand = git.add();
for (String filePattern : filePatterns){
    addCommand.addFilepattern(filePattern);
}
addCommand.call();

git.commit().setMessage(messageTextArea.getText()).setCredentialsProvider(new FxGitCredentialsProvider(getDialogScene())).call();
if (push){
    PushCommand pushCommand = git.push().setCredentialsProvider(new FxGitCredentialsProvider(getDialogScene()));
    pushCommand.call();
}
英文:

Beside merging branches, GIT can have conflicts by

  • Commit/Push ( if a file in GIT has been modified meanwhile
  • by Pull, if the local file has been modified by me.

How can I list this conflicts before the push or pull and mark them as fixed?

I tried using Git.status(), and expected to see this files under conflicts, but nothing is listed.

Status status = git.status().call();
status.getConflicting();

Here are some code snippets I use:

git.pull().setContentMergeStrategy(ContentMergeStrategy.CONFLICT).setCredentialsProvider( new FxGitCredentialsProvider( getDialogScene() )).call();
final AddCommand addCommand = git.add();
for ( String filePattern : filePatterns){
    addCommand.addFilepattern( filePattern );
}
addCommand.call();

git.commit().setMessage(messageTextArea.getText()).setCredentialsProvider( new FxGitCredentialsProvider( getDialogScene() )).call();
if ( push ){
   PushCommand pushCommand = git.push().setCredentialsProvider( new FxGitCredentialsProvider( getDialogScene()));
   pushCommand.call();
}

答案1

得分: 1

  1. git push 不会触发冲突(不像merge会产生文件级别的冲突),它只会在你要推送的提交不是现有分支尖端的快进时拒绝更新分支。

  2. git pull 实际上是一个两步命令:git fetch 用于从远程仓库获取提交,然后是 git merge <上游分支>,冲突可能会在第二步(正常的git merge)中触发。

还有其他可能触发冲突的命令:git cherry-pickgit rebase


要解决1.的问题:运行 git status 不会显示任何内容,你应该更新远程的视图(git fetch),检查历史并选择下一步操作:

  • 可能要运行 git merge <upstream/branch>git rebase <upstream/branch>。如上所述:运行 git fetch,然后运行 git merge 等同于运行 git pull,不同之处在于你可以在运行 git merge 之前检查你的仓库状态

  • 或者强制推送:git push --force-with-lease origin

要解决2.的问题:你可以运行常规合并冲突解决。

英文:

Two clarifications :

  1. git push doesn't trigger conflicts (not a file level conflict as a merge would do), it just refuses to update a branch if the commit you are pushing is not a fast forward of the existing branch tip

  2. git pull is actually a 2 steps command : git fetch, which recovers the commits from the remote repository, followed by git merge &lt;upstream branch&gt;
    conflicts may be triggered by the second step (a regular git merge)

There are other commands that may trigger conflicts: git cherry-pick and git rebase


To fix issues with 1.: you won't see anything by running git status, you should update your view of the remote (git fetch), inspect the history and select what to do:

  • probably run git merge &lt;upstream/branch&gt; or git rebase &lt;upstream/branch&gt;.
    As said above: running git fetch followed by git merge is the same as running git pull, the difference is that you can inspect the state of your repo before running git merge.

  • or force push : git push --force-with-lease origin

To fix issues with 2.: you can run a regular merge conflict resolution.

huangapple
  • 本文由 发表于 2023年1月9日 13:53:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定