Hands-On Git Workflow with Code Example

Go through the Git workflow step by step with hands-on practice (code example)

James Ho
Towards Data Science

--

Following the previous An Intro to Git and Github for Beginners, today we’re gonna get our hands dirty. This story is going to walk you through the Git workflow with practical code examples. Since Github is the most popular website to host Git repositories, we’ll be using it as examples. We’ll be working with Git’s Command-line interface, therefore, Github CLI is not considered.

Prerequisite

  1. You have to have Git installed on your local machine. You can follow the official documentation to do so.
  2. Have a Github account (a free version would suffice).

Clone the repository from Github

There are 2 ways to get a repository, create your own or get it from Github or equivalent. We’ll focus on cloning one from Github here. If you’re interested in how to enable Git in your own project and publish it on Github, this is an excellent article to help you do so.

You first need to go to the directory where you’d like to put the repository in the terminal. Let’s say we’d like to put it under Documents of home directory. When you go to a repository in Github, you can find a coloured “Code” bottom under “<Code>” tab. Under that, there are three options, HTTPS, SSH, and Github CLI. These are the three different ways that you can clone the repository.

The syntaxes are the same, they are all git clone + the URL within that option.

$cd ~/Documents/# HTTPS
$git clone https://github.com/<USERNAME>/<REPO NAME>
# SSH
$git clone git@github.com:<USERNAME>/<REPO NAME>.git

The difference is how your local machine access the remote repository. If you choose HTTPS, you will be prompted to type in your user credential whenever you interact with the remote (like pull, push, fetch, etc) if it is a private repository. On the other hand, accessing via SSH can save you a lot of trouble, especially when the repository is private. But you have to add your local machine’s SSH key to your Github account.

Once you have the repository cloned to your local machine, you need to create your own branch to work on.

Create a new branch

Let’s check the status first by running git status.

$git status
>>On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean

Now we can branch off from this up to date master branch (mostly develop branch). To achieve, we need git branch command.

$git checkout -b "new_branch_name"
>>Switched to a new branch 'new_branch_name'

Obviously, you can change the name of the branch to whatever you want.

Now, you have your branch set up, you can start working on whatever you’d like to change the files.

Rebase and Commit

Once you have everything done and ready to submit your work, always remember to rebase. To rebase to the latest master or develop branch, we need to pull the latest version first.

# Commit your changes first
$git add (this step is called staging, to add changes to the commit)
$git commit -a -m "Your commit message"
# Switch to develop/master branch
$git checkout develop
>>Switched to branch 'master'
Your branch is up to date with 'origin/master'.
# Use 'fetch' to check (just check, we haven't pulled yet)if there are updates in remote
$git fetch
# If nothing comes out, your develop is up to date. If there are updates, pull those updates to your local machine
$git pull

Now the develop/master branch on our local machine is up to date. We can then rebase on this branch

$git checkout new_branch_name
>>Switch to branch 'new_branch_name'
$git rebase -i develop

Push to remote

Recall that we only created the branch on our local machine so far, there’s no such a “new_branch_name” branch on the remote whatsoever. If you just run git push , you will get an error of the following message:

fatal: The current branch test has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin new_branch_name

This error message is complaining that it can’t find a remote branch where it pushes your commits to. The solution is simple, do exactly what it suggests you to do:

$git push --set-upstream origin new_branch_name

This line will create a branch on remote and push your current branch, which includes all the commits, to remote.

Create pull request

The last thing to do is to create a pull request. General practice is creating a pull request on Github, where the interface is easier to create a PR than using CLI. You can designate a specific person in the team to review your PR, make it a draft PR so you can keep working on it, and review all the changes you’ve made within this PR.

My Git series stories:

An Intro to Git and Github for Beginners

The Difference between Rebase and Merge

5 Git Practices I Found Most Useful

--

--

Analytics Engineer | I talk about data and share my learning journey here. Support my stories: https://hoooching.medium.com/membership