Hit enter after type your search item

List of Generally used Git Commands 

In this post, I am going to list Git commands that you may require using on regular basis but don’t want to remember. Like the other tutorials of Git commands, I am not giving examples as the length of the post will be too long. So there you go:

Initializing Git command

$ git init

Adding a repository

For example:

For changing the current repository:

$ git remote set-url origin “repo URL”

For example:

How to checkout existing remote?

Use this command for seeing the existing remote:

$git remote –v

Creating a branch command

For creating a new branch in Git, use this command:

$ git branch_name

For checking out/changing a branch:

$ git checkout branch_name

For creating a new branch and checkout by the single command:

$ git checkout –b branch_name

This command will create a new branch and checkout to the new branch as well.

Removing a branch

$ git branch -d <branch-name>

For example:

If the branch is not removed and you get the branch not fully merged error then either fix the issue or you may use the capital D i.e. –D flag as shown below.

Listing branches

If you have a number of branches in the repository then it might be required to list all as names of all branches can be hard to remember. To list all branches in the remote and local repo:

$ git branch –a

For only remote branches:

$ git branch –r

See complete tutorial with examples for listing branches.

Adding files command

For adding a file in the current branch:

$ git add file_name

For example:

Adding multiple files command

Just give the file names separated by space:

If you have many files/directories (dozens) and want to add by single Git command:

$ git add .

That is, use the dot (.) after add.

You may also use –all flag in the git add command:

$ git add –all

This will add all files/folders in the staging area.

How to remove files

For removing a file from Git repository, use the rm command as follows:

git rm file_name

For example:

$ git rm footer.php

It will remove the file from Git working tree as well as file system.

To keep the file in the file system and just removing it from working tree:

$ git rm –cached footer.php

For removing a directory:

$ git rm –r folder_name

For example, if our directory name is “images”:

$ git rm -r images

Again, this command removes the directory from Git working tree as well as file system

For keeping the directory in the file system and removing from the working tree:

For removing multiple files:

$ git rm file1.txt demo/file2.txt file-3.txt

Saving changes – the commit command

For saving the changes in Git repository e.g. addition of files, deletion or updating files/folders etc. you may use the commit command as follows:

For modifying the commit message, use the –amend flag:

Adding/committing by single command:

$ git commit -a -m “message for the commit purpose”

For committing hunk of a file, use the –patch flag:

$ git commit –patch

Learn more in Git commit tutorial.

Undo last commit commands

The reset command can be used to undo the last commit:

The above command will move the HEAD back to the previous commit and any changes (file addition, removal, modifications) are undone.

Keep changes and move HEAD to the previous commit

Read full tutorial of undo last commit

Download content by pull command

For downloading content from the remote repository to your local system and merging, use the git pull command:

$ git pull origin master

Where origin is the name of the remote repository and master is the branch name. You may set the name other than origin as adding the remote repository.

For example:

$ git remote add my_repo https://github.com/git-test-jaz/hello-git.git

In that case, the above pull command will be:

$ git pull my_repo master

Pull command without new merge commit:

Read full tutorial about GIt pull command

The fetch command

The fetch command only downloads the content from the remote repository.


$ git fetch origin

The merge command

For joining the two or more histories or developments, use the merge command. The pull command is the combination of fetch + merge.

An example of showing the sequence of merge command can be:

So, we created a new branch based on the master branch. This is followed by adding a new feature – the footer in the demo-branch.

The changes are committed and then we merged this demo-branch feature into the master branch.

Full tutorial of the merge command

Uploading the content by push command

The push is the opposite to the pull command; to upload the content, any changes made to the remote repository.

$ git push origin master

For uploading multiple branches:

If you find merge conflicts as trying push command, you may use the –force flag:

However, you should this command carefully. Learn more about Git push command here.
This div height required for enabling the sticky sidebar