Git Branches

One of the particular strengths of git(1) as a versioning software is its concept of branches. Each branch is an independent collection of commits, with no real need for centralized management. Branches have discreet points of divergence that can be reconciled if needed.


Renaming Branches

A local-only branch can be renamed easily. Given a repository named my-repo:

# ensure that you are in the repository directory and checked into the target branch
cd path/to/my-repo
git checkout master

# to rename branch 'master' to 'dev'...
git branch -m master dev

To rename a remote branch, first follow the above process. The remaining steps can produce errors.

git push -u origin dev
git push origin --delete master

If git returns an error like  ! [remote rejected] master (deletion of the current branch prohibited), then you need to adjust the HEAD of the remote repository. This determines what branch a new repository clone will start on. The 'default' branch cannot be deleted for obvious reasons.

Keeping with the example above, access the bare remote repository my-repo.git and edit my-repo.git/HEAD such that it points to the new branch's head.

ref: refs/heads/dev

Valid values correspond to the branch references in my-repo.git/refs/heads/.


Maintaining Local Branches or Forks

Best practice is to manage local changes in a separate branch. This will make it easier to merge upstream commits while maintaining your own.

After fetching or pulling updates, you will be some number of commits behind or ahead (respectively). To throw out local commits, use git reset --hard origin/master.

Then switch to your local branch and begin rebasing it. This process is visualized by upstream documentation as going from...

          A---B---C topic
         /
    D---E---F---G master

...to...

                  A'--B'--C' topic
                 /
    D---E---F---G master

To execute a rebase, try:

git checkout your-local-branch
git rebase master

With any luck, there will have been no conflicts and you will be all set.


CategoryRicottone