英文:
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
-
git push
不会触发冲突(不像merge
会产生文件级别的冲突),它只会在你要推送的提交不是现有分支尖端的快进时拒绝更新分支。 -
git pull
实际上是一个两步命令:git fetch
用于从远程仓库获取提交,然后是git merge <上游分支>
,冲突可能会在第二步(正常的git merge
)中触发。
还有其他可能触发冲突的命令:git cherry-pick
和 git 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 :
-
git push
doesn't trigger conflicts (not a file level conflict as amerge
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 -
git pull
is actually a 2 steps command :git fetch
, which recovers the commits from the remote repository, followed bygit merge <upstream branch>
conflicts may be triggered by the second step (a regulargit 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 <upstream/branch>
orgit rebase <upstream/branch>
.
As said above: runninggit fetch
followed bygit merge
is the same as runninggit pull
, the difference is that you can inspect the state of your repo before runninggit merge
. -
or force push :
git push --force-with-lease origin
To fix issues with 2.
: you can run a regular merge conflict resolution.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论