I've read the Basic Branching and Merging section of the Git Community Book.
So I follow it and create one branch: experimental
.
Then I:
there are some conflicts but after I resolve them, I did 'git add myfile'
And now i am stuck, I can't move back to master
when I do
$ git checkout master
error: Entry 'res/layout/my_item.xml' would be overwritten by merge. Cannot merge.
and I did:
$ git rebase --abort
No rebase in progress?
and I did :
$ git add res/layout/socialhub_list_item.xml
$ git checkout master
error: Entry 'res/layout/my_item.xml' would be overwritten by merge. Cannot merge.
What can I do so that I can go back to my master branch?
The first thing I want to make clear is that branch names are just an alias to a specific commit. a commit is what git works off, when you pull, push merge and so forth. Each commit has a unique id.
When you do $ git merge , what is actually happening is git tries to fast forward your current branch to to the commit the referenced branch is on (in other words both branch names point to the same commit.) This scenario is the easiest for git to deal, since there's no new commit. Think of master jumping onto the lilipad your branch is chilling on. It's possible to set the --no-ff flag, in which case git will create a new commit regardless of whether there were any code conflicts.
In a situation where there are code conflicts between the two branches you are trying to merge (usually two branches whose commit history share a common commit in the past), the fast forward won't work. git may still be able to automatically merge the files, so long as the same line wasn't changed by both branches in a conflicting file. in this case, git will merge the conflicting files for you AND automatically commit them. You can preview how git did by doing $ git diff --cached. Or you can pass the --no-commit flag to the merge command, which will leave modified files in your index you'll need to add and commit. But you can $ git diff these files to review what the merge will change.
The third scenario is when there are conflicts git can't automatically resolve. In this case you'll need to manually merge them. In my opinion this is easiest to do with a merge took, like araxis merge or p4merge (free). Either way, you have to do each file one by one. If the merge ever seems to be stuck, use $ git merge --continue, to nudge it along. Git should tell you if it can't continue, and if so why not. If you feel you loused up the merge at some point, you can do $ git merge --abort, and any merging will undo and you can start over. When you're done, each file you merged will be a modified file that needs to be added and committed. You can verify where the files are with $ git status. If you haven't committed the merged files yet. You need to do that to complete the merge. You have to complete the merge or abort the merge before you can switch branches.
Whenever You merge two branches using command git merge brancha branchb
, There are two possibilities:
One branch (lets say brancha) can be reached by the other branch (lets say branchb) by following its commits history.In this case git simply fast-forward the head to point to the recent branch (in this case branchb).
2.But if the two branches have diverged at some older point then git creates a new snapshot and add a new commit that points to it. So in case there is no conflict between the branches you are merging, git smoothly creates a new commit.
Run
git log
to see the commit after you have merged two non-conflicting branches.
Now coming back to the interesting case when there are merge conflicts between the merging branches. I quote this from the page https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging
Git hasn’t automatically created a new merge commit. It has paused the process while you resolve the conflict. If you want to see which files are unmerged at any point after a merge conflict, you can run
git status
So in case there are merge conflicts, you need to resolve the conflict then add the changes you have made to the staging area using git add filename
and then commit the changes by using the command git commit
which was paused by git because of the conflict.I hope this explains your query. Also do visit the link above for a detailed understanding. In case of any query please comment below , I'll be happy to help.
After all files have been added, the next step is a "git commit".
"git status" will suggest what to do: files yet to add are listed at the bottom, and once they are all done, it will suggest a commit at the top, where it explains the merge status of the current branch.
A merge conflict occurs when two branches you're trying to merge both changed the same part of the same file. You can generate a list of conflicts with git status
.
When the conflicted line is encountered, Git will edit the content of the affected files with visual indicators that mark both sides of the conflicting content.
<<<<<<< HEAD
conflicted text from HEAD
=======
conflicted text from merging_branch
>>>>>>> merging_branch
When you fix your conflicted files and you are ready to merge, all you have to do is run git add
and git commit
to generate the merge commit. Once the commit was made ,git push
the changes to the branch.
Reference article: Git merge.
It may be late. It is Happen because your git HEAD is not updated.
this commend would solve that git reset HEAD
.
How do I finish the merge after resolving my merge conflicts?
With Git 2.12 (Q1 2017), you will have the more natural command:
git merge --continue
See commit c7d227d (15 Dec 2016) by Jeff King (peff
).
See commit 042e290, commit c261a87, commit 367ff69 (14 Dec 2016) by Chris Packham (cpackham
).
(Merged by Junio C Hamano -- gitster
-- in commit 05f6e1b, 27 Dec 2016)
See 2.12 release notes.
merge
: add '--continue
' option as a synonym for 'git commit
'Teach '
git merge
' the--continue
option which allows 'continuing' a merge by completing it.
The traditional way of completing a merge after resolving conflicts is to use 'git commit
'.
Now with commands like 'git rebase
' and 'git cherry-pick
' having a '--continue
' option adding such an option to 'git merge
' presents a consistent UI.
Steps to resolve conflict:
"git checkout "MAIN_BRANCH"
"git merge origin/BRANCH_NAME_TO_BE_MERGED"
Auto-merging src/file1.py
CONFLICT (content): Merge conflict in src/file1.py
Auto-merging src/services/docker/filexyz.py
Auto-merging src/cache.py
Auto-merging src/props.py
CONFLICT (content): Merge conflict in src/props.py
Auto-merging src/app.py
CONFLICT (content): Merge conflict in src/app.py
Auto-merging file3
CONFLICT (content): Merge conflict in file3
Automatic merge failed; fix conflicts and then commit the result.
Now you can see it is showing "CONFLICT (content)", to those file which is having "CONFLICT", see your code and resolve them
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: file3
both modified: src/app.py
both modified: src/props.py
both modified: src/utils/file1.py
git add file3
git add src/app.py
git add src/props.py
git add src/utils/file1.py
On branch MAIN_BRANCH
Your branch is ahead of 'origin/MAIN_BRANCH' by 10 commits.
(use "git push" to publish your local commits)
7."git push"
Just git commit
it.
Optionally git abort
it:
I ran into a merge conflict. How can I abort the merge?
To make life easier with on merges install kdiff3 and configure it as a mergetool. Instructions: http://doodkin.com/2016/05/29/git-merge-easy-github-this-branch-has-conflicts-that-must-be-resolved-use-the-command-line/
That page contains this video: https://www.youtube.com/watch?v=Cc4xPp7Iuzo
The next steps after resolving the conflicts manually are:-
git merge --continue
, git cherry-pick --continue
, git rebase --continue
]In case you ever get stuck during a merge/rebase you can always
git reset --hard
to restore your working to the state of the last commit. This will lose your changes from the working tree so if you had local modifications before the merge they will be gone after this—which is why it’s advisable to not start a merge when you have local modifications. :)
Source: Stackoverflow.com