16

I'm currently just messing around with git and cant figure out how to set a branch to a newer commit. My current git history looks like this:

6be8bf1 (HEAD, main)
701c50a
95cfe6b (origin/mybranch)
1a82bd5
...

How can I edit my history to look like below?

6be8bf1 (HEAD, main, origin/mybranch)
701c50a
95cfe6b
1a82bd5
...
flakes
  • 21,558
  • 8
  • 41
  • 88

2 Answers2

21

If your branch is behind by main then do:

git checkout main (you are switching your branch to main)
git pull 
git checkout yourBranch (switch back to your branch)
git merge main

After merging it, check if there is a conflict or not.
If there is NO CONFLICT then:

git push

If there is a conflict then fix your file(s), then:

git add yourFile(s)
git commit -m 'updating my branch'
git push
flakes
  • 21,558
  • 8
  • 41
  • 88
Bayram Binbir
  • 1,531
  • 11
  • 11
8

(assuming your local is in sync with origin i.e. you've run git fetch already):

git checkout mybranch

git branch --set-upstream-to=origin/mybranch mybranch

git merge main

Verify that your setup looks like this at this stage:

6be8bf1 (HEAD, main, mybranch)
701c50a
95cfe6b
1a82bd5

git push origin mybranch

flakes
  • 21,558
  • 8
  • 41
  • 88
sdayal
  • 1,126
  • 10
  • 16
  • Thank you so much!! Can you explain a bit about what --set-upstream-to does? – flakes Nov 02 '15 at 18:33
  • http://git-scm.com/book/ch3-5.html You use it when `you already have a local branch and want to set it to a remote branch you just pulled down` – sdayal Nov 02 '15 at 18:37
  • 1
    well.. I think you're exaggerating a little here. In addition, the --set-upstream step can be done when pushing by setting the same flag there or just giving the short option `-u` – Vogel612 Nov 02 '15 at 20:25