Show List

Common GIT Commands


Here are some of the most common Git commands that you will use while working with Git

Starting a Project

Below command creates a local repository. If project name is provided in the command, then a new directory with project name is created and repository is initialized inside this directory. If project name is not provided, then a new repository is initialized in the current directory.
 $ git init [project name]

Downloading a project

Below command downloads a project with the entire history from the remote repository.
$ git clone [project url]
To switch working directory to specified branch
$ git checkout [branch_name]
Below command will also create the branch (If does not exist already) and switch working directory to that branch
$ git checkout [-b][branch_name]
Below command fetches the changes and merges them into the local branch. Difference between git pull and git checkout is that checkout does not merge the changes but makes the working directory reflect them.
$ git pull

Git Stash

Git stash command is used to temporarily store the changes you have made so that you can work on something else and then come back to them and reapply later.

Below command will put the current changes in your working directory into stash for later use:
$ git stash
Use below command to apply stored (top most) stash content into working directory, and clear stash
$ git stash pop
Below command is to apply the stored (top most) stash content into working directory, but leave the stash in the stash list for possible later reuse.
$ git stash apply
Below command will delete specific stash from all the stashes
$ git stash drop

Git Branching

To list all branches in local repository
 $ git branch
To list all branches in local repository and remote repository
 $ git branch -a
To create a new branch referencing the current head
$ git branch [branch_name]
To join specified branch into your current branch
$ git merge [branch_name]
Below command will remove selected branch if it is already merged into another
$ git branch -d [branch_name]
Below command will force delete the branch (Even if it is not merged into another)
$ git branch -D [branch_name]

Committing and pushing the changes

Below command shows the state of working directory and staging area. It checks which changes have been staged, which are not and which files are not being tracked.
$ git status
To add all changes from the working directory to the staging area
$ git add -a
To add changes from selected files to the staging area
$ git add file_name_with_extension
To view changes between working directory and staging area
$ git diff [file]
To view any changes between the staging area and the repository.
$ git diff --staged [file]
To discard changes in working directory.
$ git checkout -- [file]
To create a new commit from the changes in the staging area
 $ git commit
This command shows the history of commits in the repository
 git log

    Leave a Comment


  • captcha text