70

I have a server where there's some config that I don't properly know where i just git pull and it gets what is in a github repo, then restart it in order to deploy.

The thing is, there's a commit which isn't my latest, that isn't really on my server. The files aren't in .gitignore. How do I assure that a pull, pulled a commit?

I really don't know how to fix it, I'm thinking about restarting everything :(

14:41][root@someserver] someserver_dir (master)$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   Gemfile
#   modified:   Gemfile.lock
#   modified:   config/assets.yml
#   modified:   config/database.yml
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   randomfiles
Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
thiagofm
  • 5,693
  • 4
  • 20
  • 26
  • More information will make it easier to answer your question. What does `git status` show you? Are you on the right branch locally? For instance, if you're looking at the `master` branch on the server, are you checked out to `master` locally? How about `git log origin/master..master`? – Adam Monsen Mar 13 '12 at 17:27
  • Is the git repository public? If so, provide a URL. – Adam Monsen Mar 13 '12 at 17:35
  • not public, git status shows me everything is cool, there's only a branch. – thiagofm Mar 13 '12 at 17:42
  • I've tried commiting the changes again, and magically, when I pull, nothing happens(already up to date). – thiagofm Mar 13 '12 at 17:43
  • Include the output of `git status`. – Adam Monsen Mar 13 '12 at 17:44
  • Thanks. Will you also include `git config --list`? – Adam Monsen Mar 13 '12 at 17:54
  • Exclude any sensitive data of course. The point of my asks for additional data are that you need to figure out which question to ask, and more information will help me (or someone) help you ask it. – Adam Monsen Mar 13 '12 at 21:09

4 Answers4

201

If you always want your server version to reflect a commit from your repo, it's probably better to use git reset instead of git pull - that way you never invoke merge functionality, but instead set all of the files to exactly what they are in the commit you reset to. For example:

git fetch origin master
git reset --hard FETCH_HEAD
Amber
  • 507,862
  • 82
  • 626
  • 550
  • 12
    I always forget this command, so I keep coming back to this thread. Just wanted to add that changing 'master' to any other branch name works as well, if you want to fetch everything from a specific branch. – mdegges Dec 13 '15 at 00:34
  • 2
    you might as well reset to a specific branch, so git reset --hard origin/master also works, so as any branch you specify. (quite funny to see myself asking this question 5 years ago, I remember I was managing some remote server which used git to deploy and ended up in a bad state.. haha) – thiagofm Jul 17 '17 at 14:33
  • After this files are restored as in origin, but i got this "fatal: Could not reset index file to revision 'FETCH_HEAD'." Anything I have to do? – Sadee Mar 29 '18 at 11:08
  • Saved my day!!! Been struggling with this for quite a while, couldn't figure out why i was not pulling latest changes in a brand new branch, from origin master. – pr1nc3 Nov 06 '18 at 09:09
  • 1
    Awesome it worked. I tried `git fetch origin ` followed by `git reset --hard FETCH_HEAD`. This branch was visible on github.com but not in my local, even after trying `git pull` many times. – paul Jan 12 '22 at 10:12
  • ```git fetch origin main``` now – dclipca Feb 09 '22 at 04:30
13

You can use git pull origin branch_name.

Ex: If I have a production branch on GitHub, then I will write git pull origin production which will give me all the latest commits.

Only doing git pull sometimes does not give you the latest commits of production branch even though you are working on that branch and committing it.

Behind the scenes working

http://git-scm.com/docs/git-pull#_default_behaviour

Explanation and Examples

DinoMyte
  • 8,737
  • 1
  • 19
  • 26
invinciblemuffi
  • 908
  • 1
  • 11
  • 22
3

Try cleaning-up your local repository with and then run git pull:

$ git gc --prune=now
$ git remote prune origin
techExplorer
  • 810
  • 7
  • 16
0

Check if your branch is tracking the right latest commit. Check it using git branch -avv

If it really does not track the right commit, hard reset the FETCH_HEAD using git reset --hard [commit hash]

Aminah Nuraini
  • 18,120
  • 8
  • 90
  • 108