290

I want to get the latest file that's in the repository, and overwrite what I have locally. How can I do this with the git client?

abdullahalali
  • 396
  • 2
  • 5
  • 18
Blankman
  • 259,732
  • 324
  • 769
  • 1,199

7 Answers7

560

If you want to overwrite only one file:

git fetch
git checkout origin/main <filepath>

If you want to overwrite all changed files:

git fetch
git reset --hard origin/main

(This assumes that you're working on main locally and you want the changes on the origin's main - if you're on a branch, or your project uses the old master main branch name rather than main, substitute that in instead.)

Luke Hutchison
  • 8,186
  • 2
  • 45
  • 40
Amber
  • 507,862
  • 82
  • 626
  • 550
37

Simplest version, assuming you're working on the same branch that the file you want is on:

git checkout path/to/file.

I do this so often that I've got an alias set to gc='git checkout'.

J.M. Janzen
  • 671
  • 1
  • 8
  • 19
  • 5
    Simple, elegant, and does the job. Just remember to 'git fetch' before. – Almir Campos Jul 28 '17 at 22:23
  • 6
    `git checkout path/to/file` worked for me. Also, I found this diagram to be very useful to understand conceptually what `git checkout` is doing. [link](http://images.osteele.com/2008/git-transport.png) – Cale Sweeney Aug 22 '17 at 23:00
  • After using this solution, attempting `git pull` results in `Your local changes to the following files would be overwritten by merge: path/to/file` and the merge does not occur. How to complete `git pull` so that this threatened overwrite finally happens? – Ceph Sep 21 '22 at 12:16
14

This worked for me:

git reset HEAD <filename>
arn-arn
  • 1,328
  • 1
  • 21
  • 31
7

I believe what you are looking for is "git restore".

The easiest way is to remove the file locally, and then execute the git restore command for that file:

$ rm file.txt
$ git restore file.txt 
4

Full sync has few tasks:

  • reverting changes
  • removing new files
  • get latest from remote repository

git reset HEAD --hard

git clean -f

git pull origin master

Or else, what I prefer is that, I may create a new branch with the latest from the remote using:

git checkout origin/master -b <new branch name>

origin is my remote repository reference, and master is my considered branch name. These may different from yours.

Community
  • 1
  • 1
Chand Priyankara
  • 6,739
  • 2
  • 40
  • 63
2

if you want to override all your local changes with specific branch then u can do

git reset --hard origin/feature/branchname

feature/branchname -> is my branch name by which I replaced my all local changes

My intention to take all the changes from feature/branchname branch and commit to a branch in which I am working, the name of my branch is 'mybranch'. Then I first replaced my local changes which is I already pushed to my branch mybranch, then I have to pushed this command to my branch. Force push command is

git push --force

It will push whatever changes I got from 'feature/branchname' branch to my 'mybranch'.

asifaftab87
  • 1,315
  • 24
  • 28
1

After running into this problem, I finally tried git checkout --force <branch> and it did exactly that.

ulatekh
  • 1,311
  • 1
  • 14
  • 19
  • That's what I was trying to do, thank you. Somehow I started not to be able to sync all the remote branches (deleted ones, and new ones) on my local. Does anyone know how to do this for all the existing branches on remote? The resulting branches on local would be only the active branches on remote GitHub repo. – Cagri Sarigoz Apr 28 '22 at 07:25