@matheusbsbs / matheus-bagliano / commits / e4b0e4ab8e
Fork 0

detached HEAD -- finishing touches

This updates "git-checkout" to report which branch you are switching to. Especially for people who do not use __git_ps1 from contrib/completion/git-completion.bash this would give a friendlier feedback of what is going on, and should make the reminder message much less scary. Here is a sample session (the prompt tells which branch I am on). * I have some local modification and realize that the change deserves to be on its own new topic branch. [git.git (master)]$ git diff --stat git-checkout.sh | 10 ++++++++-- 1 files changed, 8 insertions(+), 2 deletions(-) * So I switch to a new branch. I get a listing of local modifications and assuring "Switched to a new branch" message. [git.git (master)]$ git checkout -b jc/checkout M git-checkout.sh Switched to a new branch "jc/checkout" * If I switch back to "master", I get essentially the same. [git.git (jc/checkout)]$ git checkout master M git-checkout.sh Switched to branch "master" * Detaching head would say which commit I am at and reminds me that I am not on any branch (not that I would detach my HEAD while keeping precious local changes around in any real-world workflow -- this is just a sample session). [git.git (master)]$ git checkout master^ M git-checkout.sh Note: you are not on any branch and are at commit "master^" If you want to create a new branch from this checkout, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b <new_branch_name> * Coming back to an attached state can lose the detached HEAD, so I get warned and stopped. [git.git]$ git checkout master You are not on any branch and switching to branch 'master' may lose your changes. At this point, you can do one of two things: (1) Decide it is Ok and say 'git checkout -f master'; (2) Start a new branch from the current commit, by saying 'git checkout -b <branch-name>'. Leaving your HEAD detached; not switching to branch 'master'. * Moving around while my HEAD is detached is Ok. I still get the list of local modifications. [git.git]$ git checkout master^0 M git-checkout.sh * The previous step that switched to the tip commit is an obscure but useful trick. My HEAD is still detached but now it is pointed at by an existing ref, so I can come back safely. [git.git]$ git checkout master M git-checkout.sh Switched to branch "master" * And we are back on the "master" branch. [git.git (master)]$ exit Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Feb 1, 2007 at 01:08 UTC e4b0e4ab8ee68df0fa99100640ed5cb54b736141
1 file changed +8 -2
git-checkout.sh
+8 -2
@@ -155,7 +155,7 @@ then
155 detached="$new"
156 if test -n "$oldbranch"
157 then
158 - detach_warn="Note: you are not on ANY branch anymore.
158 + detach_warn="Note: you are not on any branch and are at commit \"$new_name\"
159 If you want to create a new branch from this checkout, you may do so
160 (now or later) by using -b with the checkout command again. Example:
161 git checkout -b <new_branch_name>"
@@ -228,7 +228,7 @@ else
228 saved_err=$?
229 if test "$saved_err" = 0
230 then
231 - test "$new" = "$old" || git diff-index --name-status "$new"
231 + git diff-index --name-status "$new"
232 fi
233 (exit $saved_err)
234 fi
@@ -251,6 +251,12 @@ if [ "$?" -eq 0 ]; then
251 if test -n "$branch"
252 then
253 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
254 + if test -n "$newbranch"
255 + then
256 + echo >&2 "Switched to a new branch \"$branch\""
257 + else
258 + echo >&2 "Switched to branch \"$branch\""
259 + fi
260 elif test -n "$detached"
261 then
262 # NEEDSWORK: we would want a command to detach the HEAD