Skip to content

Commit

Permalink
checkout: implement "-" abbreviation, add docs and tests
Browse files Browse the repository at this point in the history
Have '-' mean the same as '@{-1}', i.e., the last branch we were on.

Signed-off-by: Thomas Rast <trast@student.ethz.ch>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Thomas Rast authored and Junio C Hamano committed Jan 18, 2009
1 parent d18ba22 commit 696acf4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Documentation/git-checkout.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ the conflicted merge in the specified paths.
+
When this parameter names a non-branch (but still a valid commit object),
your HEAD becomes 'detached'.
+
As a special case, the "`@\{-N\}`" syntax for the N-th last branch
checks out the branch (instead of detaching). You may also specify
"`-`" which is synonymous with "`@\{-1\}`".


Detached HEAD
Expand Down
3 changes: 3 additions & 0 deletions builtin-checkout.c
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,9 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
arg = argv[0];
has_dash_dash = (argc > 1) && !strcmp(argv[1], "--");

if (!strcmp(arg, "-"))
arg = "@{-1}";

if (get_sha1(arg, rev)) {
if (has_dash_dash) /* case (1) */
die("invalid reference: %s", arg);
Expand Down
50 changes: 50 additions & 0 deletions t/t2012-checkout-last.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/sh

test_description='checkout can switch to last branch'

. ./test-lib.sh

test_expect_success 'setup' '
echo hello >world &&
git add world &&
git commit -m initial &&
git branch other &&
echo "hello again" >>world &&
git add world &&
git commit -m second
'

test_expect_success '"checkout -" does not work initially' '
test_must_fail git checkout -
'

test_expect_success 'first branch switch' '
git checkout other
'

test_expect_success '"checkout -" switches back' '
git checkout - &&
test "z$(git symbolic-ref HEAD)" = "zrefs/heads/master"
'

test_expect_success '"checkout -" switches forth' '
git checkout - &&
test "z$(git symbolic-ref HEAD)" = "zrefs/heads/other"
'

test_expect_success 'detach HEAD' '
git checkout $(git rev-parse HEAD)
'

test_expect_success '"checkout -" attaches again' '
git checkout - &&
test "z$(git symbolic-ref HEAD)" = "zrefs/heads/other"
'

test_expect_success '"checkout -" detaches again' '
git checkout - &&
test "z$(git rev-parse HEAD)" = "z$(git rev-parse other)" &&
test_must_fail git symbolic-ref HEAD
'

test_done

0 comments on commit 696acf4

Please sign in to comment.