Commands Cheat Sheet
A quick reference guide of common Git commands.
Initialization & Cloning
| Description | Command | Notes |
|---|---|---|
| Initialize in current directory | git init | Initializes a new Git repository in the current directory. |
| Initialize a new repository | git init <name> | Creates a new directory with the specified name and initializes it as a Git repository. Example: git init hello-world |
| Clone a repository | git clone <url> | Clones an existing repository. Example: git clone https://github.com/github/docs.git |
| Clone with SSH | git clone <url> | Clones using SSH for authentication. Example: git clone git@github.com:github/docs.git |
| Clone into specific directory | git clone <url> <directory> | Clones into a specified directory. Example: git clone https://github.com/github/docs.git github-docs |
info
HTTPS is common method for cloning repositories, while SSH is often used for contributors with write access. There is also a difference in URL format between the two methods.
| Protocol | Format | Example |
|---|---|---|
| HTTPS | https://<host>/<user>/<repository>.git | https://github.com/github/docs.git |
| SSH | git@<host>:<user>/<repository>.git | git@github.com:github/docs.git |
Configuration
info
--global applies the setting for all repositories for the current user.
Omitting it sets the configuration for the current repository only, but requires
being inside a Git repository.
| Description | Command | Notes |
|---|---|---|
| Set user name | git config --global user.name "<name>" | Sets the global username for commits. Example: git config --global user.name "adam" |
| Set user email | git config --global user.email "<email>" | Sets the global email for commits. Example: git config --global user.email "adam@example.com" |
| Set default text editor | git config --global core.editor <editor> | Sets the default text editor for Git. Example: git config --global core.editor /usr/bin/vim |
| Set default branch name | git config --global init.defaultBranch <name> | Sets the default branch name for new repositories. Example: git config --global init.defaultBranch main |
| Set alias | git config --global alias.<alias> <command> | Creates a shortcut for a Git command. Example: git config --global alias.cl clone |
| Remove alias | git config --global --unset alias.<alias> | Removes a previously set alias. Example: git config --global --unset alias.cl |
| View specific config value | git config --global user.name | Displays the value of a specific configuration setting. |
| View configuration | git config --global --list | Lists all Git configuration settings. |
| Edit configuration file | git config --global --edit | Opens the global config file in the default editor. |
Staging & Committing
| Description | Command | Notes |
|---|---|---|
| Check status | git status | Displays the status of the working directory and staging area. |
| Stage specific file | git add <file> | Stages a specific file. Example: git add README.md |
| Stage specific files | git add <file1> <file2> | Stages multiple specific files. Example: git add README.md index.html |
| Stage all changes | git add . | Stages all modified and new files. |
| Commit staged changes | git commit -m "<message>" | Commits staged changes with a message. Example: git commit -m "Initial commit" |
| Amend last commit | git commit --amend -m "<message>" | Amends the last commit with a new message. Example: git commit --amend -m "Updated commit message" |