Git Cheat Sheet

A memory dump that will remember even when I forget

Page content

There are things I do rarely with git that I need to document so I don’t completely forget them. This is a place to capture those tasks and notes.

 

 

Switching from master to main for Existing Repositories

There is a movement to stop the using master as the name of the primary branch in git repositories. The consensus seems to be to use main instead.

This, of course, involves removing the use of words that can be misunderstood, misinterpreted, or that have unecessary cultural baggage. In the case of git the background is a bit muddy. The person who coded the first master into git code meant it as representing master as in digital master - of which copies were made. But Bitkeeper, the pre-existing version control software that inspired/demanded the creation of git did use `master’ in terms of master and slave.

As a result, intentionally or not, the term master for the primary branch of git repositories is changing. Instead main is being used.

For new repositories - this is not a problem or an issue. For existing repositories, that have existed for years, there is the matter of how to rename master to main in all instances of the repository.

In my case I have types of instances for my personal repositories:

  • Local instances - the working copies on the various computers I access.
  • Bare instances - the copies of the repository that I synchronize local instances with.

I work in a local instance and then synchronize my changes with one or more bare repositories. Usually these are located on separate machines and accessed as remote repositories using ssh. On the machines that host them they can also be accessed via using file or on the filesystem directly.

These are the steps to rename the master branch on local instances as well as properly updating the branch on the bare instances.

 

Renaming master in a Local Repo

Ensure the local repo is on the master branch:

git status          # check current branch
git checkout master # change to master if required

Rename the master branch to main:

git branch -m master main  # move the master branch to main

Check that the branch has been renamed:

git status

Push the new branch to the remote repositories:

git push origin HEAD

Note: This only pushes the current HEAD (main) and does not change the branch that HEAD points to on the remote repository.

 

Change master in Other Local Repos

For other local instances of the repo - check that the current branch is master:

git status          # determine current branch
git checkout master # change branch if required

Rename master to main:

git branch -m master main

Check that push and pull works from each remote bare repository:

git pull origin main
git push origin main

 

Remove master Branch from Bare Instances

This is a two step process.

First - in the bare repository itself - set HEAD to point to main instead of master:

git symbolic-ref HEAD refs/heads/main

Second - from a connected local repository - delete the master branch at each bare instance:

git push origin --delete master

Note: This delete will not work if HEAD still points to master in the bare repository.

Following these steps minimizes the chance for errors.

  • Performing a first local rename and pushing main to the bare repositories leaves master intact on the bare instances.
  • Making the change at a second local instance confirms that main has properly propagated to the bare instance.
  • Removing master from the bare instances at this point is safer than deleting it prior to using a second local instance to check that main is properly in the bare instances.