$ git clone <repository_url>
Firstly, create a repository on Github and get your_repo_url. Secondly, go to your local project directory and initialize git:
$ git init
$ git add README.md
$ git status
$ git add <file1> <file2> ...
$ git rm <file1> <file2> ...
$ git status
$ git commit -m "your_comment"
# 'origin' means the remote end of your repository
$ git remote add origin <your_repo_url>
# use '-u' for the first time, it can be ignored afterwards
$ git push -u origin <master_or_other_branch>
$ git checkout -- <file1> ...
$ git reset HEAD <file1> ...
$ git reset HEAD~1
# show current HEAD and its ancestry
$ git log --oneline
# draw graph of branch structure
$ git log --graph --pretty=oneline --abbrev-commit
# shows the entire history
$ git reflog
# where commit_id can be found in the log/history
$ git reset --hard <commit_id>
# show a list of both local and remote branches
$ git branch -a
# create a local branch from a remote branch
$ git checkout -b <new_local_branch> <remote_branch>
# checkout or switch to an existing branch
$ git checkout <branch_name>
$ git merge <branch_name>
# delete a branch
$ git branch -d <branch_name>
$ git remote -v
# check if the branch gets updated
$ git remote -v update
# show the original information of the cloned repository
$ git remote show origin
# push current local branch to remote
$ git push origin <branch_name>
$ git pull
$ git branch --set-upstream-to=<origin/branch_name> <branch_name>
# default will tag HEAD
$ git tag <tag_name>
$ git tag <tag_name> <commit_id>
# show tag list
$ git tag
# add tag message
$ git tag -a <tag_name> -m "your tag message" <commit_id>
# show tag information
$ git show <tag_name>
$ git push origin <tag_name>
$ git push --tag
# show diff of the file that hasn't been git-added
$ git diff file
# show diff of the added file
$ git diff --cached file
# update all local branches set to track remotes ones, but not merge any changes in
$ git remote update
# update the branch currently you're on, but not merge any changes in
$ git fetch
# update AND merge the remote changes to your current branch
$ git pull
# check if upstream is set correctly
$ git remote -v
# fetch changes on the upstream
$ git fetch upstream
# switch to your branch
$ git checkout <your_branch>
# merge the changes to your branch
$ git merge upstream/<your_branch>
$ git status
$ git branch -a
# push the merged local changes to your fork
$ git push origin <your_branch>
# save staged and unstaged changes in the stash stack, undoing thing to the latest commit
$ git stash
# stash including untracked files
$ git stash -u or $ git stash --include-untracked
# show all the stashes
$ git stash list
# show stash reference 1 in patch format
$ git stash show stash@{1} -p
# apply the latest stash on current branch and stash is removed from stack
$ git stash pop
# apply the stash reference 3 on current branch and stash remains in the stack
$ git stash apply stash@{3}
# clear all stashes
$ git stash clear
# delete stash reference 2
$ git stash drop stash@{2}
Git commands from Patrick Zahnd.
- Graphical view of branches
Add the following alias to the ~/.gitconfig, and running
git log1
orgit log2
will output a clear view of the relationship of branches.
[alias]
log1 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
log2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n''%C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
master (production) - hotfixes - releases - develop - features