Skip to content

Instantly share code, notes, and snippets.

@sksundram
Created September 9, 2019 13:52
Show Gist options
  • Save sksundram/0a6162ac4aea2bfaf174cd08f2977cfa to your computer and use it in GitHub Desktop.
Save sksundram/0a6162ac4aea2bfaf174cd08f2977cfa to your computer and use it in GitHub Desktop.
Learn Git

1 - Basics

git help

git help config

git config --global user.name "Shailesh Kumar"

git config --global user.email "shailesh.sundram@gmail.com"

git config --global color.ui true // Pretty command line colors


md store && cd $_

git init // Create local git repo

touch README.txt

git status

git add README.txt

git commit -m 'Create a README'


Edit README.txt and touch LICENSE

git add --all // Adds all new or modified files to stage

git commit -m 'Add LICENSE and finish README'

git log

Git commit messages should be in present tense


git add <list of files> Add the list of files

git add --all Add all files

git add *.txt Add all txt files in current directory

git add docs/*.txt Add all txt files in docs directory

git add docs/ Add all files in docs directory

git add "*.txt" Add all txt files in whole project


2 - Staging and Remotes

git diff Show unstaged differences since last commit

git diff --staged Show staged differences

git reset HEAD <file> To unstage a file

HEAD refers to the last commit on the current branch

To reset the state of the file before last commit:

git checkout -- <file> // e.g: git checkout -- LICENSE

git commit -a -m "Modify README // Add changes from all tracked files and commit them

git reset --soft HEAD^ // Undo last commit and put changes into staging

The caret ^ in HEAD^ says - move to commit which is one before the current HEAD


What if we forgot to add a file?

touch todo.txt

git add todo.txt

git commit --amend -m "Modify README and add todo.txt" // Add to last commit and overwrite the commit message

git reset --hard HEAD^ // Undo last commit and discard all changes

git reset --hard HEAD^^ // Undo last 2 commits and discard all changes


To add new remotes git remote add <remote name> <remote address>

To remove remotes git remote rm <remote name>

To push to remotes git push -u <remote name> <branch>

-u stands for upstream so that next time you just need to type git push and it will automatically push to that branch


3 - Cloning and Branching

git clone <repo address>

git clone <repo address> <your local folder name>

git remote -v // List all remotes


git branch cat // Create a new branch named cat

git checkout cat // move to the cat branch, HEAD in now on cat branch

echo "Schrodinger" > cat.txt

git add cat.txt

git commit -m 'Create quantum cat' // "committed" to the cat branch


Merge branch git checkout master

git merge cat // Fast-forward merge - when nothing was modified on the branch (master here) which merges another branch

git branch -d cat // Delete cat branch

How to restore a deleted branch? read this

git checkout -b admin // Creates an admin branch and checks it out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment