239

I'm using Git 1.7.4.1.

I want to get the latest version of my code from the repository, but I'm getting errors:

$ git pull
….
M   selenium/ant/build.properties
….
M   selenium/scripts/linux/get_latest_updates.sh
M   selenium/scripts/windows/start-selenium.bat
Pull is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use 'git add/rm <file>' as appropriate to mark resolution, or use 'git commit -a'.

I've deleted the local copies of the files the tool is complaining about, but I still get the errors.

How do I check out the latest version from the remote repository?

Mus
  • 7,290
  • 24
  • 86
  • 130
Dave
  • 8,667
  • 25
  • 72
  • 90
  • 1
    @Kzqai's answer below helped me: try just doing `git fetch` first. I wasn't getting conflicts, but it is how I got the latest version of code. – Krista K Sep 11 '13 at 21:48

12 Answers12

302

If you don't care about any local changes (including untracked or generated files or subrepositories which just happen to be here) and just want a copy from the repo:

git reset --hard HEAD
git clean -xffd
git pull

Again, this will nuke any changes you've made locally so use carefully. Think about rm -Rf when doing this.

Vi.
  • 37,014
  • 18
  • 93
  • 148
Alex Curtis
  • 5,659
  • 3
  • 28
  • 31
  • 1
    I tried these commands and still got the error, "CONFLICT (content): Merge conflict in selenium/ant/build.xml" (amongst others). I just want to overwrite the latest version of my file with what is in the repo. What can I do?? – Dave Jun 13 '11 at 20:33
  • 35
    `git clean -f` is not really part of the answer. It will delete files not being kept in the repository (e.g. in my case, log files); you don't need to do this to get `git pull` working again, just `git reset --hard HEAD` is enough. – Darren Cook Feb 24 '12 at 03:35
  • @Dave If, like me, you'd done a commit in the local version (e.g. in a failed attempt to make git happy so `git pull` would work!!), and you are happy to throw away that local commit, then use `git log` to discover the id of the commit before, then use that instead of HEAD in the call to `git reset --hard HEAD`. E.g. `git reset --hard a12312` – Darren Cook Feb 24 '12 at 03:37
  • 14
    Esh, man, don't recommend one of the few deleting commands to git newbies. – Kzqai Jan 10 '13 at 16:45
  • this is exactly what i was looking for. thnx. @Amokrane Chentir please accept this as right answer . – Shailendra Singh Rajawat Jan 31 '13 at 08:48
  • 6
    @DarrenCook I think that the `git clean -f` is important if you want a **real** clean up of your repositoy. It is the equivalent of doing a "Delete all from folder -> Get Latest version/Get Specific" in TFS and in my case, this is what I wanted. – RPDeshaies Apr 01 '15 at 11:10
  • 3
    @Darren I think git clean -f`` is important too. I want to get the latest version, and dont want to keep obsolete caches entries, or other generated resources which are not on the repo. The answer is clear about the fact that it will remove local change and mimic the last version from the repo, so it looks perfect to me. I'd move my log files to some other place if I really need to keep them between updates. – Balmipour Oct 19 '16 at 14:25
  • Yes! This works. Override local copy and fetch latest copy from repo. – CB4 Feb 24 '17 at 15:28
  • Why is it always `git reset --hard HEAD` and not just `git reset --hard`? Is HEAD not the default as the docs say? – Webveloper Aug 06 '17 at 22:08
  • 1
    well, i didn't know that `git clean -xffd` will clean my directory. – roottraveller Sep 27 '17 at 12:09
  • 1
    I'm a newbie, and used git clean -xffd, and it cleaned my director with all the supporting plugins and stuff. Have to reinstall them again. If you're a newbie, don't use this command! – Kristiyan Lukanov Jan 22 '18 at 15:26
  • 1
    I'm a newbie. I just run git clean -xffd and now I am literally messed up. Had to run 3 hours or Initialization and update. :( – CyberBoy Jan 12 '19 at 17:23
  • There really should be a clear bold warning on this comment. Any config files that may not have been pushed to git because they contain keys or auth tokens will be wiped – William T Wild Jul 21 '23 at 16:51
292

Case 1: Don’t care about local changes

  • Solution 1: Get the latest code and reset the code

    git fetch origin
    git reset --hard origin/[tag/branch/commit-id usually: master]
    
  • Solution 2: Delete the folder and clone again :D

    rm -rf [project_folder]
    git clone [remote_repo]
    

Case 2: Care about local changes

  • Solution 1: no conflicts with new-online version

    git fetch origin
    git status
    

    will report something like:

    Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
    

    Then get the latest version

    git pull
    
  • Solution 2: conflicts with new-online version

    git fetch origin
    git status
    

    will report something like:

    error: Your local changes to the following files would be overwritten by merge:
        file_name
    Please, commit your changes or stash them before you can merge.
    Aborting
    

    Commit your local changes

    git add .
    git commit -m ‘Commit msg’
    

    Try to get the changes (will fail)

    git pull
    

    will report something like:

    Pull is not possible because you have unmerged files.
    Please, fix them up in the work tree, and then use 'git add/rm <file>'
    as appropriate to mark resolution, or use 'git commit -a'.
    

    Open the conflict file and fix the conflict. Then:

    git add .
    git commit -m ‘Fix conflicts’
    git pull
    

    will report something like:

    Already up-to-date.
    

More info: How do I use 'git reset --hard HEAD' to revert to a previous commit?

Community
  • 1
  • 1
tvl
  • 3,868
  • 2
  • 16
  • 35
  • @tvl.How can I get latest updates without committing my code(have conflicts) to branch..please help me – user3354457 Jul 10 '15 at 06:42
  • @user3354457 xm, interesting question. I've never tried this before but you may try it and report back ;) so: - Make a patch with the changes: git diff > uncommited-changes.patch - Reset the branch - pull the changes - apply the patch – tvl Jul 10 '15 at 10:03
  • 3
    This answer deserves far more upvotes than the one that has been accepted as an answer. – James Jul 13 '16 at 08:56
  • @James :) I'm glad that my answer help you. Tip, try to write documentation like you don't know anything about a subject and definitely the verbosity will help you back. – tvl Jul 14 '16 at 10:15
  • 5
    Best complete answer for git fetch. This post should be locked – Callat Oct 04 '17 at 17:25
  • Wow! Great answer. One more to add: if I have made local changes that I do NOT want to merge (eg. to reflect a custom URL on my local machine) can I get the new / updated files from the repo without overwriting my one local change? Thx. – Elisabeth Jun 06 '18 at 20:50
21

I suspect that what's happened may be that you've deleted the files that you modified (because you didn't care about those changes) and now git is taking the deletion to be a change.

Here is an approach that moves your changes out of your working copy and into the "stash" (retrievable should it actually turn out that you ever need them again), so you can then pull the latest changes down from the upstream.

git stash  
git pull

If you ever want to retrieve your files (potential conflicts with upstream changes and all), run a git stash apply to stick those changes on top of your code. That way, you have an "undo" approach.

Kzqai
  • 22,588
  • 25
  • 105
  • 137
  • 1
    I'd probably recommend using `git stash pop` unless you want a history of your git stashes – Rapnar Nov 18 '14 at 21:18
7

try this code

cd /go/to/path
git pull origin master
Amirouche Zeggagh
  • 3,428
  • 1
  • 25
  • 22
  • 7
    Welcome to Stackoverflow! Please consider adding some explanation to your solution to better describe how it solves the problem. That will make your answers even more useful – dkackman Dec 09 '17 at 13:12
7

You have to merge your files first. Do a git status to see what are the files that need to be merged (means you need to resolve the conflicts first). Once this is done, do git add file_merged and do your pull again.

Amokrane Chentir
  • 29,907
  • 37
  • 114
  • 158
  • 2
    Thanks. I was facing this problem. I have some changes in my working files, which I do not want to commit. But I cannot do a git pull in this case. So I do - git stash, git pull --rebase, git stash pop. Then manually merge and git add file_merged – RuntimeException Nov 01 '12 at 14:21
3

If you just want to throw away everything in your working folder (eg the results of a failed or aborted merge) and revert to a clean previous commit, do a git reset --hard.

damian
  • 3,604
  • 1
  • 27
  • 46
1

By Running this command you'll get the most recent tag that usually is the version of your project:

git describe --abbrev=0 --tags
ReCatCoder
  • 13
  • 1
  • 7
1

I understand you want to trash your local changes and pull down what's on your remote?

If all else fails, and if you're (quite understandably) scared of "reset", the simplest thing is just to clone origin into a new directory and trash your old one.

Dan Ray
  • 21,623
  • 6
  • 63
  • 87
1

To answer your questions there are simply two steps:-

  1. Pull the latest changes from your git repo using git pull
  2. Clean your local working directory having unstaged changes using git checkout -- . .This will show the latest changes in your local repo from your remote git repo. cleaning all the local unstaged changes.

Please note git checkout -- . will discard all your changes in the local working directory. In case you want to discard any change for selective file use git checkout -- <filename>. And in case you don't want to lose your unstaged changes use git stash as it will clean your local working directory of all the unstaged changes while saving it if you need it in the future.

0

If you are using Git GUI, first fetch then merge.

Fetch via Remote menu >> Fetch >> Origin. Merge via Merge menu >> Merge Local.

The following dialog appears.

enter image description here

Select the tracking branch radio button (also by default selected), leave the yellow box empty and press merge and this should update the files.

I had already reverted some local changes before doing these steps since I wanted to discard those anyways so I don't have to eliminate via merge later.

zar
  • 11,361
  • 14
  • 96
  • 178
0

It sounds to me like you're having core.autocrlf-problems. core.autocrlf=true can give problems like the ones you describe on Windows if CRLF newlines were checked into the repository. Try disabling core.autocrlf for the repository, and perform a hard-reset.

kusma
  • 6,516
  • 2
  • 22
  • 26
0

If the above commands didn't help you use this method:

  1. Go to the git archive where you have Fork
  2. Click Settings> Scroll down and click Delete this repository
  3. Confirm delete
  4. Fork again, and re-enter the git clone <url_git>
  5. You already have the latest version
Trinh Hieu
  • 379
  • 3
  • 6