Published in


Basic Git and GitHub workflow

Photo by Roman Synkevych on Unsplash

This guide is for anyone who is just starting with Git and GitHub. This will provide you a basic git workflow on how to fork a repository or create a repository in GitHub and work from your local workstation. Let’s get started

Before we begin with commands let’s understand what is git and how it works

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency

Git works based on moving contents to different layers. The following diagram shows the git workflow

Git Workflow

1. Working directory

Any directory on your local system can be a working directory for a Git repository.

2. Staging Area

This is an intermediate layer where you can move the files you want to commit and finalized with changes. Having this layer gives the flexibility for the user to select the files that should go in a commit.

3. Local Repository

The local repository is the git repository that exists on the local workstation. A local repository is created by cloning a repo or by running an initialization command.

4. Remote Repository

The remote repository is where you share your code for collaboration (like GitHub)

Now we will see these with the help of git commands

Pre-requisite: set git configs

Let’s set these configs before start working with git commands

  • Set username and email (This will be used for your commit messages)
git config --global "Your name"
git config --global "Email address"

Case 1: Fork another repo for contributing with your changes

Photo by Julian Wallner on Unsplash

Step 1: Fork

The first step is to fork the repo you need to your profile. You can fork the repo by visiting the repo you need in GitHub and in the top right corner you can see a button Fork

Step 2: Clone

Now that you have forked the repo to your profile. Next is to have a copy of that on your workstation. For that go to the repo from your profile. You can see a green button named Code. Click on that and select HTTPS and copy the link displayed and run the git clone command from your terminal or git bash if you are a windows user

git clone <repository link>

Step 3: Change to cloned repo

cd <repo cloned>

Step 4: Set Remote

The next step is to set upstream remote. By default when you clone the repo a remote named as origin will be created based on the repo link. Now you need to set a remote for upstream from which you actually forked repo. This is important for getting updates from upstream from the time you forked.

git remote add upstream <upstream repo link>

Step 5: Create a feature branch

Now you are all set to make changes to the repo. A good way to do that is to create a new branch instead of updating in the default branch. This will help your work organized and you can easily track the features you did. The following command will create a new branch and checkout to that branch

git checkout -b <feature branch name>

Step 6: Make changes

Now you are all set to make changes to the repo. Make changes to required files.

Step 7: Review Changes

First, let’s review which all files that you have changed

git status

Review changes made in branch

git diff

So important thing to note here is git diff will display changes from the working directory and staging area

If you want to revert changes did to any file displayed in git status run

git checkout -- <file>

Step 8: Add files changed to the staging area

Now that you have reviewed the files changed and verified they are as expected. Now you can add them to the staging area.

git add -a

If you want to only specific files seen in git status specify file paths instead of -a in git add command

Step 9: Review files in the staging area (Optional)

This is an optional step just to make sure you moved only required files to the staging area

git diff  --cached

This will compare files changed from the last commit and staging area

Step 10: Commit your changes

Now that you have reviewed your changes. It’s time to commit or create a snapshot of your changes at this point in time. It’s advised to give a commit message based on changes you did.

git commit -m "Commit message"

Step 11: Push your changes to Github

To push your changes to your Github repo

git push origin <feature branch name>

Step 12: Pulling changes from upstream repo

So now that you have a repo in your local workstation. But after you have forked the repo there were some updates done in the upstream repo from which you actually forked. To be on the same page you need these changes in your local workstation. To do that you need to pull changes from upstream. For this reason, we had set a remote named upstream earlier in Step 4. Run the following command to pull changes.

git pull upstream <branch in upstream from which you need changes> --rebase

If you want to pull changes from a branch named patch1 in upstream you will use git pull upstream patch1 --rebase

Follow this guide if you encounter any merge conflicts

Case 2: You already have a project in your local workstation but want to push it to GitHub

Photo by Tim Mossholder on Unsplash

Step 1: Initialize your project folder as a git repo

Since you already have a project in your local workstation and all you need is to push this to GitHub you need to initialize that folder as git repo first. From your project folder run

git init

Step 2: Commit your changes after reviewing

Following the same steps as above as Case 1, review your files and commit your changes. Note that git diff won’t show any output for newly created files

Step 3: Create a repo in your GitHub matching the same name as your project folder

You can find an option to create a new repository in the top right corner of your GitHub main page (+ sign near your profile pic). Just give your name to your repository and click Create repository. You can skip options mentioned in Initialize this repository with

Step 4: Push your changes GitHub repo

Since you created an empty repo it will display options on your repo page on how you can push your changes. You can look at instructions under …or push an existing repository from the command line section

git remote add origin <github repo link>
git branch -M main
git push -u origin main

Basically, you need to add remote from your local workstation to the newly created repo, rename your branch to main (this is needed since git client still creates default branch name as master and GitHub creates main as default branch), and then push your changes

Further Reading

I hope this article helped those who are just starting with Git and GitHub. Thank you for reading!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jayapriya Pai

Jayapriya Pai


I love computer science, automating boring jobs, and creating awesome tools. Opinions are my own not that of my employer