DEV Community

Cover image for Basic git commands
Anisha Mohanty
Anisha Mohanty

Posted on

Basic git commands

Working with Git on the command line can be messy. To help with that, I have listed together with some basic Git commands, what each one means, and how to use them.

Command: git init

Usage :

Used to create an empty Git repository or reinitialize an existing one. Also, you create a repository within a new directory by specifying the project name.

$ git init 

#initailize empty git repository with a name
$ git init <project-name>

Command: git clone

Usage :

Used to create a local working copy of an existing remote repository.

$ git clone <remote-repository>

Command: git add

Usage :

Adds files in the to the staging area for Git. Before a commit is made to a repository, the file needs to be added to the Git index.

#add all files
$ git add .

#add a specific file:
$ git add <file-name>

#add an entire directory:
$ git add <directory-name>

Command: git rm

Usage :

Remove files from the working tree and from the index.

#remove a file from the working index (cached)
$ git rm --cached <file-name>

#delete a file (force)
$ git rm -f <file-name>

Command: git log

Usage :

Show commit logs.

#show entire git log
$ git log

#show git log based on commit author
$ git log --<author>="Author Name"

Command: git status

Usage :

Show the working tree status.

$ git status

Command: git branch

Usage :

List, create or delete branches.

#create a new branch
$ git branch <branch-name>

#list all remote or local branches
$ git branch -a

#delete a branch
$ git branch -d <branch-name>

Command: git checkout

Usage :

Switch branches or restore working tree files.

#checkout an existing branch
$ git checkout <branch-name>

#checkout and create a new branch 
$ git checkout -b <new-branch>

Command: git commit

Usage :

Record changes to the repository.

#add a commit with a message
$ git commit -m "commit message in quotes"

Command: git merge

Usage :

Join two or more development histories together.

#merge changes into current branch
$ git merge <branch-name>

Command: git fetch

Usage :

Download objects and refs from another repository.

#fetch all of the branches from the repository
$ git fetch <remote-name>

#fetch the specified branch
$ git fetch <remote-name> <branch-name>

Command: git pull

Usage :

Fetch from and integrate with another repository or a local branch.

$ git pull <branch-name> <remote-name>

Command: git push

Usage :

Update remote refs along with associated objects.

$ git push <remote-name> <branch-name>

# push all local branches to a remote repository
$ git push β€”all

Command: git remote

Usage :

Used to connect a local repository with a remote repository.

#add remote repository
$ git remote <command> <remote-name> <remote-URL>

#list named remote repositories
$ git remote -v

Further Learning

There are plenty of more commands, here are few references that might be useful.

Thanks for reading, I hope you get git done. πŸŽ‰

Top comments (0)