We have already discussed how to install Git on Ubuntu & on CentOS/RHEL. We should aware of how to use git to complete our intended function. In this tutorial, we will discuss the git commands that you should be aware to use git.

Recommended Read: Conda Commands cheat sheet for Beginners

Also Read: Complete guide to install PyCharm on Ubuntu

GIT Commands for Beginners

Setting user information (git config)

This should be your step after you have installed git on your system. We need to add user information (user name & email), for tracking purposes of git. To add information about the user, the command is ‘git config’

$ git config –global user.name “Prince”
$ git config –global user.email “prince@thelinuxgurus.com”

To check the entered information, use

$ git config –list

Now let’s discuss other git commands.

Create a new repository (git init)

To create a new repository, the command is

$ git init

Search a repository (git grep)

To search a repository, the command is

$ git grep “repository”

Connect to a remote repository (git remote)

To connect to a remote repository, the command is

$ git remote add origin remote_server

Then to check all the configured remote server,

$ git remote -v

Clone a repository (git clone)

To clone a repository from a local server, run the following commands

$ git clone repository_path

If we want to clone a repository located at a remote server, the command would be

$ git clone username@server:/repository_path

List Branches in the repository (git branch)

To check the list of all available & the current working branch, execute

$ git branch

Create a new branch (git checkout)

To create & use a new branch, the command is

$ git checkout -b ‘branch-name’

Deleting a branch (git branch)

To delete a branch, execute

$ git branch -d ‘branch-name’

If required to delete a branch on a remote repository, execute

$ git push origin:’branch-name’

Switch to another branch (git checkout)

To switch to another branch from the current branch, use

$ git checkout ‘branch-name’

Adding files to commit (git add)

To add a file to the repo, run

$ git add filename

Status of files (git status)

To check the status of files i.e. files that are to be committed or that are to be added, run

$ git status

Commit the changes (git commit)

After we have added a file or made changes to one, we will commit the code by running,

$ git commit -a

To commit changes to head and not to the remote repository, the command is

$ git commit -m “message”

Push changes (git push)

To push changes made to the master branch of the repository, run

$ git push origin master

Push the branch to the repository (git push)

To push the changes made on a single branch to the remote repository, run

$ git push origin ‘branch-name’

To push all branches to the remote repository, run

$ git push –all origin

Merge two branches (git merge)

To merge another branch into the current active branch, use

$ git merge ‘branch-name’

Merge from remote to the local server (git pull)

To download/pull changes to the working directory on local server from the remote server, run

$ git pull

Checking merge conflicts (git diff)

To view merge conflicts against the base file, run

$ git diff –base ‘filename’

To see all the conflicts, run

$ git diff

If we want to preview all the changes before merging, execute

$ git diff ‘source-branch’ ‘target-branch’

Creating tags (git tag)

To create tags to mark any significant changes, run

$ git tag ‘tag number’ ‘commit id’

We can find commit id by running,

$ git log

Push tags (git push)

To push all the created tags to the remote server, run

$ git push –tags origin

Revert changes made

If we want to replace changes made on the current working tree with the last changes in the head, run

$ git checkout –‘filename’

We can also fetch the latest history from remote server & point it local repository’s master branch, rather than dropping all local changes made. To do this, run

$ git fetch origin

$ git reset –hard master

These were some of the git commands that are used regularly & should be able to get you started with using git. That’s all we have for this tutorial, please do send in any questions, queries or suggestions using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter | Linkedin

TheLinuxGURUS are thankful for your continued support.