2

I am working on a GitHub project and I am trying to switch from master to another user's branch. Their branch is ahead of master and has multiple changes. No matter what I try, I cannot switch to their branch and get their changes.

I have tried numerous gitbash commands including checkout, pull, clone, fetch.

git clone link git checkout branchName git fetch branchName git fetch 

I expected "git checkout branchName" to switch to the other user's branch, but it just switches to a brand new local branch with the same name. "git fetch branchName" just returns an error.

I have been using git for years and I feel like an idiot that I cannot figure this out. I also cannot find the solution anywhere online.

5
  • There is no such thing as "some other user's branch". Your repository is yours, and all your branches are yours. Your Git will remember some other Git's branches via your remote-tracking names (e.g., origin/theirbranch) and you can make your own branch, which you can name theirbranch if you like but it's still your branch, that points to the same commit as origin/theirbranch. Git is really about commits though: the branch names only exist to remember a commit. So don't worry too much about the names. Commented Apr 9, 2019 at 17:15
  • 1
    As EncryptedWatermelon suggests, you can check out a commit by something other than a branch name, e.g., by the remote-tracking name origin/theirbranch. Git calls this state a detached HEAD. You're on the commit, rather than being on any branch. You're on no branch. That's fine! Git just won't be able to remember new commits you make, because branch names are how Git remembers commits. Commented Apr 9, 2019 at 17:17
  • (This isn't a completely accurate picture—for that, we have to go into Git's concept of a reference, of which branch names are just one kind—but it should get the general idea across: that Git is really about commits.) Commented Apr 9, 2019 at 17:18
  • @torek you are just wrong. This is a project that is owned by another user, and there are 6 people actively working on it. You just made a condescending response that was completely missing the point of my question. Commented Apr 9, 2019 at 17:20
  • 1
    It's not meant to be condescending, and it's literally true: once you clone a repository, that repository is yours, to do with as you will. Your Git's names are all yours. The remote-tracking names—origin/foo, upstream/bar—are also yours, they're just slaved from some other, independent Git repository. Running git fetch to that particular remote updates your remote-tracking names, while also obtaining any commits they have that you don't. Commented Apr 9, 2019 at 17:23

2 Answers 2

2

I figured it out. None of the answers seemed to understand my situation so I will reiterate. I am working on a project with multiple other users. They all have their own branches with commits on each, all stemming from master. I want to switch to another user's branch and then help work on their current branch.

To do this I needed to do the command:

git fetch origin branchName:branchName 
Sign up to request clarification or add additional context in comments.

Comments

1

git checkout <other_users_remote>/branchName or git checkout -b branchName <other_users_remote>/branchName to create a tracking branch

Did you add the other person's remote? git remote -v

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.