In our earlier tutorial, we have learned to install git on our machines. In this tutorial, we will discuss how we can use Git in the command-line interface or CLI to perform various operations with Git. So let's start, In our earlier tutorial, we have learned to install git on our machines. In this tutorial, we will discuss how we can use git i.e. various commands that are used with git. So let's start,
( Recommended Read: How to install GIT on Linux (Ubuntu & CentOS) )
Setting user information
Before we can use Git in the command line, we should first setup user information for GIT, actually, this should be first step after installing git. We will add user information (user name & email) so that when we commit the code, commit messages will be generated with the user information which makes it easier to keep track of the commit progress. To add useful information about the user, the command is 'git config'
$ git config --global user.name “Daniel”
$ git config --global user.email “[email protected]”
After adding the information, we will now check if the information has been updated successfully by running,
$ git config --list
& we should see our user information as the output.
( Also Read: Scheduling important jobs with CRONTAB )
GIT commands list
Create a new repository
To create a new repository, run
$ git init
Search a repository
To search a repository, the command is
$ git grep “repository”
Connect to a remote repository
To connect to a remote repository, run
$ git remote add origin remote_server
Then to check all the configured remote server,
$ git remote -v
Clone a repository
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, then the command to clone the repository is,
$ git clone [email protected] :/repository_path
List Branches in the repository
To checklist all the available & the current working branch, execute
$ git branch
Create new branch
To create & use a new branch, the command is
$ git checkout -b 'branch name'
Deleting a branch
To delete a branch, execute
$ git branch -d 'branchname'
To delete a branch on a remote repository, execute
$ git push origin:'branchname'
Switch to another branch
To switch to another branch from the current branch, use
$ git checkout 'branchname'
To add a file to the repo, run
$ git add filename
Status of files
To check the status of files (files that are to be committed or are to added), run
$ git status
Commit the changes
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”
To push changes made to the master branch of the repository, run
$ git push origin master
Push branch to the repository
To push the changes made on a single branch to the remote repository, run
$ git push origin 'branchname'
To push all branches to the remote repository, run
$ git push --all origin
Merge two branches
To merge another branch into the current active branch, use
$ git merge 'branchname'
Merge from remote to the local server
To download/pull changes to working directory on local server from remote server, run
$ git pull
Checking merge conflicts
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'
To create tags to mark any significant changes, run
$ git tag 'tag number' 'commit id'
We can find commit id by running,
$ git log
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 the 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
Get the complete command list for Git
To get the complete command list for Git, execute the following command,
$ git help
That's it guys for this tutorial on how to use the Git command line. We will be back soon with more interesting tutorials. If you wish that we write a tutorial on a specific topic, please let us know via the comment box below. As usual, your comments & suggestions are always welcome.