Update git branch with master without merge

11,085

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:

  1. 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
    
  2. 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
    
  3. Yes, no cherry pick is needed here.

  4. No need to use patch. Patch is used to distribute your changes to others.

    PS. git is really friendly with patch. 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.

Share:
11,085
Andrew
Author by

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 &amp; 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 &amp; 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, 2022

Comments

  • Andrew
    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 a feature branch that has fallen behind a good deal. The feature branch really does need the changes from master but I do not want to merge the changes into master until the work on feature 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
    
    1. 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? My remote is there simply for offline storage and security (it is a plain git server, not github).
    2. Assuming the remote branch is no longer an issue, can I just rebase master onto feature and just continue working on feature without also fast-forwarding master to the last feature branch commit?
    3. I don't think I need to cherry pick because feature pretty much needs all the changes in master.
    4. I think I can also just make a patch file (from the base commit to HEAD and try applying it to feature.

    Any advice is appreciated. I love git, but I have no experience rebasing yet.

  • Andrew
    Andrew almost 10 years
    But won't that also fast-forward master to the last commit of feature? I really want to keep master as clean as possible.
  • Andrew
    Andrew almost 10 years
    I've just tried this out and it gives me exactly what I wanted. feature has all of the changes in master and master is still clean. Many thanks!