git Git合并在我的文件中留下了HEAD标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10657315/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:43:54  来源:igfitidea点击:

Git merge left HEAD marks in my files

gitgit-mergemerge-conflict-resolutiongit-merge-conflict

提问by lowerkey

I tried to merge a file in the command line using Git, when an error message appeared telling me the merge was aborted.

我尝试使用 Git 在命令行中合并文件,但出现错误消息告诉我合并已中止。

I thought that was the end of it, but then I realized there are gitmarks in my files. Like so:

我以为到此结束,但后来我意识到我的文件中有 gitmarks。像这样:

start =
    expression

validchar = 
    [0-9a-zA-Z_?!+\-=@#$%^&*/.]

integer = 
<<<<<<< HEAD
    digits:[0-9]+
        { return digits.join(""); }
=======
    sign:"-"* digits:[0-9]+
        { return sign + digits.join(""); }
>>>>>>> gh-pages

The files have been edited not by me and show lines inserted with:

这些文件不是由我编辑的,并显示插入的行:

  • HEAD after less than signs (<<<<<<< HEAD)
  • lines of changed code
  • a string of equals signs (=======)
  • the new version of the code
  • another line starting with greater than signs and the name of the branch (>>>>>>> gh-pages)
  • 小于号 ( <<<<<<< HEAD)后的 HEAD
  • 更改的代码行
  • 一串等号 ( =======)
  • 新版本的代码
  • 另一行以大于号和分支名称 ( >>>>>>> gh-pages)开头

What's worse is that the file contents are no longer in order. Does anyone know how I get those files back to normal, and the changes I made in the gh-branch merged into the master branch?

更糟糕的是文件内容不再有序。有谁知道我如何让这些文件恢复正常,并且我在 gh-branch 中所做的更改合并到 master 分支中?

采纳答案by Amber

Those are conflict markers. You're still in the process of merging, but there were some parts that Git couldn't merge automatically. You'll need to hand-edit those partsto what you want them to be and then commit the results.

那些是冲突标记。您仍在合并过程中,但有些部分 Git 无法自动合并。您需要将这些部分手动编辑为您想要的样子,然后提交结果。



For instance, in your particular case, you'd probably want to resolve it like this (note - the arrows/text on the right are just my notes, not something you'd type into the file):

例如,在您的特定情况下,您可能希望像这样解决它(注意 - 右侧的箭头/文本只是我的笔记,而不是您输入到文件中的内容):

integer = 
<<<<<<< HEAD                                  <-+ remove the bits here
    digits:[0-9]+                               |
        { return digits.join(""); }             |
=======                                       <-+
    sign:"-"* digits:[0-9]+
        { return sign + digits.join(""); }
>>>>>>> gh-pages                              <-- and this

and thus you'd save the file as...

因此您将文件另存为...

integer = 
    sign:"-"* digits:[0-9]+
        { return sign + digits.join(""); }

回答by GoZoner

Absolutely start with 'git status' to see what you've got. If you aborted a merge (or had a merge aborted) and you've got conflicted files in the working directory then something went wrong. The Git status will tell you where you are. After that, you have a number of options. You should resolve the merge commit either by-hand, which can be challenging, or using a tool as:

绝对从 'git status' 开始,看看你有什么。如果您中止了合并(或中止了合并)并且工作目录中存在冲突的文件,则说明出了点问题。Git 状态会告诉你你在哪里。之后,您有多种选择。您应该手动解决合并提交,这可能具有挑战性,或者使用工具作为:

git mergetool

The merge tool will work if your files are listed as needing a merge.

如果您的文件被列为需要合并,则合并工具将起作用。

You can also perform one of:

您还可以执行以下操作之一:

git checkout --ours -- /path/to/conflicted-file       # this is probably the one you want
git checkout --theirs -- /path/to/conflicted-file

You can see the different versions using the :1:filename syntax. See herefor an explanation. But all of the above assumes that 'git status' shows the files as needing a merge.

您可以使用 :1:filename 语法查看不同的版本。请参阅此处以获取解释。但是以上所有内容都假设“git status”显示文件需要合并。

Finally, you always have the option of:

最后,您始终可以选择:

git reset --hard   # sounds like --hard is what you need but check other options

回答by Mr. Pundir

All of the answers are right but if you want to Autoremove all conflict marks & want to autochange the files to keep HEAD , then You can create your own bash script like :-

所有的答案都是正确的,但是如果您想自动删除所有冲突标记并想自动更改文件以保留 HEAD ,那么您可以创建自己的 bash 脚本,例如:-

Example Script:

示例脚本:

# vim /usr/sbin/solve.git

# vim /usr/sbin/solve.git

(Append Following)

(附加以下)

#!/bin/bash
for f in $(grep -Rl '^>>>>>>> ' --include="*.php" --include="*.css" --include="*.js" --include="*.html" --include="*.svg" --include="*.txt" .)
do
sed -i -e '/^=======/,/^>>>>>>> /d' -e '/^<<<<<<< /d' $f
sed -i -e '/^>>>>>>> /d' $f
echo "$f Fixed"
done
git add . ; git commit -am "[+] Resolved on `date` from `hostname` by `whoami`" --no-verify

# chmod 755 /usr/sbin/solve.git

# chmod 755 /usr/sbin/solve.git

& just run it in your GIT repo/path to resolve:

只需在您的 GIT 存储库/路径中运行它即可解决:

$ cd <path_to_repo>
$ solve.git

$ cd <path_to_repo>
$ solve.git

Notice:- Above mentioned file extensions are php,css,js,html,svg & txt.

注意:- 上面提到的文件扩展名是 php、css、js、html、svg 和 txt。

回答by Timar Ivo Batis

In Atom i had the issue that some files did not save the resolved merge conflicts to the drive, so i had to manually click "save". Took me quite some time to figure out.

在 Atom 中,我遇到了一些文件没有将已解决的合并冲突保存到驱动器的问题,因此我不得不手动单击“保存”。我花了很长时间才弄清楚。

回答by Mohd Abdul Mujib

I'm coming from this question. And I wanted some automated method of merging the half merged files, instead of manually editing the files (as suggested in other answers, which I am not really comfortable doing). So here's what I ended up doing via netbeans, but can be done via command line too.

我来自这个问题。而且我想要一些自动合并一半合并文件的方法,而不是手动编辑文件(如其他答案中所建议的那样,我不太习惯这样做)。所以这就是我最终通过 netbeans 做的事情,但也可以通过命令行完成。

Now, bear in mind, this only works if immediately after the merge->add->commit, you realised that you messed up, and want to re-go through the process.

现在,请记住,这仅在 之后立即起作用merge->add->commit,您意识到自己搞砸了,并想重新完成该过程。

STEP 1:Reset to a previous commit.

第 1 步:重置为之前的提交。

git reset --hard a992a93f9312c6fa07c3a1b471c85e9fbf767d0e

STEP 2:Re-Try Merging the branch

第 2重新尝试合并分支

git merge --ff origin/feature/YOUR-Branch_here

At this point you shall be prompted with the merging window if you are using a GUI. and you can then proceed as normal.

如果您使用 GUI,此时将提示您使用合并窗口。然后您可以照常进行。