Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

 

1. IDENTITY & SETUP

a. git config --global user.name "FirstName LastName"

To set your name for identification

 

b. git config --global user.email [email protected]

To set your email for identification

 

c. git config --list

A quick way to check all configurations

 

TIP 1: If you don't use --global, the configuration will be valid to the current project only.

TIP 2: Similarly, you can set other options like a default text editor, default branch ...

 

 

2. PROJECT INITIATION, STAGING & SNAPSHOT

a. git init

Initialize a local Git Repository on the current directory

 

b. git clone

Create local copy of remote repository where URL can resemble ssh://[email protected]/[username]/[repository-name].git

 

c. git status

Check status

 

d. git add [file-name.txt]

Add a file to the staging area

OR git add -A - Add all new and changed files to the staging area

 

e. git reset [file-name.txt

Remove a file from the staging area

 

e. git commit -m "[commit message]"

Commit changes to create a new snapshot

 

 

3. BRANCH AND MERGE

a. git branch

List branches (asterisk symbol will appear next to the active branch currently)

 

b. git branch -a

List all branches (local and remote)

 

c. git branch [branch name]

Create a new branch at the current commit

 

d. git branch -d [branch name]

Delete a branch with a matching branch name

 

e. git push origin --delete [branch name]

Delete a remote branch with a matching branch name

 

f. git checkout -b [branch name]

Create a new branch and switch to it

 

g. git checkout -b [branch name] origin/[branch name]

Clone a remote branch and switch to it

 

h. git branch -m [old branch name] [new branch name]

Rename a local branch

 

i. git checkout [branch name]

Switch to a branch with a matching branch name

 

j. git checkout -

Switch to the branch last checked out

 

k. git checkout -- [file-name.txt]

Discard changes to a file

 

l. git merge [branch name]

Merge a branch into the active branch

 

m. git merge [source branch] [target branch]

Merge a branch into a target branch

 

[TEMPORARY COMMITS]

 

a. git stash

Stash changes in a dirty working directory

 

b. git stash list

List stack-order of stashed file/changes

 

c. git stash pop

Write working from the top of stash stack

 

d. git stash drop

Discard the changes from the top of the stash stack

 

 

4. SHARING AND UPDATING PROJECT

a. git push origin [branch name]

Push a branch to your remote repository

 

b. git push -u origin [branch name]

Push changes to the remote repository (and remember the branch)

 

c. git push

Push changes to the remote repository (remembered branch)

 

d. git push origin --delete [branch name]

Delete a remote branch

 

e. git pull

Update local repository to the newest commit

 

f. git pull origin [branch name]

Pull changes from remote repository

 

g. git remote add

Add a remote repository

 

h. git remote set-url

Set a repository's origin branch to URL

 

 

5. INSPECTING & COMPARISON

a. git log

View changes

 

b. git log --summary

View changes (detailed)

 

c. git log --oneline

View changes (briefly)

 

d. git diff [source branch] [target branch]

Preview changes before merging