I forget to do git pull before and commit on my branch, so now my branch is 5 commits behind master and 1 commit ahead of master.
If I do "git pull origin master" my problem will be solved?
The first commit behind was a merge with the 2 branchs, so I'm afraid of having a merge conflict.
What should I do in this case?
CodePudding user response:
Currently, your history looks somewhat like
/-A-B-C-D-E (origin/master)
common
\-X (master (local))
(A-B-C-D-E
might not be linear since you meantioned being behind a merge but that doesn't change much.)
Since git pull
is equivalent to fetching and merging, it would by default (rebasing is another possibility) create a new merge commitlike the following:
/-A-B-C-D-E-\ (origin/master)
common |-M (master (local))
\-X---------/
This state would be 2 commits ahead, 0 commits behind behind
as you have all commits in your local branch but X
and M
are missing in the remote branch.
Pushing would push those two commits to origin/master
resulting in both your local branch and the remote branch being at the same state.
So yes, you can pull and it will solve the diverging branches.
A merge conflict can occur if the same (parts of the same) file have been modified from both sides (local and remote). In this case, you need to manually resolve the conflicts.
CodePudding user response:
I forget to do git pull before and commit on my branch,
It's very common and correct to commit before a pull. In fact, you often must commit your changes before you pull.
You can now:
git pull
(you should not need to addorigin master
) and deal with the possible merge issues. A merge conflict doesn't automatically means you did something wrong, many merge conflicts are legitimate issues that need to be resolved by humans.git pull --rebase
. From Git Book's 3.6 Git Branching - RebasingYou can also simplify this by running a
git pull --rebase
instead of a normalgit pull
. Or you could do it manually with a git fetch followed by a git rebase teamone/master in this case.
CodePudding user response:
It looks like you have done some work and committed on your local repo, but haven't pushed yet.
One way to place your commit on top of those five commits is :
git fetch origin master
git rebase origin/master
# same as :
git pull --rebase origin master
Check if the result matches your expectations, you can push your branch.