How to git checkout a branch a collaborator created on GitHub?

I pulled the branch “Version1” that my teammate created.

$ git pull
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 847 bytes | 282.00 KiB/s, done.
   b27e488..aa3e2f9  Version1   -> origin/Version1
Already up to date.

How do I check it out, so I can contribute to the branch?
I tried this.

$ git checkout origin/Version1
Note: switching to 'origin/Version1'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at aa3e2f9 committing V2 of code

I do not understand what’s with the “detached HEAD” state.
I tried staging a change and pushing it like this:

$ git commit -m "Test add foo.txt"
[detached HEAD 397d423] Test add foo.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 foo.txt
aryan ((397d423...)) mixed-messages
$ git push
fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
state now, use

    git push origin HEAD:<name-of-remote-branch>

I do not understand why it says that I am not on a branch. Could anyone please help?

You’re trying to work in the remote branch origin/Version1 whereas you want an actual branch. As the warning states you can create a new branch if you want to retain any commits you make (basically you are still on your old branch).

Unless you have a very complex remote set-up you can just specify the branch name when switching to remotely tracked branches. The following would create a new local branch for you to work on (with the correct upstream tracking)-

git switch Version1
1 Like