Git and its Basic Commands

Abhishek Pundir
Geek Culture
Published in
4 min readAug 22, 2021

If you are an aspiring Software Developer, you must have come across Git (or any other version control system like Mercurial, Subversion, etc.).

While working on an individual project, you might not feel the need to use any version control system but things completely change when you are working in a team, or with different collaborators.

In this post, I will be covering some basic git commands.

Let’s Get Started!

What is Git?

Git is a Version Control System which means it keeps track of all the changes that you, as well as others, make in the project. It helps you to work on individual branches, revert the code back to the previous state, compare code over a specific duration of time, keeps track of the changes made by each individual, etc.

Getting Started

git init

This command creates an empty git repository or reinitializes an existing one. Navigate to the project directory using the terminal and run this command, now you will be able to use the directory as a git repository.

git init

git clone <repo url>

This command is used to create a copy or clone a remote repository into a new directory on your local machine. <repo url> is the URL of the remote location where the project is hosted remotely (like on github.com).

remote is the location where the project is hosted remotely.

git clone https://github.com/pundirAbhishek/Git-Tutorial.git

Storing your changes

Once you make the necessary changes you can now add those changes to the remote branch.

git status

This command displays the state of the working directory and the staging area. It lets you see which files have been modified, added, or deleted in your local repository.

git status does not show any information regarding the committed project history.

git status

git add *

This command adds the changes from your working directory to the staging area. A staging area is a buffer area between your working directory and the remote. Instead of pushing the changes directly to the remote, git first keeps the changes in the staging area, if we want to change anything before we push those changes to the remote. If we want to make any changes to the files we can easily unstage the files.

* can be changed to a particular filename if we want to add only a single file or we can use . if we want to add all the files at once.

git add filename

git add .

git commit

This command takes all the files in the staging area and saves the changes to be pushed on the remote. We can also give a message along with the commit which helps to identify the changes we are pushing and for future reference for us as well as everyone else currently working on the project or who will be working on the project in the future.

git commit -m “Commit message goes here”

git push <remote> <branch-name>

This command is used to update local repository changes to the remote repository. Pushing is how we can transfer the commits from our local repository to the remote repository. <remote> is the location where the project is located, it is generally origin. <branch-name> name of the branch where we want to push the changes.

git push origin master

git stash

This command temporarily shelves the changes in our working repository so that we can work on something else, and then continue from the same position where we left off. git stash takes our uncommitted changes, saves them for later use, and then reverts them into our working directory.

git stash

Managing branches

Branching is useful when multiple collaborators are involved in a single project. Every individual can work on their branch depending on the feature they are working on etc. and then, later on, merge every branch into a main/master branch.

git branch

This command lists all the branches available in your working directory and highlights the current working branch. This is useful when we need to look at the name of the branch we are currently working on or of the target branch we want to switch to.

git branch

git branch <branch-name>

This command makes a new branch in your working directory with the name <branch-name> . But your current working branch does not change.

git branch branchName

git checkout <branch-name>

This command changes your current working branch to an already existing branch on your machine named <branch-name> .

git checkout branchName

git checkout -b <branch-name>

The above command helps the user to create a new branch named <branch-name> from the current branch and makes it the current working branch. This command combines the functionality of git branch and git checkout into a single command.

git checkout -b branchName

git pull <remote> <branch-name>

The above command lets the user to get all the changes on the branch <branch-name>from the remote and merge them onto their local repository and merge the changes into their current branch. This helps us to update our local repository with the changes that others might have done. This operation sometimes might lead to merge conflicts which means that the same file or code snippet has also been modified in the other branch and the user must inspect which changes are to be kept.

git pull origin branchName

git merge <branch-name>

The above command helps to integrate changes from another branch into your current branch. It will combine all the changes from the <branch-name> branch to your current working branch. Same as the git pull command this command can also lead to merge conflicts and we have to resolve those conflicts before the merge is successful.

git merge branchName

Conclusion

This is not an exhaustive list of commands but some that I used on a daily basis during my work.

Please feel free to share your thoughts and suggestions.

--

--