Skip to content
Related Articles

Related Articles

Useful Git Commands and Basic Concepts
  • Last Updated : 28 Dec, 2020

Open Source Development is an amazing platform for everyone, whether you are a Developer, an Open Source Enthusiast, or if you are a beginner who wants to do real-world Software Development.  

Now if you are looking to contribute to Open Source then you must have a working knowledge about Git and Github. Now, Git is a Software that is used as a Version Control System to manage/track your files in a Project that you are working on or if you are contributing to one.

So using Git and Github we can easily manage files, track changes to different files, manage workflows in a Project, and much more. That’s why they are such an important tool when working on Open Source Projects.

Today, we will be looking at some of the Useful Git commands, how you can Sync your Forks with the remote Repository. 

Setting up our Git Repository

Let’s make a Git Repository first of all and learn about all these Git commands.

Let’s start by creating a folder where we can have different files like a text file, an Excel Database file, a Markdown File, a Folder consisting of Source Code files (An actual Project may have many different files).



But right now for simplicity’s sake let’s keep these files empty.

We can use Git Bash to write Git commands or we can also use VS Code which has built-in support for Git. You can install Git here or you can check out How to integrate Git Bash with Visual Studio Code?. If you are using Git for the first time to know how to set up Git Bash.

Now let’s open Git Bash by Right-clicking and selecting “Git Bash Here”.

Initializing our Git Repository

Here we are initializing our Git Repository or you can say we are making our directory a Git Repository.

Note: If you already have made a Git Repository then don’t use this command again else all the changes that you have made to this Repository will get re-initialized.



Staging Files Initially

We are staging these files initially so that now we can track them using Git.

Creating Initial Commit

Let’s make our Initial Commit to our Repository.

Now let’s take a look at some of the useful Git commands for merging branches, forking a Repository, Renaming, and Deleting files using the command line, and much more.

Useful Git Commands

git diff 

We had already staged our files initially above and made our initial commit.

Now suppose I modify a file say “Test.txt” and save it.

Let’s run the “git status” command.

As you can see it shows that we have modified that file. Now let’s run the “git diff” command and see what happens.

Here using the “git diff” command we are actually comparing this file “Test.txt”(modified file) which is in our current working directory with the same file which we had staged earlier(i.e in the Staging Area). 

So using this command we can come to know what modifications did we do in any file in our current Working Directory with the same file in Staging Area.

Here as you can see the ‘+’ symbol denotes the changes that we made in that file(in our Working Directory) and ‘’ denotes the content in the file in the Staging Area

This command is not just limited to comparing two files, we can compare two branches as well.

git diff — staged

This command is used to know the difference between the files in the previous commit and the current Staging Area. To understand this in detail let’s modify a few files.

Now let’s stage these files and run the “git diff – staged” command.

As you can see it shows us the difference between these files in the previous commit and the changed version of these files in the Staging Area(currently staged but committed).

This can be helpful to review the previous commit and make necessary changes to any file.

Renaming and Deleting any file using Git Bash

Deleting/Renaming files can be a bit confusing and sometimes tedious especially if your Git Repository has a lot of files and folders because you would have to then stage all the files as well.

A cleaner and efficient way to do this would be to use the following Git commands:

Renaming a File

git mv <filename> <newfilename>

# <filename> is name of your file you want to rename.

# <newfilename> is name you want to give to your file.   

Deleting a file

Using these commands we can easily Rename/Delete any file in our Repository and the changes would also be staged automatically by Git so you wouldn’t have to worry about staging these changes.

git rm <filename>

git commit -a -m “<commit msg>”

Using this command we can directly commit any Tracked file in our Directory without placing it in the Staging Area.

Note: We cannot directly commit untracked files (newly created files) using this command. We first have to add that file to Staging Area(for it to be Tracked) and then commit it.

Syncing Forks in Git and Github

The World of Open Source Development is really active because so many developers contribute to different Open Source Projects. So if you are contributing to an Open Source Project then Syncing your forked Repository with the Original Repository becomes really important.

Suppose you already have a Forked Repository(on Github) which you want to sync with the Original Repository(from which you forked).

If you haven’t cloned the Forked Repo on your Local machine then just use the following command:

git clone https://github.com/<Username>/<Repository-name>.git <filename>

# filename: It is the name of file/folder for the Cloned Repository on your Local Machine.

Now we will add the Original repository as a remote repository(upstream):

git remote add upstream https://github.com/<Owner>/<Repository>.git

# add the original repository as remote repository called “upstream”

Now we will fetch the changes from the upstream repository:

git fetch upstream

# Fetch the branches and their respective commits from the upstream repository.

We will now switch to the master branch of the fork’s local repository and merge the changes in the upstream repository to the local repo’s master branch:

git checkout master

# switch to the master branch of your fork’s local repo

git merge upstream/master

# Merge the changes from upstream/master into your local master branch.

This brings your fork’s local repository in sync with the original repository. However, if you also want to update the Forked repository on GitHub then you would have to push the master branch (or the branch in which you merged the changes of the upstream repo).

git push origin master

# origin: forked repository on Github

And that’s it! Your Forked Repository on Github is also now synced with the Original Repository!!   

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :