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
27
votes
6 answers

What's the difference among "git fetch && git checkout" versus "git checkout" only?

Should do we always do as: git fetch && git checkout Or only, git checkout ? For example when doing a checkout from a branch in bitbucket they provide the command as: git fetch && git checkout develop But why is this necessary if git…
mario ruiz
  • 880
  • 1
  • 11
  • 28
27
votes
4 answers

undo git pull of wrong branch onto master

I have pulled from a different branch from the remote. The 2 branches are different yet the merge happened silently on my current branch. I am currently working on the "master" branch locally, and it has been updated with the changes of the remote…
OverTheEdge
  • 749
  • 1
  • 11
  • 17
25
votes
2 answers

Create new branch based on current branch to work on a new feature

How do I create a new branch in git to begin work on a new feature? I want the new branch to be a duplicate of the current branch (ie, the new branch's HEAD should be the same as the current HEAD). Question differentiation: Create a branch in Git…
Tom Hale
  • 40,825
  • 36
  • 187
  • 242
25
votes
2 answers

Why does git checkout with explicit refs/heads/branch give detached HEAD?

If I checkout a branch using just the branch name, HEAD is updated to point at that branch. $ git checkout branch Switched to branch 'branch' If I checkout a branch by using refs/heads/branch or heads/branch, HEAD becomes detached. $ git checkout…
darthbith
  • 18,484
  • 9
  • 60
  • 76
24
votes
2 answers

Why does `git checkout ` stage the change?

If I start from a clean working tree and run git checkout , where has a different version of this file, I end up with a staged rather than an unstaged change. What's the reason for this? Is this just for consistency with…
Luke
  • 7,110
  • 6
  • 45
  • 74
24
votes
2 answers

Problems with corrupt git repo

My git repo got corrupted while running a find and replace command (See here: Git reset failing after find and replace). So I deleted some pack files about which git told me that there were not reachable. So I ran rm ./.git/index, git reset. When…
epsilones
  • 11,279
  • 21
  • 61
  • 85
24
votes
11 answers

git checkout -- doesn't discard changes?

I have changes in my working directory that I'm trying to discard (reset to the current indexed version of the files), however, git checkout -- will not discard the changes. I've attempted to manually remove the files (rm -r files) then run…
Highway of Life
  • 22,803
  • 16
  • 52
  • 80
23
votes
2 answers

What does "fatal: You are on a branch yet to be born" mean in this context

I was migrating a svn repo to git. I created a clean temp dir, used git svn init to get stuff in there and then added the remote and pushed it. Now on the other actual source dir (which I had cloned from the empty repo before committing stuff from…
Osada Lakmal
  • 891
  • 2
  • 8
  • 22
23
votes
4 answers

git checkout master does not switch branch - repository broken?

tl;dr: git checkout master does not switch to master branch, gives no error or any output at all and I have no clue why. Any other branch works fine. I have a git repository that consists of the branches development and master. I do a fresh clone of…
schrom
  • 1,372
  • 1
  • 22
  • 36
23
votes
2 answers

Git reset and checkout by single command

Consider I've staged for committing a file (e.g. db/schema.rb) that I didn't intended to change. I need to do: git reset db/schema.rb git checkout db/schema.rb Can I do it by single command?
Alexey
  • 9,197
  • 5
  • 64
  • 76
23
votes
3 answers

How to git fetch and checkout without creating a remote branch locally?

Use Case: I have some repo on GitHub, someone forked it and added new feature and initiated pull request. I can't automatically merge it because there are some minor problems I would like to fix first. It's one-time activity I'll never need this…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
22
votes
5 answers

Why is the git command to switch branches named "git checkout"?

Why is the git command to switch branches named git checkout? Does it really make sense ? I would name it git switch instead. Besides, git checkout has other meanings: e.g. reverting a file (like svn revert)
Michael
  • 10,185
  • 12
  • 59
  • 110
22
votes
4 answers

GIT checkout except one folder

I want to checkout to other branch, or previous commit, but I want to keep one folder the same files (not checkout the folder). I want that the folder, will be shown in git status, so I can add this folder now to the index. For example, I have a…
Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117
22
votes
3 answers

Why does Git tell me "Not currently on any branch" after I run "git checkout origin/"?

I was trying to follow the instructions from Git: "Not currently on any branch." Is there an easy way to get back on a branch, while keeping the changes? but git checkout appears to be broken: $ git checkout origin/web-zach HEAD is now at 1366cb1...…
hubatish
  • 5,070
  • 6
  • 35
  • 47
21
votes
1 answer

git: replace folder with the same folder on a different branch

I tried git checkout OTHER_BRANCH -- /path/to/folder the problem is with files that exist on current branch, but do not exist on the other branch. I want to delete these.
Jacko
  • 12,665
  • 18
  • 75
  • 126