Git Commands


9 min read

What is Git?

Firstly lets start with whats git?

Git is the most commonly used version control system. Git tracks the changes you make to files, so you have a record of what has been done, and you can revert to specific versions should you ever need to. Git also makes collaboration easier, allowing changes by multiple people to all be merged into one source.

So regardless of whether you write code that only you will see, or work as part of a team, Git will be useful for you. This is an important section to absorb, because if you understand what Git is and the fundamentals of how it works, then using Git effectively will probably be much easier for you.

In order to check if you already have Git installed on your computer you can type the command git --version in the terminal. If you already have Git installed then you will see what version you have. If you don’t have Git installed you can visit the Git website and easily follow the download instructions to install the correct version for your operating system.

Why You Should Use Git?

One of the biggest advantages of Git is its branching capabilities. Unlike centralized version control systems, Git branches are cheap and easy to merge. This facilitates the feature branch workflow popular with many Git users. Feature branches provide an isolated environment for every change to your codebase. When a developer wants to start working on something they create a new branch. This ensures that the main branch always contains production-quality code.

Using feature branches is not only more reliable than directly editing production code, but it also provides organizational benefits. They let you represent development work at the same granularity as the your agile backlog. For example, you might implement a policy where each Jira ticket is addressed in its own feature branch. Git is a very important tool used in software development because of the advantages it provides.



Git Config

This command sets the author name and email address to be used with your commits. Global username and global E-Mail ID.

$ git config --global "your_username"
$ git config --global ""

Git Init

Git init is a command used to initialize a new Git repository. It can be used to convert an existing, un-versioned folder into a Git repository. Also, you can use it to initialize an empty repo. cd into the directory, you want to initialize. Then, run this command.

$ git init

Git Clone

The git clone command is used to download the source code from a remote repository. This can be useful if you want to work on the same project but don’t have access to the original source code. When you clone a repo, the code is automatically downloaded to your local machine.

$ git clone <https://url-of-the-repository>

Git Checkout

The git checkout command allows us to switch to an existing branch or create and switch to a new branch. This will automatically switch you to the branch name you mentioned in the command. When you checkout a tag, the changes that have been made to the files in the repository since that tag was created are downloaded and applied. When you checkout a commit, the changes that have been made to the files in the repository since the most recent commit are downloaded and applied.

Switch to an existing branch:

$ git checkout <branch-name>

Create and switch to a new branch:

$ git checkout -b <new-branch-name>

Git Branch

Branch is one of the most important functionalities of Git. This allows teams to work on the same code base in parallel. Branch is a Git command that enables you to manage project branches. A branch is basically a separate version of your project, and the ability to work with different branches is one of most powerful features of Git. Usually, developers tend to create separate branches when implementing new features or introducing bug fixes — this is very convenient because this way they don’t mess with the main version of the application. To view all branches in your project, run:

$ git branch

To create a new branch, run:

$ git branch [YourBranchName]

Git Merge

The git merge command merges your branch with the parent branch. The parent branch can either be a development or master branch depending on your workflow. Git merge is used to combine changes made in different branches into one single branch — this makes it easier for developers working on the same project to see updates quickly. It also helps avoid unintentional errors when multiple people are working on the same codebase simultaneously. Before merging, you must make sure that you update your local develop branch.

$ git merge [branch name]

Git Status

When you’re feeling a bit lost with what’s happened in your repo the Git Status command can tell you all the information you’ll need to know. This command can give you information such as:

  • Your current branch

  • Whether your current branch is up to date

  • If there’s anything in the branch that needs to be committed, pushed, or pulled.

  • If you have any files that are either staged or not staged.

  • And if you have any files that are created, modified, or deleted.

$ git status

Git Commit

Git commit saves the changes in your local repository. Every time you commit your code changes, you have to include a brief description of the changes made. This commit message helps others understand the changes that have been done.

Commit any files added with the git add command and files that have been changed since then:

$ git commit -a

Commit files with a message:

$ git commit -m “YourMessage”

Git Add

Add is probably one of the most common Git commands. Basically, it updates the index with the content found in your working tree thus making the content staged for the next commit. An index is like a snapshot of your working tree. When you perform a commit, this snapshot is utilized. Therefore, before doing a commit you should perform git add for any files that were modified or created.

In the simplest case you can say:

$ git add .

This dot . means “take all the files in the working tree” and add them to the index.

However, you can also add individual files and folders as well as use fileglobs:

$ git add single_file.txt
$ git add my_folder

So basically after you have performed some changes in your project, you will need to run git add before doing a commit.

Git Push

To make all your committed changes available to your teammates, you’ll have to push them to the remote origin. It’s important to remember that git push command will upload only the changes you’ve committed.

$ git push <remote> <branch-name>

Git Pull

This command fetches and merges changes on the remote server to your working directory. You’d want to have the latest updates from teammates as well! The git pull command allows you to fetch all the changes that your teammates pushed and automatically merge them into your local repo.

$ git pull <remote>

In many cases, you will run into conflict because you had changed a line in a file that another teammate added. In such cases, you need to resolve the conflicts manually.

Git Diff

Git Diff is my go-to command when I want to quickly see the difference between my current branch and another branch (usually the branch I’m merging into). This will show you any uncommitted changes in your local repo.

$ git diff

To compare two branches :

$ git diff branch1..branch2

This will show all the file differences between the two branches.

Git Stash

Git Stash temporarily shelves your work, so you can switch to another branch, work on something else, and then come back to this at a later time.

It’s perfect if you need to work on something else and you’re midway through a code change, but aren’t ready to commit the code.

$ git stash

Now all the changes you’ve made will be stored separately, and the state of the project will be reverted to the last commit. You can create as many stashes as needed, and to view them run:

$ git stash list

To automatically also delete the stash from the stack, the git stash pop command is used.

If you want to do it for a specific stash in the stack, run:

$ git stash pop stash@{0}

Git Log

This command is used to list the version history for the current branch.

  • If you want to view only the last 3 commit history, you can use the following command: git log -n 3.

  • To condense the commit history into a single line and view them, run git log --oneline. This is the easiest way to get a high level overview of all the commit history. It might still be a bit too much if you’ve got a lot of commits.

  • If you want to view the commit history by a specific author, run git log --author"<author-username>".

$ git log

Git Fetch

Git fetch is used to retrieve files from a Git repository. This can be useful if you want to get a copy of the latest version of a file without having to clone or checkout the repository first. git fetch is a primary command used to download contents from a remote repository. git fetch is used in conjunction with git remote, git branch, git checkout, and git reset to update a local repository to the state of a remote.

Git fetch [options]

-A | — all

Fetch all remote branches matching the given path. If no path is given, the current branch’s branches will be fetched.

-B | — bisect

Fetch only the latest commit from each branch, or if there is no latest commit on a branch, then the branch’s HEAD is used. If there are multiple commits on a branch, then the last commit is used.

-C | — color

Prettify diagnostic output using colors. Defaults to true.

-D | — no-merge

Do not merge in changes from the remote branch. This overrides any merge that may have been attempted on the local branch. If a merge was attempted and it failed, this flag will have no effect.

-F | — force

Force fetching of any branches which do not have a local version that agrees with the remote version. This will overwrite any local changes that are incompatible with the remote version.

$ git fetch --all

Git Rm

Git rm is used to remove files from a repository. This can be useful if you want to delete a file from the project but don’t want to risk accidentally deleting any other files in the same directory structure. It takes the following arguments:

1. filename — The name of the file to be removed.

2. force — If set, forces deletion even if there are other files with the same name in the repository.

3. recursive — If set, recursively removes any child files of the target file.

$ git rm -r [file-name.txt]

Git Reset

reset is the command we use when we want to move the repository back to a previous commit, discarding any changes made after that commit. At a high level, the git reset command enables you to undo or "reset" code changes that you previously made.

$ git reset [<mode>] [<commit>]