You can use git reset –hard <remote_name>/<remote_branch_name> command to force git pull to overwrite local repository files.
This is helpful when you are getting “error: Your local changes to the following files would be overwritten by merge” or “fatal: Need to specify how to reconcile divergent branches” messages during the pull and you want to discard your local repository changes and force pull your remote changes.
Example:
Create a new git local repository:
1 2 3 |
## Create a new git local repository cd git init myrepo1 && cd myrepo1 |
Create first commit in default branch of local repository:
1 2 3 4 |
## Create first commit in default branch echo "cat" > file1 git add file1 git commit -m "commit1" |
Create a new empty git remote repository:
1 2 3 4 5 6 7 8 9 |
## Add a remote repository ## Create an empty remote git repository (i m using GitHub for this demo) ## Add your username and email to git config (optional) git config --add --local user.name cloudaffaire git config --add --local user.email cloudaffaire@gmail.com ## Add remote repository to your local repository git remote add origin git@github.com:CloudAffaire/myrepo.git git branch -M main ## rename of default local branch (master -> main) git push -u origin main |
Clone the remote repository to some other place and push a change:
1 2 3 4 5 6 7 8 |
## Now clone the remote repository to some other place and perform a change. cd git clone git@github.com:CloudAffaire/myrepo.git myrepo2 cd myrepo2 echo "dog" >> file1 git add file1 git commit -m "commit2" git push |
Next, move back to the previous git repository (myrepo1) and try to pull after some local changes:
Change an existing file (already indexed) and try to pull:
1 2 3 4 5 6 7 8 9 |
## move back to myrepo1 cd ../myrepo1 ## update an existing file in your local repo working tree echo "cow" >> file1 ## now try to pull from remote repo git pull ## error: Your local changes to the following files would be overwritten by merge: |
Observe: You will get an error “error: Your local changes to the following files would be overwritten by merge:“. Here you can either discard the local change or force pull, we will discard the local change and carry on.
Discard local change:
1 2 3 4 5 6 |
## Undo the change (discard) git reset --hard ## or ## force pull ## git reset --hard origin/main |
Create a new file (not indexed) in the working tree and try to pull:
1 2 3 4 5 |
## add a new file in your working tree echo "rat" > file2 ## now try to pull from remote repo git pull ## pull does not effect untracked files |
Observe: This time you are able to pull as it does not impact any file in your working tree that is not yet indexed by git.
Discard local change:
1 2 3 |
## Undo the change (discard) git reset --hard HEAD~1 git clean -df |
Create a new file (not indexed) in staging area and try to pull:
1 2 3 4 5 6 |
## add a new file in your staging area echo "rat" > file2 git add . ## now try to pull from remote repo git pull ## pull does not effect staged changes |
Observe: This time you are able to pull as well as it does not impact any file in your staging area that is not yet indexed by git.
Discard local change:
1 2 |
## Undo the change (discard) git reset --hard HEAD~1 |
Create a new file and commit (indexed) and then try to pull:
1 2 3 4 5 6 7 |
## add a new file in your local repo echo "rat" > file2 git add . git commit -m "commit2" ## now try to pull from remote repo git pull ## fatal: Need to specify how to reconcile divergent branches. |
Observe: You get an error “fatal: Need to specify how to reconcile divergent branches”. Again you can either discard the change or force pull but unlike last time we are going to force pull this time to demonstrate how to force pull.
Force pull discarding/overwriting local changes:
1 2 |
## force pull git reset --hard origin/main |