2

In IntelliJ I was working on a new branch from a remote branch called stage. My branch was called PM-43655-stage. I committed my changes and did git push -u origin PM-43655-stage. It created a merge request into the stage branch. Now It shows that the source branch is 2 commits behind the target branch.

There is no merge conflicts though.

I get confused between git pull and git fetch and I'm not sure what to do in this case.

torek
  • 448,244
  • 59
  • 642
  • 775
Aditya K
  • 135
  • 2
  • 4
  • 10
  • 2
    This is a wonderful, approachable resource to learning the fundamentals of git: https://www.atlassian.com/git/tutorials/syncing (check the whole "Collaborating" section) – msanford Dec 21 '21 at 14:18
  • Does this answer your question? [What I can do to resolve "1 commit behind master"?](https://stackoverflow.com/questions/34118404/what-i-can-do-to-resolve-1-commit-behind-master) – pkamb Dec 21 '21 at 16:41
  • Yes, I mean there are multiple ways to do it. I wanted to try git pull to resolve this. – Aditya K Dec 21 '21 at 23:18

1 Answers1

9

This message means that the branch you're trying to merge into (stage in this case) has advanced since you branched out of it to create your MR. If there are no merge conflicts it can still be merged, but it's usually a good practice to have it up to date before merging so you can make sure the CI runs on the actual code that that will get merged.

You can do this with git pull, which both fetches the remote changes and applies them, but personally, I prefer to have more control and rebase myself in case there are conflicts I want to resolve:

git fetch origin
git rebase origin/stage

Once you're done with that, you'll need to push your updated branch:

git push --force origin PM-43655-stage

EDIT:
With regard to the issue of local changes you don't want to commit brought up in the comments, the easiest approach would probably be to stash them - i.e., put them aside while you pull and push, and then return them:

# Put your local changes aside
git stash save

# Pull the stage branch
git pull origin stage

# Push the updated branch
git push --force origin PM-43655-stage

# Retrieve the uncommited changes and go back to work
git stash pop
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    Yeah so I remember trying git pull. So lets say in my local I'm in branch PM-43655-stage. So in this branch itself I'll do git pull origin stage(which is the target branch) right? – Aditya K Dec 21 '21 at 13:34
  • @AdityaK yes, indeeed – Mureinik Dec 21 '21 at 13:35
  • So I have some uncommitted changes in my local that I do not wish to change or commit and push. I tried git pull origin stage and got an error: Your local changes to the following files would be overwritten by merge: src/main/resources/logback.xml – Aditya K Dec 21 '21 at 13:41
  • I would like my local changes to remain as it is. If I commit them and do a git pull it will push these changes too right? – Aditya K Dec 21 '21 at 13:43
  • @AdityaK I think it's easier to stash them. I've edited my answer with the details, see there (it's a bit inconvenient to describe this in the comments) – Mureinik Dec 21 '21 at 13:47