Summary of common Git commands and syntax, documentation, and recommended reading to gain a general understanding of Git usage.
INITialize a new empty local repository [in the <target-directory>
], and add the REMOTE tracked repository:
git init [<target-directory>]
git remote add <remote-name> <remote-repository>
Or CLONE a <remote-repository>
[checking out a specific <branch>
other than master
] [into the <target-directory>
]:
git clone [-b <branch>] <remote-repository> [<target-directory>]
CONFIGure a name and email address:
git config [--local|--global] user.name "UserName"
git config [--local|--global] user.email "[email protected]"
List existing BRANCHes, [including those in the remote repository]:
git branch [-a]
CHECKOUT or switch the working copy to a <branch>
:
git checkout <branch>
List existing REMOTE tracked repositories:
git remote
FETCH updates to the current branch from the remote repository, without merging:
git fetch
PULL updates to the current branch from the remote repository, merging new commits into the working copy:
git pull
ADD all new/modified files and stage them to be committed:
git add -A
ADD specific new/modified files and stage them to be committed:
git add <file> [<file>...]
MOVE (or rename) a <file>
to a <new-file>
:
git mv <file> <new-file>
REMOVE (delete) a <file>
:
git rm <file>
Recursively REMOVE (delete) a <directory>
:
git rm -r <directory>
Check the STATUS of the working copy:
git status
DIFF [staged] changes [to a specific <file>
]:
git diff [--staged] [<file>]
COMMIT staged changes:
git commit -m "summary" [-m "details"]
Show commit LOGs:
git log
TAG the working copy, using the specified <tag-name>
:
git tag -a <tag-name> -m "message"
PUSH commits on the current branch to the remote repository:
git push
Create a BRANCH of the working copy, using the specified <branch-name>
:
git branch <branch-name>
git checkout <branch-name>
git push --set-upstream origin <branch-name>
MERGE the <source-branch>
into the <destination-branch>
:
git checkout <source-branch>
git pull
git checkout <destination-branch>
git merge --no-commit <source-branch>
git commit -m "message"
git push
1. Version control basics: About Version Control
2. Git basics: Git Basics
Initialize a new empty repository (https://git-scm.com/docs/git-init):
git init [<target-directory>]
Clone an existing remote repository to work on it locally (https://git-scm.com/docs/git-clone):
git clone [--verbose] [--branch <branch-name>] <remote-repository-url> [<target-directory>]
git clone [-v] [-b <branch-name>] <remote-repository-url> [<target-directory>]
- If
--branch
is omitted and--no-checkout|-n
is not specified, the default branch (typicallymaster
) will be checked out automatically. - If no
--origin
is specified, the remote repository will be tracked using the default name oforigin
. - If
<target-directory>
is omitted, a new directory with the name of the remote repository will be created within the current working directory.
Configure a username and email address (https://git-scm.com/docs/git-config):
git config [--local|--global] user.name "UserName"
git config [--local|--global] user.email "[email protected]"
- Tip: use
--global
to use the same account on all your repositories, or--local
to use a specific account on just this repository.
List available branches in the repository (https://git-scm.com/docs/git-branch):
git branch [--verbose [--verbose]] [--list] [--all]
git branch [-v|-vv] [-a]
- If
--all
is omitted, only local branches will be shown.
Check out a branch to work on locally (https://git-scm.com/docs/git-checkout):
git checkout <branch-name>
4. Pulling and pushing remote repositories: Working with Remotes
Show remote tracked repositories (https://git-scm.com/docs/git-remote):
git remote [--verbose] [show]
git remote [-v] [show]
Add a remote tracked repository (https://git-scm.com/docs/git-remote):
git remote add <remote-name> <remote-repository>
- The remote repository will be tracked using the specified
<remote-name>
(typicallyorigin
).
Fetch updates from the remote repository without merging them into your local working copy (https://git-scm.com/docs/git-fetch):
git fetch [--verbose] [<remote-repository> [<branch>]]
git fetch [-v] [<remote-repository> [<branch>]]
- If
<branch>
is omitted, the currently-checked-out branch will be fetched. - If
<remote-repository>
is omitted, the currently-configured remote repository (usually tracked by the nameorigin
) will be pulled from.
Pull updates from the remote repository and merge them into your local working copy (https://git-scm.com/docs/git-pull):
git pull [--verbose] [<remote-repository> [<branch>]]
git pull [-v]
- If
<branch>
is omitted, the currently-checked-out branch will be pulled and merged. - If
<remote-repository>
is omitted, the currently-configured remote repository (usually tracked by the nameorigin
) will be pulled from.
Push committed changes on the current branch from your local working copy to the remote repository (https://git-scm.com/docs/git-push):
git push [--verbose] [<remote-repository> [<branch>]]
git push [-v]
- If
<branch>
is omitted, the currently-checked-out branch will be pushed. - If
<remote-repository>
is omitted, the currently-configured remote repository (usually tracked by the nameorigin
) will be pushed to.
5. Adding, moving, removing, ignoring, diffing, staging and committing files in your local repository: Recording Changes to the Repository
Ignore files (https://git-scm.com/docs/gitignore):
Add path-specs to .gitignore
file.
Stage new/changed files to be committed (https://git-scm.com/docs/git-add):
git add [--verbose] <path-spec> [<path-spec>...]
git add [-v] <path-spec> [<path-spec>...]
Stage all new/changed files within the repository directory and subdirectories (https://git-scm.com/docs/git-add):
git add [--verbose] --all
git add [-v] -A
Move/rename a file (https://git-scm.com/docs/git-mv):
git mv [--verbose] <source-file> <destination-file>
git mv [-v] <source-file> <destination-file>
Remove/delete a file (https://git-scm.com/docs/git-rm):
git rm <file>
Recursively remove/delete a directory (https://git-scm.com/docs/git-rm):
git rm -r <directory>
Show the current status of added/modified/moved/renamed/removed/staged files (https://git-scm.com/docs/git-status):
git status
- If the
--verbose
or-v
option is added, it will show a diff of staged files.
Show modifications to files (https://git-scm.com/docs/git-diff):
git diff [--staged|--cached] [<file>]
- If
--staged
and--cached
are omitted, only the differences for un-staged files will be shown. - If
--staged
or--cached
is specified, only the differences for staged files will be shown. - If
<file>
is omitted, the differences for all files will be shown (according to whether--staged|cached
is specified).
Commit staged files to the local repository (https://git-scm.com/docs/git-commit):
git commit [--verbose] --message="summary" [--message="details"]
git commit [-v] -m "summary" [-m "details]
Show commit logs (https://git-scm.com/docs/git-log):
git log
6. Tagging
Create a tag of the current code in the currently-checked-out branch (https://git-scm.com/docs/git-tag):
git tag [--annotate] <tagname> [<checksum>] [--message="tag message"]
git tag [-a] <tagname> [<checksum>] [-m "tag message"]
- Specify part of the checksum to tag a specific previous commit.
- Omit both
-a
and-m
to create a lightweight tag instead of an annotated tag.
7. Branching: Branches in a Nutshell
Create a branch from the current code in the currently-checked-out branch (https://git-scm.com/docs/git-branch):
git branch [--verbose [--verbose]] <branch-name>
git branch [-v|-vv] <branch-name>
List available branches in the repository (https://git-scm.com/docs/git-branch):
git branch [--verbose [--verbose]] [--list] [--all]
git branch [-v|-vv] [-a]
- If
--all
is omitted, only local branches will be shown.
Check out a branch to work on locally (https://git-scm.com/docs/git-checkout):
git checkout <branch-name>
8. Merging: Basic Branching and Merging
Check out the source branch (https://git-scm.com/docs/git-checkout):
NOTE: The source branch must exist locally, so the simplest way to do that is to check it out.
git checkout <source-branch>
Make sure the source branch is up to date (https://git-scm.com/docs/git-pull):
git pull [--verbose] [<remote-repository> [<branch>]]
git pull [-v]
- If
<branch>
is omitted, the currently-checked-out branch will be pulled and merged. - If
<remote-repository>
is omitted, the currently-configured remote repository (usually tracked by the nameorigin
) will be pulled from.
Check out the destination branch, into which the merge will be made (https://git-scm.com/docs/git-checkout):
git checkout <destination-branch>
Merge the source branch into the currently-checked-out working code (https://git-scm.com/docs/git-merge):
git merge [--verbose] [--no-commit|-m "message"] <source-branch>
git merge [-v] [--no-commit|-m "message"] <source-branch>
- If
--no-commit
is omitted, the merge will be automatically committed if successful.
Show the current status of added/modified/moved/renamed/removed/staged files (https://git-scm.com/docs/git-status):
git status
- If the
--verbose|-v
option is added, it will show a diff of staged files.
Show modifications to files (https://git-scm.com/docs/git-diff):
git diff [--staged|cached] [<file>]
- If
--staged
and--cached
are omitted, only the differences for un-staged files will be shown. - If
--staged
or--cached
is specified, only the differences for staged files will be shown. - If
<file>
is omitted, the differences for all files will be shown (according to whether--staged|cached
is specified).
Commit staged files to the local repository (https://git-scm.com/docs/git-commit):
git commit [--verbose] --message="commit message"
git commit [-v] -m "commit message"
Push committed changes on the current branch from your local working copy to the remote repository (https://git-scm.com/docs/git-push):
git push [--verbose] [<remote-repository> [<destination-branch>]]
git push [-v]
- If
<destination-branch>
is omitted, the currently-checked-out branch will be pushed. - If
<remote-repository>
is omitted, the currently-configured remote repository (usually tracked by the nameorigin
) will be pushed to.