How to `git pull` while ignoring local changes?

729,089

Solution 1

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.

Solution 2

For me the following worked:

(1) First fetch all changes:

$ git fetch --all

(2) Then reset the master:

$ git reset --hard origin/master

(3) Pull/update:

$ git pull

Solution 3

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

Solution 4

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'

Solution 5

shortest way to do it is:

git pull --rebase --autostash
Share:
729,089
markdorison
Author by

markdorison

Updated on July 08, 2022

Comments

  • markdorison
    markdorison 6 months

    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?

  • Colonel Panic
    Colonel Panic over 10 years
    If after git reset your files still differ from the remote, read stackoverflow.com/questions/1257592/…
  • Shailen
    Shailen almost 10 years
    Git is the strangest thing ever. Git reset --hard done. Then git status: Your branch is ahead by 2 commits.
  • Cascabel
    Cascabel almost 10 years
    @shailenTJ "Local changes" here means uncommitted changes, not local commits. git reset --hard affects the former, not the latter. If you want to fully reset to the remote's state, git reset --hard origin/<branch> - but often and in this case, those two commits you're ahead of origin by are work you did, not something you want to throw away.
  • Ali
    Ali about 9 years
    I think you meant "tracked files" which is exactly what I need, thanks.
  • sudo
    sudo about 9 years
    So this is the same thing as destroying the local repository and re-downloading, right? I just want to be able to force the pull and overwrite changes for convenience. 99% of the time I get this error message when I've accidentally messed something up locally and just want to start over from the repo.
  • xster
    xster over 8 years
    What if you cannot possibly not have a local change vs head? E.g. the repo was made on a case sensitive file system and is cloned on a case insensitive file system and there's 2 files with same name different casing?
  • Adambean
    Adambean about 6 years
    YES. This is what I needed for an ultimate "don't give an F about what's local" approach. Thanks. :)
  • agsachin about 6 years
    this even works when u have committed ur local changes, but still u want to revert
  • agsachin about 6 years
    this will not works if u have committed ur local changes
  • Marco Servetto
    Marco Servetto over 5 years
    I can not understand what is the purpose of point 1: what happens if I do only 2 and 3?
  • Artur Barseghyan
    Artur Barseghyan over 5 years
    @Marco Servetto: You first fetch all your git changes, but don't apply them yet. Then you reset the master to the last state (updated). If you skip the first step, you will revert changes to the old master (local). From my experience, the way I described it, never causes problems. All other attempts do at the end.
  • adelriosantiago
    adelriosantiago about 5 years
    Dangerous, very dangerous commands here! Usually this happens when you still relevant information in the repo. Doing the first group of commands will wipe these changes, doing the second one is like throwing them all into the waste bin and then saying "ohh wait this was relevant information". I would advice to use less-lethal commands like fetch, see Artur's response.
  • Cascabel
    Cascabel about 5 years
    @adelriosantiago Artur's answer also has a reset --hard. It throws away exactly the same as the part of this answer. And yes, throwing things away is potentially dangerous, but it's what the OP asked to do. Their alternative was deleting the directory and re-cloning. They wanted to throw things away. If you want to, say, commit to a local branch and also separately fetch origin's master, great, that's a more common thing to want to do - it's just not what this question was about.
  • adelriosantiago
    adelriosantiago about 5 years
    @Jefromi Ohh, you are right. That "ignores any local file changes without blowing the directory away" is a bit confusing. At first it seems that he wants to keep (ignore) these changes (for which I would recommend a fetch)
  • Yuri Ghensev
    Yuri Ghensev over 4 years
    That's what really works, even when you already have local commits that you want to remove.
  • Neri
    Neri over 4 years
    This worked for me, I wanted to ignore all my local changes including recovering deleted files
  • Ramesh Navi about 4 years
    Warning guys!! git clean -fxd removes files from .gitignore also.
  • Smily
    Smily almost 4 years
    Why using stash with pull? Git will notify you in case you have local changes afaik.
  • Earlee over 3 years
    any equivalent for Powershell?
  • Sandwich
    Sandwich over 3 years
    Correct answer.
  • Victor
    Victor about 3 years
    @RameshNavi Sure. This is exactly what is wanted. What is wanted is to have a faster way to re-clone it, i.e. to delete the entire local repo and then clone it.
  • MarredCheese
    MarredCheese almost 3 years
    I just did steps 1 and 2, and it seemed sufficient. What is the purpose of step 3?
  • mike rodent
    mike rodent almost 3 years
    @MarredCheese Yes! I want to know that: the working files have changed after the 2nd command, and the 3rd command says "Already up to date".
  • mike rodent
    mike rodent almost 3 years
    That's very similar to the 2015 answer of Artur Barseghyan... but I'd still like to know what the purpose of the 3rd command is: the working files have changed after the 2nd command, and the 3rd command says "Already up to date"
  • Artur Barseghyan
    Artur Barseghyan almost 3 years
    @MarredCheese: That's rather occasional that 3rd step is not needed in your case. I've seen enough cases when it was required. Otherwise you might find yourself working on accidentally outdated master.
  • Pablo Pazos
    Pablo Pazos almost 3 years
    that is the only combination that worked on my environment, maybe you are seeing a different thing, for me, I needed the three commands
  • mike rodent
    mike rodent almost 3 years
    Interesting. Might be a version thing. I'm on git 2.7.4. But I've also just seen a new comment from Artur Barseghyan: "Otherwise you might find yourself working on accidentally outdated master."
  • Pablo Pazos
    Pablo Pazos almost 3 years
    maybe, but the only thing I can say is "this worked for me when no other solution did", so it might help others
  • Aubin
    Aubin over 2 years
    Clearly, this is the ONLY RIGHT answer!
  • camposer
    camposer over 2 years
    This is not working when you have local changes committed. In my case I had some commit amends that wanted to rollback, the solution bellow worked for me.
  • Ujjwal Roy over 2 years
    It will delete local changes
  • Cassiano Franco over 2 years
    @UjjwalRoy From the comment on the question, I believe that was the intention. But when I want to save local changes I use git add -A git stash
  • Victor
    Victor almost 2 years
    To completely remove all the local changes, you need the command git clean -fxd
  • Victor
    Victor almost 2 years
    To completely remove all the local changes, you need the command git clean -fxd
  • DrBeco
    DrBeco almost 2 years
    Just be careful with git-clean - Remove untracked files from the working tree, as this affects "untracked files", not exactly what OP asked for. But ok, good to be here in the comment, just in case someone wants to also remove untracked files.
  • GuruJeya over 1 year
    Worked like a charm. Was stuck in a mess, but your answer worked. I wanted to ignore the merge conflicts by keeping only the changes from the remote branch. These steps worked great! Thanks :)
  • Jeaf Gilbert
    Jeaf Gilbert 8 months
    This should be the answer!