markdorison
markdorison

Reputation: 148934

How to `git pull` while ignoring local changes?

Is there a way to do a git pull that ignores any local file changes without blowing the directory away and having to perform a git clone?

Upvotes: 758

Views: 1032282

Answers (16)

Artur Barseghyan
Artur Barseghyan

Reputation: 14202

For me the following worked:

  1. First fetch all changes:

    $ git fetch --all
    
  2. Then reset the master:

    $ git reset --hard origin/master
    

    Note - For users of github, "master" was replaced with "main" in October 2020. For projects created since then you may need to use "main" instead, like:

    $ git reset --hard origin/main
    
  3. Pull/update:

    $ git pull
    

Upvotes: 565

Raj Kumar Ram
Raj Kumar Ram

Reputation: 11

git pull will give error if we change any thing in any files in our local system, So we need to git stash

  1. git pull I got the message - error: Your local changes to the following files would be overwritten by merge: Please commit your changes or stash them before you merge. Aborting

  2. git stash Saved working directory and index state WIP on ....

  3. git pull Updating... 10 files changed, 291 insertions(+), 169 deletions(-)

Upvotes: 0

l001d
l001d

Reputation: 962

If you are on a branch, and want to discard any local changes on the branch and pull the remote branch, but encounter Your branch and 'origin/<branch_name>' have diverged, it can be resolved while staying on the branch by:

git fetch --all
git reset --hard origin/<branch_name>

Upvotes: 12

Akshay Patil
Akshay Patil

Reputation: 51

It's late but someone can find this useful.(Worked for me)

  1. git restore < fileName> or git restore .
  2. git pull

Upvotes: 5

Sergiu Savva
Sergiu Savva

Reputation: 71

Also, it's possible to keep changes from local commits and push them as a new commit. I use these steps when I have a mess in my local commits.

  1. git reset --soft origin/main
  2. git stash
  3. git pull --rebase
  4. git stash pop

Upvotes: 0

Cascabel
Cascabel

Reputation: 497252

If you mean you want the pull to overwrite local changes, doing the merge as if the working tree were clean, well, clean the working tree:

git reset --hard
git pull

If there are untracked local files you could use git clean to remove them.

  • git clean -f to remove untracked files
  • -df to remove untracked files and directories
  • -xdf to remove untracked or ignored files or directories

If on the other hand you want to keep the local modifications somehow, you'd use stash to hide them away before pulling, then reapply them afterwards:

git stash
git pull
git stash pop

I don't think it makes any sense to literally ignore the changes, though - half of pull is merge, and it needs to merge the committed versions of content with the versions it fetched.

Upvotes: 1175

Leo Bravo
Leo Bravo

Reputation: 31

.gitignore

"Adding unwanted files to .gitignore works as long as you have not initially committed them to any branch. "

Also you can run:

git update-index --assume-unchanged filename

https://chamindac.blogspot.com/2017/07/ignoring-visual-studio-2017-created.html

Upvotes: 2

gil
gil

Reputation: 2552

shortest way to do it is:

git pull --rebase --autostash

Upvotes: 34

Cassiano Franco
Cassiano Franco

Reputation: 410

I usually do:

git checkout .
git pull

In the project's root folder.

Upvotes: 13

Victor
Victor

Reputation: 1445

You just want a command which gives exactly the same result as rm -rf local_repo && git clone remote_url, right? I also want this feature. I wonder why git does not provide such a command (such as git reclone or git sync), neither does svn provide such a command (such as svn recheckout or svn sync).

Try the following command:

git reset --hard origin/master
git clean -fxd
git pull

Upvotes: 55

Pablo Pazos
Pablo Pazos

Reputation: 3216

this worked for me

git fetch --all
git reset --hard origin/master
git pull origin master

with the accepted answer I get conflict errors

Upvotes: 13

Luca C.
Luca C.

Reputation: 12594

git fetch --all && git reset --hard origin/master

Upvotes: 13

Petah
Petah

Reputation: 46060

This will fetch the current branch and attempt to do a fast forward to master:

git fetch && git merge --ff-only origin/master

Upvotes: 3

DrBeco
DrBeco

Reputation: 11785

The command bellow wont work always. If you do just:

$ git checkout thebranch
Already on 'thebranch'
Your branch and 'origin/thebranch' have diverged,
and have 23 and 7 different commits each, respectively.

$ git reset --hard
HEAD is now at b05f611 Here the commit message bla, bla

$ git pull
Auto-merging thefile1.c
CONFLICT (content): Merge conflict in thefile1.c
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.

and so on...

To really start over, downloading thebranch and overwriting all your local changes, just do:


$ git checkout thebranch
$ git reset --hard origin/thebranch

This will work just fine.

$ git checkout thebranch
Already on 'thebranch'
Your branch and 'origin/thebranch' have diverged,
and have 23 and 7 different commits each, respectively.

$ git reset --hard origin/thebranch
HEAD is now at 7639058 Here commit message again...

$ git status
# On branch thebranch
nothing to commit (working directory clean)

$ git checkout thebranch
Already on 'thebranch'

Upvotes: 43

Strahinja Kustudic
Strahinja Kustudic

Reputation: 4472

If you are on Linux:

git fetch
for file in `git diff origin/master..HEAD --name-only`; do rm -f "$file"; done
git pull

The for loop will delete all tracked files which are changed in the local repo, so git pull will work without any problems.
The nicest thing about this is that only the tracked files will be overwritten by the files in the repo, all other files will be left untouched.

Upvotes: 9

Seth Johnson
Seth Johnson

Reputation: 15192

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.

Upvotes: 11

Related Questions