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

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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

Solution 3 - Git

This worked for me:

git reset HEAD <filename>

Solution 4 - Git

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.

Solution 5 - Git

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 

Solution 6 - Git

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

Solution 7 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlankmanView Question on Stackoverflow
Solution 1 - GitAmberView Answer on Stackoverflow
Solution 2 - GitJ.M. JanzenView Answer on Stackoverflow
Solution 3 - Gitarn-arnView Answer on Stackoverflow
Solution 4 - GitChand PriyankaraView Answer on Stackoverflow
Solution 5 - GitPaulo Henrique Lellis GonalvesView Answer on Stackoverflow
Solution 6 - Gitasifaftab87View Answer on Stackoverflow
Solution 7 - GitulatekhView Answer on Stackoverflow