817

I cloned a git repository of a certain project. Can I turn the files to the initial state and when I review the files go to revision 2, 3, 4 ... most recent? I'd like to have an overview of how the project was evolving.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
xralf
  • 3,312
  • 45
  • 129
  • 200

7 Answers7

1230

Before executing this command keep in mind that it will leave you in detached head status

Use git checkout <sha1> to check out a particular commit.

Where <sha1> is the commit unique number that you can obtain with git log

Here are some options after you are in detached head status:

  • Copy the files or make the changes that you need to a folder outside your git folder, checkout the branch were you need them git checkout <existingBranch> and replace files
  • Create a new local branch git checkout -b <new_branch_name> <sha1>

Note: to "undo" (return from) the detached head state simply use:

git checkout <branch> (where <branch> is e.g. master).

mirekphd
  • 4,799
  • 3
  • 38
  • 59
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • And is there a way I can find out in which revision the project is? I tried to checkout to the first commit with it's sha1 and I'd like to confirm I'm really there and what the sha1 of next commit will be. – xralf Sep 24 '11 at 14:17
  • 2
    You can do this `git log -n1`. But unless `git checkout` failed, it's a waste of effort. – Marcelo Cantos Sep 24 '11 at 23:56
  • 2
    It works. I had to use full sha1 (not partial). And if I want to put the project to second revision? `git log` shows only the first commit now, can I find out the sha1 of the next commit? – xralf Sep 25 '11 at 09:05
  • 7
    You should only have to use enough of the sha1 to guarantee uniqueness. Perhaps you had an unlucky coincidence. Git has no concept of the "next" commit; history is a DAG with all arrows pointing backwards. You should run `git log --oneline` and stick the output into a text file for reference (the abbreviated sha1 sums it provides are guaranteed to be unique). Another option, if your history is linear, is to figure out how many commits there are from the first commit till `master` and use `git checkout master~543` (if there are 543 commits), then `git checkout master~542`, etc. – Marcelo Cantos Sep 25 '11 at 09:27
  • I'm now in the first revision, so when I run `git log --oneline` I can see only one commit, should I clone the repository again? Then run `git log --oneline > commits.txt` and then use file commits.txt as a reference? Isn't it a little cumbersome? – xralf Sep 25 '11 at 10:24
  • No, just `git log --oneline master`. – Marcelo Cantos Sep 25 '11 at 11:35
  • Note: That does not remove directories, so you might see empty version7-related directories even though the files are those of version5. It can be surprising. – Nicolas Raoul Aug 21 '12 at 06:22
  • 34
    and how to checkout back to the current commit from "git checkout "? – Incerteza Jun 28 '15 at 09:33
  • 12
    @AlexanderSupertramp Checkout the branch. – Marcelo Cantos Jun 29 '15 at 10:17
  • 2
    @MarceloCantos i meant to say was, after the checkout, how does one return to the main branch or go back to the commit that they were originally on? I'd assume its `git checkout master` or some other branch depending where you are working? – Charlie Parker Mar 07 '16 at 14:52
  • 2
    Ah, now I see; I misplaced the implied comma. Yes, that's right, git checkout can be used to go to whichever commit you want, by id, tag, branch, or any of a number of other methods to specify a commit. – Marcelo Cantos Mar 07 '16 at 18:52
  • found so many complex responses for this in other threads. Yet this simple statement does it all. ;) Thanks – Prachi Apr 19 '16 at 14:22
  • 1
    You should probably add what to do when *`error: pathspec '866e452bdb859' did not match any file(s) known to git`* is encountered on a valid commit. Its kind of amazing GitHub can find it but local Git cannot. – jww Nov 13 '17 at 04:30
  • 1
    what is `` here? – manas May 17 '18 at 04:07
  • 2
    After doing the checkout you may also wish to go back to the latest as described here: https://stackoverflow.com/questions/5772192/how-can-i-reconcile-detached-head-with-master-origin – Den-Jason Aug 28 '19 at 11:20
  • 1
    @Den-Jason there's a lot of content on that question. What exactly are you referring to? In any event, going back to the latest is as simple as `git checkout -`. – Marcelo Cantos Aug 31 '19 at 09:46
  • @MarceloCantos it's the answer from Daniel Alexiuc. The `git rev-parse` stuff provided by Chris Johnsen was useful too. – Den-Jason Sep 02 '19 at 09:57
  • Does this mean that it will checkout the branch where that sha was originally committed and also bring the state of the checked out branch to that particular commit? – jxramos Oct 18 '19 at 17:08
  • 12
    `git checkout -b ` : to check out the commit to a branch. – Loganathan Jul 16 '20 at 13:50
  • If the git command isn't "seeing" that commit ( shat1 ) that you are trying to checkout, ie: pushed by someone else up to GitHub, you may need to do a `git fetch ` to update your local revisions. Also, once its checked out you may want to do a `git checkout -b ` to create a working/feature branch from it. – G-Man Oct 07 '20 at 14:35
  • fyi sometimes the files are not fully updated for some mysterious reason. I found out that going to the root of the git project and running `git checkout .` fixed that issues. – Charlie Parker Sep 16 '22 at 20:29
  • This saved my day – Vagabond Aug 31 '23 at 11:05
81

To go to a particular version/commit run following commands. HASH-CODE you can get from git log --oneline -n 10

git reset --hard HASH-CODE

Note - After reset to particular version/commit you can run git pull --rebase, if you want to bring back all the commits which are discarded.

J4cK
  • 30,459
  • 8
  • 42
  • 54
  • 1
    Note that a `reset` doesn't just checkout a particular point in the graph it will also move your currently checked out branch – Liam Jul 03 '19 at 10:57
  • Also with `reset`, all your pending changes are discarded. – WilliamKF Sep 13 '19 at 22:35
  • 6
    --hard flag will delete any commits after said hash.... probably want to add that little tid bit here. I'm sure people have lost history and wondered why. – Urasquirrel Feb 13 '20 at 20:01
  • 1
    `git pull --rebase` works only if you have a remote for your repo *and* it's up to date. – Keith Thompson Feb 14 '20 at 18:48
  • 1
    fyi sometimes the files are not fully updated for some mysterious reason. I found out that going to the root of the git project and running `git checkout .` fixed that issues. – Charlie Parker Sep 16 '22 at 20:29
23

You can get a graphical view of the project history with tools like gitk. Just run:

gitk --all

If you want to checkout a specific branch:

git checkout <branch name>

For a specific commit, use the SHA1 hash instead of the branch name. (See Treeishes in the Git Community Book, which is a good read, to see other options for navigating your tree.)

git log has a whole set of options to display detailed or summary history too.

I don't know of an easy way to move forward in a commit history. Projects with a linear history are probably not all that common. The idea of a "revision" like you'd have with SVN or CVS doesn't map all that well in Git.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • 3
    Be aware: git will not lie to you by giving you a single linear history of the project. That is unless the project actually evolved that way. – Andres Jaan Tack Sep 24 '11 at 12:57
  • Moving forward is logically meaningless (even in a linear history), since a commit makes no reference to the "future". At best, you can identify all commits that have the commit in question as a parent. Mind you, moving backward isn't a trivial exercise either, due to merges. – Marcelo Cantos Sep 25 '11 at 00:01
  • @MarceloCantos Well, that's not entirely true. `git log -p -m --first-parent --reverse` will do a really good job of showing you a linear and accurate mainline history of changes since the beginning, with changes from merged history shown summarized in a single diff. – jthill Oct 23 '20 at 18:02
6

Using a commit's SHA1 key, you could do the following:

  • First, find the commit you want for a specific file:

    git log -n <# commits> <file-name>

    This, based on your <# commits>, will generate a list of commits for a specific file.

    TIP: if you aren't sure what commit you are looking for, a good way to find out is using the following command: git diff <commit-SHA1>..HEAD <file-name>. This command will show the difference between the current version of a commit, and a previous version of a commit for a specific file.

    NOTE: a commit's SHA1 key is formatted in the git log -n's list as:

commit <SHA1 id>

  • Second, checkout the desired version:

    If you have found the desired commit/version you want, simply use the command: git checkout <desired-SHA1> <file-name>

    This will place the version of the file you specified in the staging area. To take it out of the staging area simply use the command: reset HEAD <file-name>

To revert back to where the remote repository is pointed to, simply use the command: git checkout HEAD <file-name>

Wizard
  • 69
  • 1
  • 3
4

To get to a specific committed code, you need the hash code of that commit. You can get that hash code in two ways:

  1. Get it from your github/gitlab/bitbucket account. (It's on your commit url, i.e: github.com/user/my_project/commit/commit_hash_code), or you can
  2. git log and check your recent commits on that branch. It will show you the hash code of your commit and the message you leaved while you were committing your code. Just copy and then do git checkout commit_hash_code

After moving to that code, if you want to work on it and make changes, you should make another branch with git checkout -b <new-branch-name>, otherwise, the changes will not be retained.

ihojmanb
  • 452
  • 8
  • 17
2

I was in a situation where we have a master branch, and then another branch called 17.0 and inside this 17.0 there was a commit hash no say "XYZ". And customer is given a build till that XYZ revision. Now we came across a bug and that needs to be solved for that customer. So we need to create separate branch for that customer till that "xyz" hash. So here is how I did it.

First I created a folder with that customer name on my local machine. Say customer name is "AAA" once that folder is created issue following command inside this folder:

  1. git init
  2. git clone After this command you will be on master branch. So switch to desired branch
  3. git checkout 17.0 This will bring you to the branch where your commit is present
  4. git checkout This will take your repository till that hash commit. See the name of ur branch it got changed to that commit hash no. Now give a branch name to this hash
  5. git branch ABC This will create a new branch on your local machine.
  6. git checkout ABC
  7. git push origin ABC This will push this branch to remote repository and create a branch on git server. You are done.
ashish
  • 99
  • 1
  • 7
1

One way would be to create all commits ever made to patches. checkout the initial commit and then apply the patches in order after reading.

use git format-patch <initial revision> and then git checkout <initial revision>. you should get a pile of files in your director starting with four digits which are the patches.

when you are done reading your revision just do git apply <filename> which should look like git apply 0001-* and count.

But I really wonder why you wouldn't just want to read the patches itself instead? Please post this in your comments because I'm curious.

the git manual also gives me this:

git show next~10:Documentation/README

Shows the contents of the file Documentation/README as they were current in the 10th last commit of the branch next.

you could also have a look at git blame filename which gives you a listing where each line is associated with a commit hash + author.

Alexander Oh
  • 24,223
  • 14
  • 73
  • 76