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

Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 6 years ago

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.

Instructor: [00:00] 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.

[00:21] 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.

[00:43] 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.

[00:57] 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.

[01:21] 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.

[01:36] 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.

[01:55] 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.

[02:15] 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.