Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Push a rebased local branch by using `--force-with-lease`

    Juri StrumpflohnerJuri Strumpflohner

    When you update your local feature branch using git rebase, you’re rewriting the history. As such, when trying to update your remote branch counterpart using git pull you will be rejected. In this lesson we’re going to have a look how to use the --force and --force-with-lease to force an update of the remote branch.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: Since we are using rebase, we are actually rewriting history as we mentioned before. There's one strange side effect you might encounter. Let's simulate that. Let's push our branch up onto Origin. Let's assume this is a branch we are working on which we are creating and pushing up every day to have just a remote backup in case something goes wrong with our machine.

    Now our branch is synchronized with the remote repository. Let's simulate another change in master. Let's simply open up the readme. Let's here simply change the title. This git demo repository. Let's save this. Let's add this to our repository. Let's create a comment.

    Let's do git log again. As we can see, we now have another comment on master which is not yet on our app refactoring, so let's do the same game again. Let's jump over to app refactoring. Let's do a git rebase with master.

    Again, it will synchronize our branch with master so we again have a linear history here and we have synchronized everything. As you can see, we get all that commit off the update of the readme. However, you can already see these arrows here. If I want to synchronize now my app refactoring branch with remote, we will get a message that it has been rejected.

    The reason it was rejected was because the tip of our current branch is behind the remote counterpart, which might sound strange because we actually added new stuff other than being behind and no one else wanted to change our remote branch.

    The reason however is because we rewrote those comment hashes. Git doesn't recognize them anymore and therefore we have to synchronize back down to change the result in the remote repository. However, we don't want that. What we actually want is we know that our local branch is updated and we want to override our remote counterpart.

    We use a git push minus F which stands for minus force which overrides the remote repository branch we are currently pushing. Be aware to not do that on master or something but just on your feature branch and only do it if your feature branch is actually just your own personal one and it's not shared with anybody else.

    Also, you can add the force with lease which is a bit more secure as it does some additional checks if no one else has modified that branch. With that we have now overridden our remote counterpart and we are again up to date.