The Coding Canuck

Git workflow

Published on

Table of content

Git is a free open-source distributed version control system. Without it, collaboration becomes a problem. The reality is that if you want to become a developer of any kind, you'll have to master it. To install it simply follow the steps outlined on Git's website.

There are many tools to visualize and perform git commands via a GUI. I used the source control tab in VSCode for a number of years. That being said, GUIs are for soy devs. If you want to gain the respect of senior devs at your job, learn to use the command line!

Add your name and email

Git creates an immutable log of commits. Other than code, it saves information like a unique commit id, the timestamp and the identity of the author of the commit. Because of this, it wants your name and email.

# global user name
git config --global user.name "Your Name"

# global email
git config --global user.email "youremail@example.com"

Init new git repo

Whenever you are starting a new project. After cd'ing into the directory, the first step should be to initialize a new git repository.

# Create an empty Git repository or reinitialize an existing one
git init

At this point, git will start tracking all new changes in the directory. It also creates the main branch. In older versions, it was called the master branch but that triggered some people.

Display the status of current branch

It's easy to forget on which branch you are and what files were modified. Whenever you are in doubt, use git status.

# display branch information, changes and untracked files.
git status

# display only the changes
git status -s

Create new branch

We don't develop directly on main. Let's create a new branch instead with branch.

# create new branch from the one you currently are on
git branch branchName

Just be vigilant from which branch you are creating this one. You'll usually want to switch to main before creating a branch.

Change/Create origin of branch

You might have a repo and want to change the origin.

git remote set-url origin https://github.com/YOUR_URL.git

List all of the local branches

You'll soon start create a bunch of different branches and forget their names. Whenever you want the list, branch is the answer.

# display all local branches
git branch

The output is weird here, you'll have to type q to exit.

Change branches

Switch to branches with checkout.

git checkout branchName

Just make sure that all current changes are either stashed or commited.

Add changes

You need to select which changes you want to include in the next commit. To do this, you add changes in the staging area with add.

# Add a specific file to the staging area
git add filename.txt

# Add all changes to the staging area
git add .

Commit your changes

Once all changes are in the staging area, it's time to create the commit. Commits needs a message for future git archeology which will inevitably happen.

# Commit all changes currently in the staging area with a message
git commit -m "a very professional message with no typos"

Push feature branch on github for the first time

You created a new branch, commited stuff on it. Now you want to push on github, the standard git push will not work. You'll get an error saying "fatal: The current branch branch_name has no upstream branch". This one always gets me.

This simply means that the branch that you have created locally does not exist on github. You need to create the branch on github.

# Create your new branch on github and push to it
git push --set-upstream origin branch_name

From there you will see your new branch on Github.

Delete local branches

Once your finished with a local branch and push it on github, it will live on your machine.

git branch -d branch_name