Git Guidelines: Branches

Exploring the branch + master relationship.

Meagan Rossi
3 min readSep 23, 2020

This is Part 2 in a series outlining the basic tenets of using Github through your terminal. This blog post assumes that the software environments for Github have been installed. It also assumes that the user has active repositories in their Github account along with associated text editors/notebooks for coding.

Branches

Branches are excellent for group projects and are also recommended for individual projects as a means to control drafts and maintain version control. Branches function as individual working files with the master standing as the centralized folder for project contributors or viewers. Approved or finalized changes made by individual branches are pushed to the master version so that all project users have access to the most recent draft. When working with others, it it essential to communicate when you are pushing changes to the master draft to avoid concurrent pushes.

Image Source: Noble Desktop
# Create branch
$ git checkout -b <branch name>
# List repo branches
$ git branch
# Prunes, or removes, deleted branches
$ git fetch -p

Fork

If the project has made some progress and you want to start from what has already been completed when creating a branch, fork (or clone) the master to begin working remotely without affecting the project until a pull request has been made.

# Go directly to Github to copy/paste the link
$ git clone <repo>

Mapping Branches

View the existing versions and relationships active in a repository and modify as needed.

# Identify which branch is active
$ git status
# View all branches
$ git branch -a
# Switch branches by first signing into a branch
$ git checkout <branch name>
# Show the user whether they are actively in the master or in a branch of the working git folder and maps out the existing master/branch relationships.
$ git remote show origin
# Add master origin
$ git remote add origin <github link>
# Track branch to master
$ git push -u origin <branch name>
# The u is a tracking parameter
$ git push -u origin master — tags

Working v. Staging

This is the difference between your working environment (draft) and your staging area (commit, ready to send to master). This is the sensitive area for version control and will likely require approval.

# View differences between trees. 
$ git diff <master name> <branch name>
# View the unstaged changes (not committed).
$ git diff — staged
# View differences between commits.
$ git diff master branchB
# What are the changes that I need to pull?
$ git fetch <remote> <branch>
# Fetch <remote> and merge into the local version.
$ git pull <remote>
# Push to <remote> with commits.
$ git push <remote> <branch>

Git Config

These are the settings for your Github account. You can define your username, email address, define passwords and more which will benefit both yourself and project collaborators. If you want to create settings for all repos, use the global command.

# View config command options
$ git config — list
# Git looks for configuration values
$ git config — global -e
# Open text editor to access/edit configuration settings.
$ git config --global --edit
# Create git alias. This is a method to rename a git command and is often used as a shortcut. An example of standard command is the language following git, such as 'commit'.
$ git config --global alias.<alias> <standard command>
# Remove git alias
$ git config — global — unset alias.<git alias name>

Please email me with any questions or follow-ups, meaganrossi@gmail.com.

--

--