Git Cheat Sheet

Git is a free and open-source distributed version control system.

Below listed are most commonly used git commands.


Setup

git config --global user.name "Danny Adams"

git config --global user.email "myemail@gmail.com"

Starting a Project with Git

Create a local repo

git init <directory>

Download a remote repo

git clone <url>


Make a Change

Add a file to staging

git add <file>

Stage all files

git add .

Commit all staged files to git

git commit -m "commit message"

Add all changes made to tracked files & commit

git commit -am "commit message"


Basic Git Concepts

main: default development branch

origin: default upstream repo

HEAD: current branch


Branches

List all local branches. Add -r flag to show all remote branches. -a flag for all branches.

git branch

Create a new branch

git branch <new-branch>

Switch to a branch & update the working directory

git checkout <branch>

Create a new branch and switch to it

git checkout -b <newbranch>

Delete a merged branch

git branch -d <branch>

Delete a branch, whether merged or not

git branch -D <branch>


Undoing Things

Move (&/or rename) a file & stage move

git mv <existing_path> <new_path>

Remove a file from working directory & staging area, then stage the removal

git rm <file>

Remove from staging area only

git rm --cached <file>

View a previous commit (READ only)

git checkout <commit_ID>

Create a new commit, reverting the changes from a specified commit

git revert <commit_ID>

Go back to a previous commit & delete all commits ahead of it (revert is safer). Add --hard flag to also delete workspace changes (BE VERY CAREFUL)

git reset <commit_ID>


Review your Repo

List new or modified files not yet committed

git status

List commit history, with respective IDs

git log --oneline

Show changes to unstaged files. For changes to staged files, add --cached option

git diff

Show changes between two commits

git diff commit1_ID commit2_ID


Pull

Fetch the remote repo's copy of the current branch, then merge

git pull

Move (rebase) your local changes onto the top of new changes made to the remote repo (for clean, linear history)

git pull --rebase <alias>

Upload local content to remote repo

git push <alias>

Upload to a branch (can then pull request)

git push <alias> <branch>


Thank you so much for going through this content

Hope you find this useful!