Git cheatsheet

Common git commands

make directory a git repository

1
git init

Adds files in the to the staging area for Git.

1
git add <file or directory name>

Adding a commit with message

1
git commit -m "Commit message"

current state of the repository

1
git status

Create a new branch

1
git branch <branch name>

Checkout an existing branch

1
git checkout <branch name>

Merge changes into current branch

1
git merge <branch name>

Add remote repository

1
2
3
4
git remote <command> <remote_name> <remote_URL>

# connect remote repo to the local repo, the remote repo has name origin, followed by its URL
git remote add origin git@account_name.git.beanstalkapp.com:/acccount_name/repository_name.git

get the latest version of a repository

1
git pull <branch_name> <remote_URL/remote_name>

Sends local commits to the remote repository

1
2
3
4
5
6
7
8
9
10
11
# push local commits to a remote repo
git push <remote_URL/remote_name> <branch>

# push local branch MASTER to remote repo ORIGIN,
git push origin master

# push all local branches to remote repo
git push -all origin

# -u flag will keep track all pushes so next time you can just use git push
git push -u origin master

show the chronological commit history for a repository

1
git log

make a copy to another remote repo

1
git fork

make a copy from a remote repo to the a local repo

1
git clone

Temporarily save local changes before pulling from remote repo (local changes are not ready for commit)

1
2
# stash basically has the same logic as commit, git will just save the changes in different places so it will not appear in your commit history
git stash save

Reapply local changes after pulling

1
git stash pop

change the starting poing of a branch

1
2
# this is make the commit history looks cleaner. But will cause problems when working with others because when you do it, your branch's starting poing is different to others and becomes a different branch
git rebase

git squash

combine multiple commits into one commit, make the commit history looks cleaner