Questions tagged [git-checkout]

Checkout a branch or file to the current working tree in a Git repo.

git checkout is used to update files in the current working tree to a specific version in the index or specified branch.

To restore a file to the version in the index:

git checkout -- <filename>

Popular questions

See also

References

$ git checkout --help
1090 questions
8468
votes
43 answers

How do I check out a remote Git branch?

Somebody pushed a branch called test with git push origin test to a shared repository. I can see the branch with git branch -r. How do I check out the remote test branch? I've tried: git checkout test, which does nothing git checkout origin/test…
Juri Glass
  • 88,173
  • 8
  • 33
  • 46
7611
votes
41 answers

How do I revert a Git repository to a previous commit?

How do I revert from my current state to a snapshot made on a certain commit? If I do git log, then I get the following output: $ git log commit a867b4af366350be2e7c21b8de9cc6504678a61b` Author: Me Date: Thu Nov 4 18:59:41 2010…
Crazy Serb
  • 76,330
  • 8
  • 35
  • 47
5639
votes
36 answers

How do I reset or revert a file to a specific revision?

How do I revert a modified file to its previous revision at a specific commit hash (which I determined via git log and git diff)?
Hates_
  • 66,613
  • 6
  • 32
  • 37
3176
votes
30 answers

How do I find and restore a deleted file in a Git repository?

Say I'm in a Git repository. I delete a file and commit that change. I continue working and make some more commits. Then, I discover that I need to restore that file after deleting it. I know I can checkout a file using git checkout --…
avdgaag
  • 41,292
  • 7
  • 29
  • 26
2552
votes
19 answers

How do I revert all local changes in Git managed project to previous state?

I ran git status which told me everything was up to date and there were no local changes. Then I made several consecutive changes and realized I wanted to throw everything away and get back to my original state. Will this command do it for me? git…
Jacques René Mesrine
  • 46,127
  • 27
  • 66
  • 104
1963
votes
14 answers

How to get just one file from another branch

I have a main branch with a file called app.js. I made changes to this file on an experiment branch. I want to apply only the changes made to app.js from experiment onto the main branch.
Nick Vanderbilt
  • 36,724
  • 29
  • 83
  • 106
942
votes
9 answers

Is there any way to git checkout previous branch?

I sort of want the equivalent of cd - for git. If I am in branch master and I checkout foo, I would love to be able to type something like git checkout - to go back to master, and be able to type it again to return to foo. Does anything like this…
Matt Briggs
  • 41,224
  • 16
  • 95
  • 126
935
votes
13 answers

Rollback to an old Git commit in a public repo

How can I go about rolling back to a specific commit in git? The best answer someone could give me was to use git revert X times until I reach the desired commit. So let's say I want to revert back to a commit that's 20 commits old, I'd have to…
David
  • 9,799
  • 5
  • 28
  • 32
844
votes
20 answers

Merge, update, and pull Git branches without using checkouts

I work on a project that has 2 branches, A and B. I typically work on branch A, and merge stuff from branch B. For the merging, I would typically do: git merge origin/branchB However, I would also like to keep a local copy of branch B, as I may…
charles
  • 11,212
  • 3
  • 31
  • 46
790
votes
6 answers

What is git tag, How to create tags & How to checkout git remote tag(s)

when I checkout remote git tag use command like this: git checkout -b local_branch_name origin/remote_tag_name I got error like this: error: pathspec origin/remote_tag_name did not match any file(s) known to git. I can find remote_tag_name when I…
Ryanqy
  • 8,476
  • 4
  • 17
  • 27
782
votes
8 answers

What's the difference between git switch and git checkout

Git 2.23 introduces a new command git switch -- after reading the docs, it seems pretty much the same as git checkout can someone explain the difference or use case? Two new commands "git switch" and "git restore" are introduced to …
Bastian Venthur
  • 12,515
  • 5
  • 44
  • 78
681
votes
25 answers

How can I get the latest tag name in current branch in Git?

What's the simplest way to get the most recent tag in Git? git tag a HEAD git tag b HEAD^^ git tag c HEAD^ git tag output: a b c Should I write a script to get each tag's datetime and compare them?
culebrón
  • 34,265
  • 20
  • 72
  • 110
644
votes
8 answers

Unstage a deleted file in git

Usually, to discard changes to a file you would do: git checkout -- What if the change I want to discard is deleting the file? The above line would give an error: error: pathspec '' did not match any file(s) known to git. What command…
lurscher
  • 25,930
  • 29
  • 122
  • 185
590
votes
27 answers

How to sparsely checkout only one single file from a git repository?

How do I checkout just one file from a git repo?
Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
587
votes
5 answers

What are the differences between double-dot ".." and triple-dot "..." in Git commit ranges?

Some Git commands take commit ranges and one valid syntax is to separate two commit names with two dots .., and another syntax uses three dots .... What are the differences between the two?
Pat Notz
  • 208,672
  • 30
  • 90
  • 92
1
2 3
72 73