Go to particular revision
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>
How do I check out a particular version in Git from 'git log'?
You can checkout a commit using git checkout sha-of-commit
which you already have.
But you cannot commit anything (as you're not in a branch, you're in a static commit).
If you need to commit anything on top of that commit, you need to check it out into a branch using git checkout sha-of-commit -b testing-a-commit
.
git log <file>
only shows commits that affect that file.
git log of a single revision
You can use show
:
git show commit_id
How to retrieve a single file from a specific revision in Git?
Using git show
To complete your own answer, the syntax is indeed
git show object
git show $REV:$FILE
git show somebranch:from/the/root/myfile.txt
git show HEAD^^^:test/test.py
The command takes the usual style of revision, meaning you can use any of the following:
- branch name (as suggested by ash)
HEAD
+ x number of^
characters- The SHA1 hash of a given revision
- The first few (maybe 5) characters of a given SHA1 hash
Tip It's important to remember that when using "git show
", always specify a path from the root of the repository, not your current directory position.
(Although Mike Morearty mentions that, at least with git 1.7.5.4, you can specify a relative path by putting "./
" at the beginning of the path. For example:
git show HEAD^^:./test.py
)
Using git restore
With Git 2.23+ (August 2019), you can also use git restore
which replaces the confusing git checkout
command
git restore -s <SHA1> -- afile
git restore -s somebranch -- afile
That would restore on the working tree only the file as present in the "source" (-s
) commit SHA1 or branch somebranch
.
To restore also the index:
git restore -s <SHA1> -SW -- afile
(-SW
: short for --staged --worktree
)
As noted in the comments by starwarswii
It lets you pipe the contents into a file, which is great if you want to just quickly compare files from a commit.
E.g. you can do:
git show 1234:path/to/file.txt > new.txt
git show 1234~:path/to/file.txt > old.txtthen compare them.
Using low-level git plumbing commands
Before git1.5.x, this was done with some plumbing:
git ls-tree <rev>
show a list of one or more 'blob' objects within a commit
git cat-file blob <file-SHA1>
cat a file as it has been committed within a specific revision (similar to svn
cat).
use git ls-tree
to retrieve the value of a given file-sha1
git cat-file -p $(git-ls-tree $REV $file | cut -d " " -f 3 | cut -f 1)::
git-ls-tree
lists the object ID for $file
in revision $REV
, this is cut out of the output and used as an argument to git-cat-file
, which should really be called git-cat-object
, and simply dumps that object to stdout
.
Note: since Git 2.11 (Q4 2016), you can apply a content filter to the git cat-file
output.
See
commit 3214594,
commit 7bcf341 (09 Sep 2016),
commit 7bcf341 (09 Sep 2016), and
commit b9e62f6,
commit 16dcc29 (24 Aug 2016) by Johannes Schindelin (dscho
).
(Merged by Junio C Hamano -- gitster
-- in commit 7889ed2, 21 Sep 2016)
git config diff.txt.textconv "tr A-Za-z N-ZA-Mn-za-m <"
git cat-file --textconv --batch
Note: "git cat-file --textconv
" started segfaulting recently (2017), which has been corrected in Git 2.15 (Q4 2017)
See commit cc0ea7c (21 Sep 2017) by Jeff King (peff
).
(Merged by Junio C Hamano -- gitster
-- in commit bfbc2fc, 28 Sep 2017)
How do I check out a certain revision with Git?
I think you are looking for git archive
. Maybe something like this:
$ git archive --format=tar --prefix=my-tag-or-commit-id/ my-tag-or-commit-id | (cd /path/to/repo/checkout && tar xf -)
Check git help archive
for other options and examples. You can use remote branch names for as well.
I hope this helps.
How do I get a particular (older) version of a file from git repository without wiping out later versions?
When you just use git checkout <somehash>
, then you will get into a detached HEAD. This happens because git checkout
by default just switches branches, and specifying a commit instead of a branch gets you into a “branchless mode” (that’s what detached HEAD means).
If you want to get a single file from a different version, you also need to specify the path, i.e. git checkout <somehash> -- path/to/file
. This will keep everything as it is and just replace the file at path/to/file
with its version from <somehash>
.
So this does overwrite the file. If you want to keep the current version as well, you should rename it first. Otherwise, you can also use a special syntax of git show
for this
git show <somehash>:path/to/file > new_filename.ext
This will essentially get the contents of the file from <somehash>
and print it, so if you pipe it to file, you can extract a file from a different version into a different filename.
How can I see the changes in a Git commit?
To see the diff for a particular COMMIT
hash, where COMMIT
is the hash of the commit:
git diff COMMIT~ COMMIT
will show you the difference between that COMMIT
's ancestor and the COMMIT
. See the man pages for git diff for details about the command and gitrevisions about the ~
notation and its friends.
Alternatively, git show COMMIT
will do something very similar. (The commit's data, including its diff - but not for merge commits.) See the git show manpage.
(also git diff COMMIT
will show you the difference between that COMMIT
and the head.)
how to checkout a specific commit from git
The question has wrong terminology. You cannot pull a commit, you can only checkout a commit. Pulling a commit would defy the whole branch/commit structure saving memory. You pull all commits in a branch or repository, and if you want to check out a specific one, then well, check it out:
git checkout 9ce920d
You will be in headless mode (no pointer to the commit, even if you have branches pointing to them - you have to check them out explicitly!), so you may want to create a branch if you want to mess around:
git checkout -B mess_around_branch
For posterity, OP actually wanted to discard all commits from the latest commit on their branch to a specific commit before. To do this from your branch hit:
git reset 9ce920d
which will discard all commits, but leave the files as they were, allowing you to decide if you wish to retain some of the changes. If you really want to lose everything:
git reset --hard 9ce920d
Related Topics
Getting Following Error After The Command Sudo Apt-Get Update on Ubuntu 16.04
How to Programmatically Invert Screen Colors in Linux
Implementing Basic File System
What Is The Fastest Way to Display an Image in Qt on X11 Without Opengl
Convert Column to Matrix Format Using Awk
Check If All Lines from One File Are Present Somewhere in Another File
Securing Udp - Openssl or Gnutls or ...
Linux: How to Debug a Sigsegv? How to Trace The Error Source
Why Processes Are Deprived of CPU for Too Long While Busy Looping in Linux Kernel
Docker Run Hello-World Still Fails, Permission Denied
How to Pass Env Variables Between Make Targets
Principle of Qemu CPU Emulation
How to Disable Serial Console(Non-Kernel) in U-Boot