Git: Correct way to change Active Branch in a bare repository?

git bare repository
git create branch in bare repository
git push to bare repository
git clone --bare
git change default branch bare repository
git checkout empty branch
git convert bare to normal
git cannot use bare repository

I have a bare repository that's used as the central store for my project. All the developers do git clone <repo> to share with it. When they do the clone, they get a checkout of the master branch (unless they do git clone -n) because repo.git/HEAD contains ref: refs/heads/master, making this the Active Branch.

The question is, how do I change the Active Branch properly? I could simply hack the repo.git/HEAD file directly, but that seems nasty and, well, hacky.

I tried doing git checkout <otherbranch> in the repo .git directory, but that failed because I wasn't in a work tree.

I tried git update-ref HEAD refs/heads/otherbranch but that just updated refs/heads/master to be the same as refs/heads/otherbranch (okay, I did that one in a dummy repository, not my production one!)

I tried git update-ref --no-deref HEAD refs/heads/otherbranch and that almost worked. It updated the HEAD file, but it set it to the SHA1 of the commit pointed to by refs/heads/otherbranch.

I'm testing with git version

I'm guessing there's no way to do this through git push, as allowing all and sundry to change your default branch seems a bit unsafe (!), but surely there's a better way to do it in the repo .git directory than directly hacking the HEAD file.

Switch current branch in git bare repository, Git: Correct way to change Active Branch in a bare repository? Try this instead of git checkout : git symbolic-ref HEAD refs/heads/develop. Then you should be� If you have direct access to the repo, you can just open up a shell [in the bare repo] directory and use good old git branch to see what branch origin is currently on. To change it to another branch, you have to use git symbolic-ref HEAD refs/heads/another-branch.

To change the branch you need to change HEAD reference to the branch you want to use.

First list all the references in the bare repository by doing

$find ref

Then find the reference for your branch, the format will be as follows refs/heads/<my_branch>. So next step is to check current reference, just type:

$git symbolic-ref HEAD

so you know which is the current branch then update it as needed.

$git sumbolic-ref HEAD ref/heads/<my_branch>

Thant's it. Enjoy.

Setting the default git branch in a bare repository, A bare repository in git does not include a checkout. So in order to set the default branch that users will get after they clone, you cannot use� Types of git bare repos. There are two ways to create a bare git repo: Clone an existing repository with the git clone –bare switch; Create a new bare git repo with the git init –bare switch; Obviously, the bare git init approach is used when you are starting a brand new project.

Bare Repositories in Git, A non-bare or default git repository has a .git folder, which is the It stores the hashes of commits made in the branches and a file where Since you cannot commit, or make changes to it, bare repositories are pretty useless on their own. need not be bare, hence not many people know about it properly. git clone [url]: Clone (download) a repository that already exists on GitHub, including all of the files, branches, and commits. git status : Always a good idea, this command shows you what branch you're on, what files are in the working or staging directory, and any other important information.

Also, if you don't have access to the bare repository, by doing a git remote set-head and you are done

See this previous response

Branches in a Nutshell, The way Git branches is incredibly lightweight, making branching operations you a powerful and unique tool and can entirely change the way that you develop . every repository has one is that the git init command creates it by default and You can see the master and testing branches that are right there next to the� UPDATE: There is Good analysis of the whole main branch convo in the Git Rev News: Edition 65. Git likely uses master in the context of "master copy" or "master recording." So we see that while the word master doesn't always connote slave, for many, it's evocative via basic word-association and they just don't want to look at the word on their

I also have a bare repo on our server and was able to successfully retrieve files using

git clone //server/repo/directory -b branch_name

into a new local repository even though the manpage says this is only for non-bare repositories.

user-manual Documentation, The best way to get one is by using the git-clone[1] command to download a A freshly cloned repository contains a single branch head, by default named guaranteed that the commit can never change without its name also changing. The branches of this repository are called "remote branches" from our point of view. "Git: Correct way to change Active Branch in a bare repository?" "How do I change a Git remote HEAD to point to something besides “master”" "How does origin/HEAD get set?" Note: you need to use git branch --list (or git branch), not git branch -l.

git-clone Documentation, Want to read in your language or fix typos? You can help Specifying --no-local will override the default when /path/to/repo is given, using the regular Git transport instead. In a non-bare repository, this is the branch that will be checked out. --branch can Create a bare repository to publish your changes to the public:. Compared to --bare, --mirror not only maps local branches of the source to local branches of the target, it maps all refs (including remote-tracking branches, notes etc.) and sets up a refspec configuration such that all these refs are overwritten by a git remote update in the target repository.

githooks Documentation, This hook is invoked by git-commit[1] right after preparing the default log a flag indicating whether the checkout was a branch checkout (changing branches, See contrib/hooks/setgitperms.perl for an example of how to do this. Just before starting to update refs on the remote repository, the pre-receive hook is invoked. If you select a different branch, the system will automatically pull changes from the remote repository when it checks out that branch. If the repository is a bare repository, the system will not display this information. Currently Checked-Out Branch — The currently checked-out local branch. Click to open the Gitweb interface in a new browser

Git FAQs, Set the remote. git remote add origin path to/url for your bare repository Update files. Edit a file. When done editing, commit the changes First change to the branch (could be master, the default branch) you want your branch to start from. Remove your current Git repository; Reconfigure Git with the same settings, but make sure Import existing Data Factory resources to repository is selected and choose New branch; Create a pull request to merge the changes to the collaboration branch; Below are some examples of situations that can cause a stale publish branch: A user has multiple

  • IMO you're just fundamentally trying to do The Wrong Thing here. If you want the default branch to be something other than master, then that branch needs to be the master. Alternatively, use two different repositories.
  • How is this fundamentally trying to do the wrong thing here? A bare repository supports multiple branches. I use a bare repository as back-up to my local repository, and as such mirror the branches. I have a master on both and a development branch on both. If I want to see the log of the development branch on the bare repository, I have to hack files - seems like git is fundamentally wrong here with regard to bare repository support.
  • @NicholasKnight IMHO you're fundamentally wrong here. There is nothing special about "master" as a branch name, it's just a default. In the respositories that maintain we don't have a master branch, as "master" is not meaningful to the company. Whenever we do a release we create a new maintenance branch with the new release number, and assign that as the active branch.
  • @NicholasKnight While I appreciate where you're coming from, this is the first SO Q/A that's told me how to switch to master! I had my initial repo on a feature branch when I made my bare clone, and subsequent clones from that bare repo were defaulting to that branch instead of master.
  • Wow - this question just runs and runs - it's my number 1 reputation point scorer! The thing about "master" is that it's just a name, and if it doesn't make sense for your organisation, team, project, phase, whatever, then choose something that IS appropriate so that when you collaborators clone your repo they immediately switch to the branch that you, as Configuration Manager, want them to be on. I used to work with ClearCase (bletch!) so your choices were "main", "main" or "main". Yuk.
  • Thanks! I do have direct access to the remote bare repo so git-symbolic-ref will do the job. I like the no-common-ancestor trick mentioned on the other thread, though - definitely one for the bottom drawer. I spent ages Googling for this but couldn't find your previous answer, yet "git remote head master" finds it as the second-highest ranked hit, just below git-remote(1). Bizarre. Just goes to show how hard it is to find something when you don't know exactly what you're looking for.
  • git symbolic-ref HEAD refs/heads/mybranch worked just fine for me! THANKS! ;)
  • I really appreciate this question, because I accidentally checked out a different branch than master and now I had to fix that.
  • This doesn't work for me. Oddly, even though the remote HEAD in the bare repo now shows the correct branch, git STILL defaults me to a different branch when I clone from it!
  • @Magnus that would be a good question of its own to ask in a new page.
  • This very simple solution worked for me, thanks! One note: I had to create an empty work-tree directory by hand for the command to execute successfully.
  • While what you say is true, the fact that you're using -b to select a particular branch breaks your answer in the context of my question, which is how do you set the DEFAULT branch.
  • There are subtle breaking problems that can be introduced by directly editing Git ref files. I highly recommend against that. The plumbing commands are easier and safer than directly editing refs.