Skip to main content
34 events
when toggle format what by license comment
Mar 10, 2022 at 17:24 history edited VonC CC BY-SA 4.0
add Git 2.36
Jan 18, 2022 at 15:58 comment added Mark Ransom @musiphil using the name of a branch is not a guarantee that you won't get a detached head. The word "your" in the first sentence of this answer needs more emphasis. I kept getting a detached head when checking out a remote branch, never using a hash at all. I'm still not sure I'll be able to avoid it next time I try.
Jun 17, 2021 at 15:11 comment added VonC @AaronFranke To reset a branch to another: git switch -C master upstream/master: you force the recreation of master, starting from upstream/master
Jun 17, 2021 at 13:19 comment added Aaron Franke But I already have a local branch with the same name (there's local master, origin/master, upstream/master, and local has origin set as the remote). I want to reset my local master to upstream's master.
Jun 17, 2021 at 7:31 comment added VonC @AaronFranke You create a new local branch: git switch -c abranch: if origin/abranch exists, it will be checked out and tracked automatically for you.
Jun 16, 2021 at 23:05 comment added Aaron Franke How do you check out a remote branch without detaching? git switch doesn't allow this, and git checkout leads to a detached head.
Feb 9, 2021 at 13:42 comment added VonC @Honey Sure. I have edited the answer to add an example where I do checkout a remote branch... and end up with a detached HEAD.
Feb 9, 2021 at 13:41 history edited VonC CC BY-SA 4.0
add checkout vs. switch
Feb 9, 2021 at 13:22 comment added mfaani Thanks, I'm still confused a bit about what sequence of git commands result in 'if you checkout a "remote branch" without tracking it first, you can end up with a detached HEAD' Can you share that please?
Feb 9, 2021 at 13:14 comment added VonC @Honey That is expected, because the guess mode is the default: git-scm.com/docs/git-switch#Documentation/… (see also the last part of stackoverflow.com/a/54058395/6309). Any branch name which exists in the remote namespace means a git switch/git checkout would create a local branch tracking the remote one. Detached HEAD means you switch to a commit SHA1, not a branch name.
Feb 9, 2021 at 13:08 comment added mfaani What are the steps to get into a detached head when doing checking out a remote branch? Because when I do 1. git fetch 2. git checkout <name-of-remote-branch> I successfully create a new local branch, like git just knows what I intended as discussed here
May 16, 2020 at 9:19 comment added VonC @timgeb Example: simonberner.rocks/2019/10/01/Jenkins-In-Detached-HEAD , devcenter.bitrise.io/jp/faq/…, thenewstack.io/…
May 16, 2020 at 9:09 comment added timgeb Thanks. I need to think about that a little. I can see why checking out a branch can and should give different results depending on where that branch points to at the moment. I'm not quite convinced yet that detached head mode makes sense if you check out a commit that happens to be a branch.
May 16, 2020 at 9:06 comment added VonC @timgeb Simple: when you checkout "aBranch", you don't know what you get. If you did any pull, that branch SHA1 could have changed. When you check out a SHA1, you know precisely what you get, a fixed content which won't change in time. A branch can be updated, renamed, deleted, in short transient. A commit is immutable. A CI/CD tool usually works with SHA1, especially when you need to replay an old build which has failed.
May 16, 2020 at 9:01 comment added timgeb Thanks. What are those use cases you speak of? Do you have a real world example where I would want to do something different with checkout <branch X> and checkout <hash branch X points to>?
May 16, 2020 at 8:58 comment added VonC @timgeb The idea is to differentiate a checkout of a SHA1 (referenced or not by one of the files in .git/refs/heads from a checkout of a branch (referencing in turn a commit as its HEAD). Those are two different use cases.
May 16, 2020 at 7:49 comment added timgeb Sure, but it would be trivial to check whether the checked out commit is referenced by one of the files in .git/refs/heads. So with my limited knowledge not doing this still seems odd.
May 15, 2020 at 19:31 comment added VonC @timgeb The commit itself has no idea it resents the tip of a branch. Only the branch itself (in .git/refs/heads/<aBranchName>) knows which commit represents its HEAD.
May 15, 2020 at 8:42 comment added timgeb "A SHA1 which represents the tip of a branch still gives a detached HEAD." I wonder what's the reason behind this design.
Sep 26, 2019 at 20:29 history edited VonC CC BY-SA 4.0
fix switch
Sep 26, 2019 at 12:57 history edited VonC CC BY-SA 4.0
add switch
Sep 26, 2019 at 12:36 history edited Johannes Overmann CC BY-SA 4.0
typo
Mar 18, 2019 at 22:37 comment added VonC @Honey Not sure: asking that as a new question (with more details, like Git version and OS, and shell used when checking out the branch) should help. In particular, the output of git branch -avv is important.
Mar 18, 2019 at 19:24 comment added mfaani I did git checkout origin/552 and got Note: checking out 'origin/552 You are in 'detached HEAD' state... Then a few days later I did such and didn't get that error. Can you explain why? (FYI I haven't downloaded that branch itself locally...)
May 23, 2017 at 10:31 history edited URL Rewriter Bot
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Mar 5, 2016 at 8:36 comment added Adriano "You can think of this as an anonymous branch" :) I like the analogy
May 30, 2014 at 5:13 history edited user456814 CC BY-SA 3.0
Added another newline for readability.
Feb 13, 2014 at 8:29 history edited VonC CC BY-SA 3.0
add sha1
Feb 13, 2014 at 8:26 comment added musiphil In fact, you will get a detached HEAD whenever you checkout any commit by its SHA1, whether or not it's at the tip of a branch; the only kind of thing you can checkout without getting a detached HEAD is a branch name. For example, even though master is at ed489 on the diagram above, git checkout ed489 will give you a detached HEAD, while git checkout master will not.
Sep 3, 2013 at 5:44 comment added VonC @max: this extract you mention is for a git commit -a: the stage isn't so much 'cleared' as 'made identical to the new commit', which means any new modification will be detected in a git diff, because git diff compares the working tree to the index. So that is why the git commit man page describes the -a option as "Tell the command to automatically stage files that have been modified and deleted"
Sep 3, 2013 at 1:46 comment added max In this visual guide, there's this explanation: git commit files creates a new commit containing the contents of the latest commit, plus a snapshot of files taken from the working directory. Additionally, files are copied to the stage. What does it mean by "files are copied to the stage"? I thought the files are committed, which means the stage is cleared?
Jul 9, 2013 at 3:48 comment added user456814 Another way you can enter detached head state is if you're in the middle of an interactive rebase, and you want to edit one of the commits. When Git drops you at the commit to edit, you'll be in a detached head state until you finish the rebase.
Sep 22, 2011 at 1:57 vote accept Adam Bergmark
Oct 19, 2010 at 6:04 history answered VonC CC BY-SA 2.5