Frequently used git commands - Explained!
GitHub is a source code version control platform for online software development. Its main purpose is to store, track and collaborate software projects. It is useful when more than one person is working on a project. For say, If a team of developers want to work on a project and keep updating the code simultaneously, they can use GitHub to host their project, merge and integrate their code and resolve conflicts effectively.
Git is the tool used for source code management and is very popular in the developer world. Yet, it is confusing for beginners to use git due to its vast list of commands. This blog explains and helps programmers create a repository, push, pull and merge code using command line terminal efficiently. It also lists some of the popular and frequently used git commands which come handy while developing projects on GitHub.
Frequently used GIT commands:
This command is used to create a repository locally using the command line interface. A repository is a directory that contains all the project-related data. There can also be more than one project on a repository.
$ git init
Above command makes the local directory a git repository. If there are any files in the local directory, they will all be part of that repository.
The git init command also allows us to create a new blank repository and a directory together using the following command.
$ git init <project name>
The ‘git status’ command lists the files newly created as well as the files that are modified or staged locally. In other words, it shows the state of the working directory but, does not list the committed project history.
$ git status
It clones or copies a repo into a new directory and creates remote-tracking branches for each branch in the cloned repository.
$ git clone <repo path>
A cloned repository gets a copy of the history and it is now a functional git repository.
Configuration settings are usually done soon after installing Git. They can also be done at a later time using the following commands.
To set username globally
$ git config --global user.name "FIRST_NAME LAST_NAME"
To set email address: $ git config --global user.email "MY_NAME@example.com"
To set repository specific configuration, omit the ‘global’ keyword.
The git add command adds the new and modified files in the working directory to the staging area. Only the files in the staging area can be committed to be pushed into the remote repository.
$ git add <directory>
This command adds all the files under the directory into the staging area.
Each file can be staged one at a time using the following command.
$ git add <file-path>
All files in the working directory can be staged at once using a couple of different ways either by the option ‘-all’ or simply ‘-A’ for short.
$ git add -all
We can also specify the current directory along with the sub directories by using the special character ‘dot’.
$ git add .
While it is convenient to add all files in one shot, be cautious about adding unintended files, thereby pushing them into the repository.
The staged files need to be committed in the working directory in order to be pushed into the remote repository. This can be achieved by the git commit command.
$ git commit -m “<Message>”
Message stands for the comments related to the staged changes. For example, a new feature or a defect fix etc.
A branch is an independent line of development in the repository. Multiple branches can be created under a repo which are considered as multiple versions of the repository.
Using the git branch command, we can create, list, rename and delete branches.
$ git branch
Lists all the branches in the present repository specific to the user. Present working branch is denoted with a ‘*’ in front of it in the list.
To include all the remote branches(created by other users) in the list, use the option -a.
$ git branch -a
New branch can be created by giving a branch name. But, it does not checkout the branch.
$ git branch <branch-name>
There are two options to delete a branch. One is the safe way and the other is force delete.
$ git branch -d <branch-name>
The above option deletes the branch only if there are no unmerged changes in the branch. To force delete the branch, replace -d with -D.
There are situations one may want to rename a branch. This can be achieved using the following command.
$ git branch -m <branch-name>
Git renames the branch with the given branch name. Be sure to navigate to the branch that needs to be renamed.
The word ‘checkout’ in terms of git means, switching between versions. We use the git checkout command to navigate between the local branches in the repository.
$ git checkout <branch-name>
Git checkout can also be used to create a new branch and checkout simultaneously in one single command.
$ git checkout -b <branch-name>
Git pull is the command used to download and integrate the remote changes into the current local working branch. Do not forget to first checkout the branch you want to pull.
$ git pull
It is always safe to commit the local changes and then go for a git pull, because git pull downloads the remote changes and immediately merge them into the local repository. The conflicted files will be flagged to resolve manually. If there are uncommitted changes, the merge part of the git pull command will fail and the local branch will be untouched resulting in not being able to get the remote changes into the local branch.
The options for git pull are as follows.
$ git pull --no -commit
Fetches the remote content but does not create a new merge commit.
The git fetch command downloads the remote changes into the local branch without overwriting the local changes.
$ git fetch <remote>
The git fetch command without the remote name retrieves data from the default remote repository branch name.
Fetch is considered the safe way to get the remote changes compared to git pull due to its non-overwriting behavior when there are local changes noticed.
$ git fetch <remote> <branch-name>
The above command fetches the particular branch from the mentioned remote repository.
The git push command uploads the committed changes of the local branch into the remote repository branch.
$ git push <remote> <branch>
This command pushes the branch to remote along with necessary commits. If the branch does not exist in the remote repo, git creates one. The remote and branch names are optional.
$ git push –set-upstream origin <branch-name>
The easiest way to set an upstream branch is to use the “--set-upstream” option when pushing the branch to the remote repository for the first time.
Git merge command helps to merge two branches, usually the master branch and a feature branch. It means combining several sequences of commits into a single history.
First the master branch has to be checked out, to make the control point to the master. Then the command git merge has to be run to merge the new feature into the master branch. Git merge merges the specified branch into the currently active branch.
$ git merge <feature-branch>
Conclusion: These frequently used git commands make a developer’s life easy while building and managing projects hosted on GitHub.