Quick Guide to use most basic Github Commands

Vivek Shakya
3 min readJan 8, 2021

Git hub is a great version control system(VCS) which is widely used by developers whether they are students or professionals as a repository for their code base. As a student I have seen lot of times students around me struggled to understand and remember git commands, and they always end up executing wrong command in wrong branch or they worry about code sync and or they are not just confident about git.

This blog is just a way to help fellow students to understand the git commands and provide them reference for them as well as myself for most generic git commands that git users must have knowledge about.

Set remote upstream and remote origin

Upstream — it is the original repository from where you fork your working repository.

Origin — this is your working repository which you forked from the original repository(upstream)

Now when you start working on your local machine you need to clone the forked repository(origin)

Git clone — used to clone the repository onto your local system

git clone <git repository remote url>

you can find the git repository remote url here as shown in above image, HTTPS url is simpler to access specially for starters, whereas to use SSH url we need to create a SSH keypair and add the public key to github .

Refer github’s documentation to learn more — https://docs.github.com/en/free-pro-team@latest/github/using-git/which-remote-url-should-i-use

Git Command to set remote upstream on your local machine

git remote add upstream <git repository remote url>

by default your forked repository will be set as the remote origin when you clone the forked repo on your system.

Command to verify the remotes

git remote -v

Output of the above command will look like as below

origin git@github.com:shakyav/test.git (fetch)
origin
git@github.com:shakyav/test.git (push)
upstream
git@github.com:shakyav-upstream/test.git (fetch)
upstream
git@github.com:shakyav-upstream/test.git (push)

This setup also helps to track your changes and github will tell whether repos are in sync with each other.

Whenever we work on a codebase where multiple people are contributing to the same codebase, it is a good habbit to make a new branch for every new feature you individually work on so that it doesn’t create any conflicts with other’s code. In general as well it is good to create a new branch for each new feature to stay away from any conflicts.

create a new branch on your local machine from command line

git checkout -b <feature_branch_name>

feature_branch_name could be any name relevant to the feature you are working on.

Stage your changes

git add .

this command will stage all your changes but you can also use the below command to stage a specific file

git add <path/file.txt>

if you want to see what all files have been modified or created since your last commit, use the following command:

git status

Commit your changes

git commit -m “<commit message>

Push your changes to the feature branch

git push origin <feature_branch_name>

In case you are working on an existing branch, make sure your origin master/main is in sync with upstream master/main and your feature branch is in sync with the origin master/main.

Sync forked repo(orign) with Upstream

  1. checkout to the master/main branch of the forked repo and execute following command to fetch the upstream

git fetch upstream main

2 . merge the changes to the origin master/main

git merge upstream/main

3 . Push your the code fetched from upstream to the origin’s main branch

git push origin main

4. Now checkout to the feature branch and rebase it with the origin’s master/main

git rebase origin main

5. Push the synced code to the feature branch on github so all the three(upstream,origin and origin’s feature branch) are in sync.

git push — force origin <feature_branch_name>

Refer the github doc to learn more — https://docs.github.com/en/free-pro-team@latest/github/collaborating-with-issues-and-pull-requests/syncing-a-fork

Some people prefer to use git pull command instead of git fetch and git merge, git pull does fetch and merge the remote content to create a new local commit .

For more detail refer — https://git-scm.com/docs/git-pull

Please feel free to comment , provide feedback or any updates or any corrections that I can make. I usually follow above process for my github repos and it works well for me and helps to stay away from any merge conflicts.

--

--

Vivek Shakya
0 Followers

Master's Student at Northeastern Univeristy