Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git pull based on SHA

Tags:

git

I would like to know how to do a pull from repo based on a SHA?

git pull origin master

the above code will pull master once we've done git add remote.

like image 714
d3bug3r Avatar asked Aug 16 '12 15:08

d3bug3r


People also ask

How do you pull from a commit hash?

To pull up a list of your commits and their associated hashes, you can run the git log command. To checkout a previous commit, you will use the Git checkout command followed by the commit hash you retrieved from your Git log.

What is SHA in git?

As is well-known, Git has been using SHA-1 to calculate a hash for each commit: For example, files, directories, and revisions are referred to by hash values unlike in other traditional version control systems where files or versions are referred to via sequential numbers.

Should I use git pull or fetch?

When comparing Git pull vs fetch, Git fetch is a safer alternative because it pulls in all the commits from your remote but doesn't make any changes to your local files. On the other hand, Git pull is faster as you're performing multiple actions in one – a better bang for your buck.


2 Answers

A git pull does two things for you:

  1. Fetches a specific branch from the repository
  2. Merges it with your current branch.

It sounds like what you want to do is to get a specific revision from the repository and merge it with your current branch.

The best way to do this is two commands:

git fetch origin
git merge YOUR_SHA_HERE

If what you want is just to see what's in a specific revision from the repository and make it the working tree, but not do a merge, then you would want:

git fetch origin
git checkout YOUR_SHA_HERE

If what you want is to get the specific version, and make it the new "master" (or another branch), then you would want to run

git fetch origin
git reset --hard YOUR_SHA_HERE

All of these will fetch new code from the repository (via the 'git fetch origin'), but then there are different ways to combine with / replace your current code.

like image 176
Eric Avatar answered Nov 13 '22 19:11

Eric


I don't think there's a way to pull just part of a branch based on a SHA.

What's wrong with:

git pull
git reset --hard <sha>
like image 26
wadesworld Avatar answered Nov 13 '22 21:11

wadesworld