xralf
xralf

Reputation: 3772

Go to a particular revision

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.

Upvotes: 890

Views: 1158744

Answers (7)

ihojmanb
ihojmanb

Reputation: 482

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, or 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 left 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.

Upvotes: 6

ashish
ashish

Reputation: 99

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 number, say "XYZ". And the 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 a separate branch for that customer till that "xyz" hash value. So here is how I did it.

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

  1. git init
  2. git clone After this command, you will be on the master branch. So switch to the 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 your branch. It got changed to that commit hash number. Now give a branch name to this hash value
  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 the remote repository and create a branch on the Git server. You are done.

Upvotes: 2

Nivir
Nivir

Reputation: 31218

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

git reset --hard HASH-CODE

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

Upvotes: 95

Wizard
Wizard

Reputation: 79

Using a commit's SHA-1 key/hash value, 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 SHA-1 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>

Upvotes: 7

Alexander Oh
Alexander Oh

Reputation: 25661

One way would be to create all commits ever made to patches. Check out 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 directory, 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.

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.

Upvotes: 1

Mat
Mat

Reputation: 206909

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 SHA-1 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.

Upvotes: 23

Marcelo Cantos
Marcelo Cantos

Reputation: 186108

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

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

Where <sha1> is the commit unique number (SHA-1 hash value) that you can obtain with git log.

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

  • Copy the files or make the changes that you need to a folder outside your Git folder, check out the branch where 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).

Upvotes: 1325

Related Questions