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

 $ git remote add origin "repo URL"

For example:

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

For changing the current repository:

$ git remote set-url origin “repo URL”

For example:

$ git remote set-url origin https://github.com/git-test-jaz/tst-demo.git

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:

$ git branch -d my_branch

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.

$ git branch –D my_branch

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:

$ git add header.php

$ git add folder/file_name

Adding multiple files command

Just give the file names separated by space:

$ git add file1.txt file2.txt header.php footer.php

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:

$ git rm -r --cached images

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:

$ git commit -m "message for the commit"

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

$ git commit --amend -m "Provide the new message here"

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:

$ git reset --hard HEAD~1

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

$ git reset --soft HEAD~1

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:

$ git pull --no-commit <remote>

Read full tutorial about GIt pull command

The fetch command

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

Example:

$ 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:

$ git checkout master

$ git branch demo-branch

$ git checkout demo-branch

$ git add footer.php

$ git commit –m “added footer component”

$ git checkout master

$ git merge demo-branch

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:

$ git push origin --all

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

$ git push origin master --force

However, you should this command carefully. Learn more about Git push command here.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!