Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force overwrite of local file with what's in origin repo?

Tags:

git

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?

like image 424
Blankman Avatar asked Oct 16 '10 16:10

Blankman


People also ask

How do I force git pull to overwrite local files?

Just like git push --force allows overwriting remote branches, git fetch --force (or git pull --force ) allows overwriting local branches.

How do I force git to checkout a file?

Force a Checkout You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD ). Basically, it can be used to throw away local changes.

Does git pull origin master overwrite local changes?

The reason for error messages like these is rather simple: you have local changes that would be overwritten by the incoming new changes that a "git pull" would bring in. For obvious safety reasons, Git will never simply overwrite your changes.

How do I discard local changes in git and pull?

Look at git stash to put all of your local changes into a "stash file" and revert to the last commit. At that point, you can apply your stashed changes, or discard them.


2 Answers

If you want to overwrite only one file:

git fetch git checkout origin/master <filepath> 

If you want to overwrite all changed files:

git fetch git reset --hard origin/master 

(This assumes that you're working on master locally and you want the changes on the origin's master - if you're on a branch, substitute that in instead.)

like image 139
Amber Avatar answered Sep 22 '22 15:09

Amber


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'.

like image 28
J.M. Janzen Avatar answered Sep 20 '22 15:09

J.M. Janzen