? - indicates optional arguments
Dangerous commands are marked with
-
Set username
$ git config --global user.name "<name>" -
Set email
$ git config --global user.email "<email address>"
ℹ️ Drop --global option to set local (repo specific) config.
-
Create new local repo
$ git init <?project-name> -
Clone existing
$ git clone <url> -
Create from existing code
$ git init
Add files & commit
$ git remote add origin <remote>
$ git push origin master
-
Add all changes for next commit
$ git add . -
List all new & modified files to be committed
$ git status -
Show unstaged changes
$ git diff -
Commit
$ git commit -m "<message>"
-
Fetch
$ git fetch -
Pull (fetch & merge)
$ git pull -
Push
$ git push <remote-name> <branch-name>
-
Get current branch name
$ git branch | grep \* | cut -d ' ' -f2 -
List all existing branches
$ git branch -av -
Create new branch
$ git branch <new-branch-name> -
Switch to branch
$ git checkout <branch-name> -
Carry changes to a new branch
$ git checkout -b <branch-name> -
Merge specified branch into current branch
$ git merge <branch-name> -
Delete branch
$ git branch -d <branch-name> -
Delete unmerged branch
$ git branch -D <branch-name>⚠️ -
Rename branch
$ git branch -m <new-branch-name># Rename current branch
$ git branch -m <old-branch-name> <new-branch-name># Rename another branch -
Rename remote branch (after above step) [2]
$ git push origin :old_branch# Delete the old branch
$ git push --set-upstream origin new_branch# Push the new branch, set local branch -
Reset branch w.r.t. master
$ git reset $(git merge-base master <your-branch>)
-
Show all remotes
$ git remote -v -
Add remote
$ git remote add <shortname> <url> -
Show remote information
$ git remote show <remote-name> -
Rename remote shortname
$ git remote rename <old-shortname> <new-shortname>
-
Delete file from working directory and stage the deletion
$ git rm <file> -
Remove file from version control but preserve it locally
$ git rm --cached <file>
- Show all commits in reverse chronological order
$ git log
-
Remove uncommitted changes
$ git checkout .⚠️ # use -f for force -
Change last commit
$ git commit --amend -
Undo last local commit [1]
$ git reset --soft HEAD~# soft -- keep your changes
$ git reset --hard HEAD^# hard -- discard changes -
Undo last published commit [1]
$ git revert HEAD -
Unstage a file
$ git reset HEAD <file> -
Unmodify a file
$ git checkout -- <file>⚠️ -
Remove untracked files
$ git clean -f⚠️ -
Remove untracked directories
$ git clean -d⚠️ -
Reset branch to remote
$ git reset --hard origin/<branch-name>
-
Create tag
$ git tag -a <tag> -m "<message>" -
Push all tags
$ git push origin --tags
- Delete all local branches that no longer have a remote
⚠️
$ git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d
-
Commited: data is safely stored in your local database
-
Modified: you have changed the file but have not committed it to your database yet
-
Staged: you have marked a modified file in its current version to go into your next commit snapshot
-
Tracked: are files that were in the last snapshot; they can be unmodified, modified, or staged
-
Untracked: files are everything else – any files in your working directory that were not in your last snapshot and are not in your staging area
-
Remote: Remote repositories are versions of your project that are hosted on the Internet or network somewhere
-
Origin: Default name Git gives to the server you cloned from
-
master: name given to default branch.
-
HEAD: pointer to current branch.
-
fast-forward: is a type of merge. When you try to merge one commit with a commit that can be reached by following the first commit’s history, Git simplifies things by moving the pointer forward because there is no divergent work to merge together.
-
Remote tracking branches: are references to the state of remote branches. They act as bookmarks to remind you where the branches in your remote repositories were the last time you connected to them.
-
Rebase: another type of integrating changes (the other one being
merge) from one branch into another by taking all the changes that were committed on one branch and replaying them on another.
- [1]: Undo a commit and redo http://stackoverflow.com/a/927386/2251156
- [2]: Updating your local branch's tracking reference to the new remote http://stackoverflow.com/a/16220970/2251156