Skip to content Skip to main navigation Skip to footer

Git Cheatsheet

Configuration

Configure global user information

# These changes will be reflected in ${HOME}/.gitconfig
$ git config --global user.name "Gitter Jenkins"
$ git config --global user.email "gitter.jenkins@build-masters.com"

Initializing

Create a new local repository

git init

#
$ mkdir hello-world
$ cd hello-world

$ git init
Initialized empty Git repository in /home/user/hello-world/.git/

# Simple example: add a file and commit changes
$ echo "Hello World" > README.md
$ git add README.md
$ git commit -m "Initial commit"
[master (root-commit) f20eb2c] Initial commit
1 file changed, 1 insertion(+)
create mode 100644 README.md

Cloning an Existing Repository

git clone <REPOSITORY URL>

# Clone repository
$ git clone https://github.com/git/git.git
Cloning into 'git'...
remote: Enumerating objects: 248, done.
remote: Counting objects: 100% (248/248), done.
remote: Compressing objects: 100% (94/94), done.
remote: Total 295773 (delta 165), reused 229 (delta 154), pack-reused 295525
Receiving objects: 100% (295773/295773), 146.82 MiB | 1.07 MiB/s, done.
Resolving deltas: 100% (220334/220334), done.

Day-to-day activities

Show the working tree status

git status

#
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

Stage all changes in file or directory

git add <file|directory>

Show changes

git diff 

# Show changes between working directory and staging area
$ git diff

# Show changes between the staging area and the repository
$ git diff --staged

# Show changes between two commits
$  git diff f20eb2c 2013e88
diff --git a/README.md b/README.md
index 557db03..fefab07 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
-Hello World
+Hello World!

# Show changes between two branches
$ git diff master dev

Commit changes

git commit -m <message> [file|directory]

#
$ git commit -m "Initial commit"
[master (root-commit) f20eb2c] Initial commit
1 file changed, 1 insertion(+)
create mode 100644 README.md