git checkout custom_branch && git rebase master
This will update custom_branch with changes from master branch.
Don't forget to make sure master is up to date first. git pull
This is also possible with git checkout custom_branch && git merge master
For an explanation on why the first one is (probably) what you should be using: When do you use git rebase instead of git merge? Answer from tehp on Stack Overflow
git checkout custom_branch && git rebase master
This will update custom_branch with changes from master branch.
Don't forget to make sure master is up to date first. git pull
This is also possible with git checkout custom_branch && git merge master
For an explanation on why the first one is (probably) what you should be using: When do you use git rebase instead of git merge?
git switch custom_branch # switch current working environment to custom_branch
git merge master # merge master branch into custom_branch
This will update your current branch with the changes from your local master branch, the state of which will be that of when you last pulled while on that branch.
I think this is what you are looking for: git merge origin master
Not sure if this is the right place to ask this. My branch is behind origin master by many commits, I tried to git merge master to my branch earlier but my git log history now shows a bunch of other people’s commits, but I actually just wanted to keep my branch up-to-date with master in one commit only, something like “Merge branch ‘master’ into ‘some-branch’” any help is appreciated. Thanks
git - Can I split a feature branch and then merge it back to the origin master branch without any issues? - Software Engineering Stack Exchange
How do I merge a new branch into my main branch? I am running into issues
git - How to merge remote master to local branch - Stack Overflow
version control - Get changes from master into branch in Git - Stack Overflow
You can certainly branch off of any branch. If your refactor branch is long-lived, and you need to add a new feature based off it the work you've already done, create feature2 off the refactor branch.
However, those 2 branches are intertwined now. You'll need to regularly pull from refactor into feature2 (in addition to pulling from main into refactor). Your PR from feature2 into refactor will only reflect the diffs between those 2 branches, but when you open a PR from refactor into main you will get see diffs from feature2 as well - something to keep in mind for whoever is reviewing.
In order for the feature2 PR to be as clean as possible, the changes you make in feature2 should be completely unrelated to the changes you're making in refactor. When you start changing the same functions in the same files on the two branches, that means conflicts. Given your use-case though (the feature depends on the work you're doing in the refactoring) this may be unavoidable.
Given all that, I'd caution about having such a long-lived branch (refactor) that you are doing other development on (feature2) at the same time. If this is truly a refactoring, adding in new features can complicate the determination that the refactoring was successful (is a bug due to the refactor or the new feature). So while it is possible to branch off of your refactoring branch, I'd evaluate whether you really want to do that, or focus on wrapping up the refactoring first.
Use git rebase to move the branches (refactor and feature) when work happens on the other branches (main and refactor) and finally when refactoring is merged. You can also move the commits from refactor that you need in feature to the base and first merge those into main rebasing refactor and feature on the merge of those changes.
Given new changes in refactor do:
git switch feature;
git rebase refactor...feature --onto refactor
Graphically you are doing. From:
* Refactor: new changes feature should be rebased on these
| * Feature
| * a commit in Feature
|/
* Fearure...Refactor (common base of Feature and Refactor)
* a commit in Refactor
* Main: before starting refactorring
To:
* Feature
* a commit in feature
* Refactor: new changes feature should be rebased on these
* this was the common base now it's just a commit
* a commit in Refactor
* Main: before starting refactoring
For changes in Main you rebase in two steps first Refactor onto Main then Feature onto the moved Refactor. Make sure to remember the hash of the base of feature (on create a tempoeary branch for it):
git branch tmp/feature-base feature...refactor;
git switch refactor;
git rebase refactor...main --onto main;
git switch feature;
git rebase tmp/feature-base --onto refactor;
git branch -D tmp/feature-base;
Graphically from:
* Main: new changes both branches should have these
| * Feature
| * a commit in Feature
| * tmp/feature-base, Refactor
| * a commit in Refactor
|/
* Refactor...Main: before starting refactoring
Intermediate:
* Refactor
* a commit in refactor
* Main: new changes both branches should have these
| * Feature
| * a commit in Feature
| * tmp/feature-base: this commit is no longer is the refactor branch but it is in the history of Feature
| * a commit in Refactor
|/
* before starting refactoring
To:
* Feature
* a commit in Feature
* Refactor
* a commit in refactor
* Main: new changes both branches should have these
* before starting refactoring
Once Refactor is merged into main rebase Feature onto the merge commit, this leads to a semi linear history with features visible and small commits that tell a story about why the code is as.
* Feature
* a commit on feature
* merge refactor into main
|\
| * Refactor
| * a commit in refactor
|/
* before starting refactoring
Don't be affraid of rebasing or merge conflicts.
From your feature branch (e.g configUpdate) run:
git fetch
git rebase origin/master
Or the shorter form:
git pull --rebase
Why this works:
git merge branchnametakes new commits from the branchbranchname, and adds them to the current branch. If necessary, it automatically adds a "Merge" commit on top.git rebase branchnametakes new commits from the branchbranchname, and inserts them "under" your changes. More precisely, it modifies the history of the current branch such that it is based on the tip ofbranchname, with any changes you made on top of that.git pullis basically the same asgit fetch; git merge origin/master.git pull --rebaseis basically the same asgit fetch; git rebase origin/master.
So why would you want to use git pull --rebase rather than git pull? Here's a simple example:
You start working on a new feature.
By the time you're ready to push your changes, several commits have been pushed by other developers.
If you
git pull(which uses merge), your changes will be buried by the new commits, in addition to an automatically-created merge commit.If you
git pull --rebaseinstead, git will fast forward your master to upstream's, then apply your changes on top.
I found out it was:
$ git fetch upstream
$ git merge upstream/master