Update Older Branch to Newest Commit on Master or Main
(This post serves more as a mental note to myself than anything else)
If a branch, in this example feature1 on git/github was created in the past, but now master/main has some commits that are not present in feature1, this is what has to be done, to include to branches and get feature1 up to date with the latest commit on master/main:
- Get the latest commits for both branches:
1git fetch origin master #or main
2git fetch origin feature1
- Change to the
feature1branch
1git checkout feature1
- Merge the
master/mainbranch intofeature1
1git merge master
Resolve possible merge conflicts (hopefully none).
Push the updated
feature1branch to the repo
1git push origin feature1
n.b. this push will possibly have a lot of commits: all the commits that master/main was ahead of feature1. The feature1 branch get’s an added merge commit, containing all those commits. e.g. before the merge feature1 was 1 commit ahead of master/main and 42 commits behind master/main. After the merge feature1 will be two commits ahead of master/main i.e. the one commit it was ahead before the merge + the merge commit we just did.
Using merge preserves the whole history and does not rewrite commit hashes.
Hopefully that’s it.