Update git branch with master without merge
Solution 1
Since your remote feature
branch exists simply for safe-keeping purpose and has not been shared with anyone else, it is certainly safe to delete the branch or force-push to it. You can rebase your current work at branch feature
on master
, and continue working on it:
# while staying at feature branch
git rebase master
Depending on the changes in master, you may have to resolve some conflicts during the rebase.
To update the remote safe-keeping branch (which no one else has seen other than yourself):
git push -f origin feature
The history will then look like:
* newsha1 - (feature, origin/feature) feature branch commit
* newsha2 - Start of feature branch
* 0e109d5 - (HEAD, origin/master, origin/HEAD, master) latest commit
* 9188511 - major schema change
* 80d93a8 - Base commit
In essence, the commit history grows linearly, as if you only started the work on feature
after you have introduced the changes in master
.
Solution 2
I think what you want here is to rebase
feature
onto master
. Just do:
git rebase master feature
If you are currently on feature
branch, just:
git rebase master
For your questions:
It deosn't matter since you are the sole developer. After the
rebase
, just push to your remote branch with-f
or--force-with-lease
. The latter one is more safe, but no difference here for sole developer.git push --force-with-lease origin feature
Yes, you don't need to fast-forwarding master at the moment. You can do it after you finish work on
feature
. You can use--ff-only
to allow only fast-forward merge to keep your history linear.git checkout master git merge --ff-only feature
Yes, no
cherry pick
is needed here.No need to use patch.
Patch
is used to distribute your changes to others.PS.
git
is really friendly withpatch
. You can do it either way as follows.git diff > some.patch git apply some.patch
Or
git diff > some.patch patch -p1 < some.patch
Or
git diff --no-prefix > some.patch patch -p0 < some.patch
Solution 3
Seems to me as though you simply want to merge master into your feature branch?
git checkout feature-branch
git merge master
will attempt to move all of the commits in master into your feature branch, so that you can deal with conflicts / merge issues there, without touching the current state of master
.
Andrew
Since 1998, when I began my professional career in programming and web development, and while working for some of Canada's top media companies,, I've developed the following skills: Team management Site architecture hybrid mobile development (iPhone & Android -- plus BlackBerry while it lasted) SQL (MS SQL Server, MySQL, SQLite) JavaScript (my current focus is React + vanilla, but past work includes Angular, Backbone and jQuery) Node.js .NET & C# Objective C Java (Android) PHP I've also done a good deal of professional work in the past in: Adobe Flash (since version 3.0) ActionScript (original, 2.0 and 3.0) Adobe Air Perl ColdFusion WebTV
Updated on June 16, 2022Comments
-
Andrew about 2 years
I am the sole developer working on a fairly big project. I made several important changes in
master
and I'm about to resume working on afeature
branch that has fallen behind a good deal. Thefeature
branch really does need the changes frommaster
but I do not want to merge the changes intomaster
until the work onfeature
is ready for release. I think this is a pretty straight forward case for rebasing, but I am not sure. Below is a very boiled down version of my situation (the actual history is much longer).* 0e109d5 - (HEAD, origin/master, origin/HEAD, master) latest commit * 9188511 - major schema change | * d3472a5 - (origin/feature, feature) feature branch commit | * 6c36837 - Start of feature branch |/ * 80d93a8 - Base commit
- I did push
feature
to the remote for safe-keeping, which is normally a bad thing for rebasing. But since it hasn't been shared with anyone else, can I simply delete the remote branch and continue like it never existed? Myremote
is there simply for offline storage and security (it is a plain git server, not github). - Assuming the remote branch is no longer an issue, can I just rebase
master
ontofeature
and just continue working onfeature
without also fast-forwardingmaster
to the last feature branch commit? - I don't think I need to cherry pick because
feature
pretty much needs all the changes inmaster
. - I think I can also just make a patch file (from the base commit to
HEAD
and try applying it tofeature
.
Any advice is appreciated. I love git, but I have no experience rebasing yet.
- I did push
-
Andrew almost 10 yearsBut won't that also fast-forward
master
to the last commit offeature
? I really want to keepmaster
as clean as possible. -
Andrew almost 10 yearsI've just tried this out and it gives me exactly what I wanted.
feature
has all of the changes inmaster
andmaster
is still clean. Many thanks!