Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git pull doesn't actually restore my missing files from remote

Tags:

git

I have been working on a branch. I committed and pushed it up to the remote repository. Now some files on that branch are missing. Hopefully they are still available on the remote branch, so I tried to do git pull:

git pull origin feature/my_branch 

However, git said all is synchronized with remote:

 * branch            feature/my_branch -> FETCH_HEAD Already up-to-date. 

How can that be up to date? I cannot find my missing files locally, and my project didn't compile because of those missing files. Again, I can see those files in the commit history of the remote branch on bitbucket.

like image 520
Malloc Avatar asked May 29 '13 01:05

Malloc


People also ask

Does git pull affect remote?

The short answer is simple: no, the remote-tracking branch remains unaffected.

Does git pull pull everything?

The git pull command first runs a git fetch command to check for changes. The fetch operation returns the metadata for our commits . Then, the git pull command retrieves all the changes we have made to our remote repository and changes our local files.

Does git pull overwrite remote changes?

For obvious safety reasons, Git will never simply overwrite your changes.


1 Answers

git pull corresponds to the sequence of a git fetch and then git merge. So if you delete the files from your local repository, doing a git pull will not restore them.

In order to restore them you need to checkout that files from the remote repository using:

git checkout <branch name> -- <path/to/file> 

<branch name> can be local or remote.

Example: Using remote repository

  1. Remote upstream name: origin
  2. Remote branch: feature/xpto
  3. Local branch: feature/xpto
  4. File missing on local branch: example.js
  5. Recover the file to the local branch from remote branch: git checkout origin/feature/xpto -- example.js
  6. Now you have example.js back on your local branch

Example 2: Rollback a commit

  1. git log
  2. Current commit hash 7bb2154cf23b68feb0a0bbbd95446f2cd8bf3a44
  3. Want to rollback to this commit hash dbc04c23b2c6f2dd7bc9b5ef4aef22611a57c3ad
    • git checkout dbc04c23b2c6f2dd7bc9b5ef4aef22611a57c3ad
  4. Only want to recover the file example.js from the above commit hash
    • git checkout dbc04c23b2c6f2dd7bc9b5ef4aef22611a57c3ad -- example.js
like image 156
Daniel Gomes Avatar answered Sep 19 '22 03:09

Daniel Gomes