The Git pull command allows you to fetch from and integrate with another repo or local branch.
There may come a time in your workflow when you wish to forcefully override your local branch’s history and files to match your remote. While Git does have ways of making this happen, you actually won’t be using the Git pull command.
We’re going to walk you through how to force a pull to overwrite your local branch history using the cross-platform GitKraken Git GUI before going over how to accomplish the same thing in the command line.
Why would you want to Git pull force?
A few situations might arise in your workflow that would encourage you to forcefully override your local branch.
Let’s say, for example, that the history on your remote has dramatically changed from your local copy; simply doing a Git pull could create a very confused history, code conflicts, or both. On the other hand, you may have numerous changes on your local repo that are now irrelevant; maybe you and a team member mistakenly worked on a bug on the same branch and they solved it first.
In short, you would want to perform a Git pull force if you don’t care about our local copy’s history and just want to get your repo “back up to speed” with your remote as quickly as possible.
Without the visual context offered by GitKraken, knowing which remote branch you’re targeting when overwriting local changes can be challenging.
How do I Git pull force with GitKraken?
In this example of a Git pull force, let’s say you have a local copy of a branch that is behind its remote version. Here is what that would look like in the central GitKraken graph:
Let’s say you just want to overwrite your local branch with whatever changes the remote branch has. You won’t want to perform a Git pull, because then your local copy would then have both your undesired local history and the remote changes.
The Git pull action is actually a series of two other Git commands: a Git fetch followed by a Git merge.
The Git merge action is the part that prevents the Git pull action from performing in the desired manner in this use case; instead of overwriting changes, it’s actually merging the remote branch into your local branch, preserving both sets of changes. This can also open the door to some nasty Git merge conflicts. 😱
GitTip: See the difference between Git pull and Git fetch and understand how each command works.
What about git pull --force
? While it might seem that this command would result in a Git pull force, adding the --force
flag to the Git pull command actually just passes the flag down to git fetch
, making it a Git fetch force instead.
So this will actually yield the same result as just using git pull
on its own.
To force your local branch to be overwritten by the remote in GitKraken, you will take the following steps:
1.Fetch – you can perform a Git fetch in GitKraken by clicking the convenient Fetch
button in the top toolbar.
You can also perform a Git fetch in GitKraken by right-clicking the remote in the left panel.
2. Double-click the remote ref directly in the graph.
3. When prompted, select Reset Local to Here
at the top of the GitKraken UI.
Boom! 💥 Mission accomplished! You have forcefully overwritten your local history using the GitKraken Git client. Yes, it’s really that easy.
You can verify that the action was performed successfully by noting that your local branch matches with the remote history in your central graph.
GitKraken presents many benefits over the command line when it comes to overwriting local fines and changes in Git, including better visual representation of what actions are being performed and to which branches.
Forcefully overwriting local changes can be a risky operation. GitKraken’s one click Undo/Redo protects developers from common mistakes.
How do you Git pull force in the command line?
The process for forcing your local branch to be overwritten by your remote in the CLI is similar.
- You will start by performing a Git fetch.
2. Next, you will need to find the commit SHA for the remote branch you want to pull changes from:
git rev-parse <remote-branch-name>
(git rev-parse
is faster than running git log <remote-name>
or git log <branch-name>
)
3. Then you will use git reset --hard
followed by the remote’s current commit SHA. This will reset your local branch to that remote branch.
🚨Warning🚨 The git reset --hard
command is considered highly dangerous because it will destroy all work on the local branch, committed and uncommitted changes included. So be absolutely sure you wish to discard the changes from your local branch before performing this action.
GitKraken includes verification prompts for destructive operations, like Git reset hard, so you can avoid catastrophic mistakes.
4. At this point, because you lack the visual context offered by the GitKraken graph, you will need to verify that your local branch and remote branch are pointing to the same commit.
git diff <local-branch-name> <remote-branch-name>
If they are not pointing to the same commit, you will see something like this:
Sometimes, it is faster to just overwrite your changes locally if you’ve gotten too far behind your remote. Forcing an overwrite of local changes doesn’t have to be intimidating or destructive with the help of the cross-platform GitKraken Git client for Windows, Mac, & Linux.