Command | Description | Example |
---|---|---|
init |
Initialize a new Git repository | git init |
clone |
Clone a remote repository | git clone <remote_url> |
status |
Show the status of the repository | git status |
add |
Add files to the staging area | git add <file> , git add . |
reset |
Unstage changes | git reset <file> |
diff |
Show the changes on a file | git diff <file1> |
diff |
Show the changes on the staged files | git diff --staged |
commit |
Commit changes to the repository | git commit -m "<commit_message>" |
branch |
List branches | git branch |
branch |
Create a new branch | git branch <new_branch_name> |
checkout |
Switch to a branch | git checkout <branch_name> |
checkout |
Create a new branch and switch to it | git checkout -b <new_branch_name> |
merge |
Merge changes from a branch | git merge <branch_name> |
log |
View the commit history | git log |
log |
View the commit history with number of ins/del | git log --stat |
fetch |
Fetch changes from a remote repository | git fetch |
pull |
Pull changes from a remote repository | git pull |
push |
Push changes to a remote repository | git push |
You can install Git by clicking on the link below
-
Use
git --version
to check if Git is installed.git --version
-
Use
git config --global user.name
andgit config --global user.email
to configure Git.git config --global user.name "<first_name> <last_name>" git config --global user.email "<email>"
--global
is used to configure Git globally.<first_name> <last_name>
is the name of the user you want to configure Git for.<email>
is the email address of the user you want to configure Git for. -
Use
git config --global color.ui
to enable colored output in Git.git config --global color.ui auto
always
always color output of capable Git commands.auto
enables colored output of capable Git commands (default).false
,never
,off
orno
disables colored output of capable Git commands.true
,on
oryes
enables colored output of capable Git commands.
Use git init
to initialize git in the working repository.
git init
git init <directory>
<directory>
is the name of the directory you want to initialize git in. After initializing git, usecd <directory>
to move to the directory.
Use git clone
to clone a repository.
git clone <remote_url>
<remote_url>
is the URL of the remote repository you want to clone.
Use git branch
to list branches.
git branch
Use git branch
to create a new branch.
git branch <branch_name>
<branch_name>
is the name of the branch you want to create.
Use git checkout -b
to create a new branch and switch to it.
git checkout -b <branch_name>
<branch_name>
is the name of the branch you want to create.
Use git checkout
to switch to a branch.
git checkout <branch_name>
<branch_name>
is the name of the branch you want to switch to.
Use git branch -m
to rename a branch.
git branch -m <old_branch_name> <new_branch_name>
<old_branch_name>
is the name of the branch you want to rename.<new_branch_name>
is the new name of this branch.
Use git branch -d
to delete a branch.
git branch -d <branch_name>
<branch_name>
is the name of the branch you want to delete.
Warning
Before deleting a branch, make sure that it is not the current branch.
Use git status
to get status of the working repository.
git status
Use git add
to stage changes.
git add <file>
<file>
is the name of the file you want to stage.
Use git add .
to stage all changes.
git add .
.
means the current directory and its contents.
Use git reset
to unstage changes.
git reset <file>
<file>
is the name of the file you want to unstage.
Use git reset .
to unstage all changes.
git reset .
.
means the current directory and its contents.
Use git diff
to show changes on a file.
git diff <file>
<file>
is the name of the file you want to show changes on. If<file>
is not specified, it will show changes of all files.
Use git diff --staged
to show changes on staged files.
git diff --staged <file>
Use git commit
to commit changes.
git commit -m "<commit_message>"
<commit_message>
is the message of the commit.
Use git push
to push changes to a remote repository.
git push <remote_name> <branch_name>
<remote_name>
is the name of the remote repository you want to push on.<branch_name>
is the name of the local branch you want to push.<remote_name>
and<branch_name>
are optional.
Use git pull
to pull changes from a remote repository.
git pull
Use git fetch
to fetch changes from a remote repository.
git fetch
First, switch to the branch you want to merge into.
git checkout <branch_name>
<branch_name>
is the name of the branch you want to merge into.
Use git merge
to merge branches.
git merge <branch_name>
<branch_name>
is the name of the branch you want to merge.
Use git log
to show logs.
git log
Use git log --stat
to show logs with numbers of insertions and deletions.
git log --stat
Show the commits on branch_name_2 that are not on branch_name_1.
git log <branch_name_1>..<branch_name_2>
<branch_name_1>
and<branch_name_2>
are the names of the branches you want to compare.
git log --follow <file>
<file>
is the name of the file you want to show commit history.