问题
When I run git status
, this is what I am seeing:
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
You are in the middle of an am session.
(fix conflicts and then run "git am --continue")
(use "git am --skip" to skip this patch)
(use "git am --abort" to restore the original branch)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: xxx
modified: xxx
modified: xxx
Untracked files:
(use "git add <file>..." to include in what will be committed)
xxx
no changes added to commit (use "git add" and/or "git commit -a")
$ git version
git version 1.9.1
So, what' that git is trying to tell me and what's the correct way to resolve it?
I don't know if that's relevant, but we use gerrit and all changes go through review/approval process.
回答1:
fix conflicts
Do a git diff
to see if you have any merge marker, like:
$ git diff hello.txt
diff --cc hello.txt
index 5eb9649,379bd44..0000000
--- a/hello.txt
+++ b/hello.txt
@@@ -1,1 -1,1 +1,7 @@@
++<<<<<<< HEAD
+Hello, master change.
++||||||| merged common ancestors
++Hello, Original.
++=======
+ Hello, branch b1 change.
++>>>>>>> b1
If not, try and reapply git am with the -3
option: git am -3
If you have, do, for instance using kdiff3 (Windows or Linux), git mergetool --tool=kdiff3
. That will launch a graphical tool allowing you to chose between local, base and remote
+--------------------------------+
| BASE | LOCAL | REMOTE |
+--------------------------------+
| MERGED |
+--------------------------------+
With:
LOCAL
: A temporary file containing the contents of the file on the current branch.BASE
: A temporary file containing the common base for the merge.REMOTE
: A temporary file containing the contents of the file to be merged.MERGED
: The file containing the conflict markers.
The git am --continue
should only be done when git status doesn't report any unstaged files.
See more with Git Conflict Resolution by Ted Felix: it has this handy summary:
- Always use "
-3
" with "git am
" to make sure you get conflict markers.- Use "
git status
" and "git diff
" to find out what went wrong.- Resolve the conflicts by any of the following methods:
- Edit each conflicting file with your favorite editor.
- "
git checkout --theirs
" or "git checkout --ours
".- "
git checkout -m
" to undo conflict resolution on specific files. (BE CAREFUL!)- "
git mergetool
" and an appropriate merge GUI tool likekdiff3
.- "
git add
" the resolved files.- "
git am --continue
" to continue the am.
With Git 2.17 (Q2 2018), don't forget to start your git am session with git am --show-current-patch
to get a better view of the path to edit in case of conflict.
See "Show current git interactive rebase operation".
回答2:
What happened is that some file in working tree was changed, and committed prior AM session began (AM is a way to apply patches from email messages where email is split to changes and author info and then applied as a patch to repository).
This is same as if you changed file, committed it, and then tried to merge change in same file in same lines based on old version. Git simply doesn't know which version of change is valid and therefore ends up in conflict
state.
These lines tell you that you have conflict:
You are in the middle of an am session.
(fix conflicts and then run "git am --continue")
(use "git am --skip" to skip this patch)
(use "git am --abort" to restore the original branch)
There are several ways to resolve this sort of conflicts, and all of them are manual. You need some 3-way merge tool (can google for it) that allows you to compare changes and choose the one you want to keep. vim
editor AFAIK has this tool embedded, but I never used it.
There are also graphical tools allowing you to resolve conflicts in software like SourceTree or similar, but it all depends on where the repository is and whether those graphical tools are available there.
UPD: You also can revert this AM session changes by doing git am --abort
which is written in the message. This will revert branch to the state before AM session started, effectively loosing the patch information.
回答3:
Let's look at their meanings one by one...
On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits)
This means that you committed something locally but have not synced it with the origin.
Local : The repository you cloned in your machine and started working on it.
origin : The main repository from where every person can clone.
You are in the middle of an am session. (fix conflicts and then run "git am --continue") (use "git am --skip" to skip this patch) (use "git am --abort" to restore the original branch)
You were in middle of making a patch and you had conflicts , you have to either restore the things to the original state (using git am --abort
) or resolve the conflicts by following these steps.
- Type
git status
Check the status , if you see file names saying (
both modified
)Open those files , Resolve the conflicts keeping what you want and discarding the what you don't.
Now add the files you resolved the conflicts in by typing
git add file1 file2
Now its time to continue the session
- Type
git am --continue
In case you want to skip doing this patch type
git am --skip
You had some changes and you were in middle of making a patch out of it. Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory)
modified: xxx modified: xxx modified: xxx
Untracked files: (use "git add ..." to include in what will be committed)
xxx
no changes added to commit (use "git add" and/or "git commit -a")
So here git is trying to inform you about the files which are changed since the last commit . The ones that were old files and you just changed somethings here and there inside it will be shown as the modified .
The one you see under the untracked files are those which were unknown to git earlier because they are new files.
Steps to resolve this step
1.) For the untracked files
1.1.) git add <filename1> <filename2> and so on...
2.) Commit the added files to the repository
2.1) git commit -m "Message of your choice"
Note
As you have mentioned you are working with a review system as well (gerrit). You might want to just add a new patch to an existing commmit rather than a new commit. If that is the case you need to do this
git commit --amend
3.) Now its time to push the code (if you want to)
git push
For gerrit do this
git push review
来源:https://stackoverflow.com/questions/28875767/what-does-you-are-in-the-middle-of-an-am-session-mean