git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
git config --global core.editor emacs
# 查看当前git用户
git config user.name
Push an existing folder:
cd existing_folder
git init
git remote add origin <git@gitlab.xxx.cn:xxx/xxx.git>
git add .
git commit -m "Initial commit"
git push -u origin master
Push an existing Git repository:
cd existing_repo
git remote rename origin old-origin
git remote add origin <git@gitlab.xxx.cn:xxx/xxx.git>
git push -u origin --all
git push -u origin --tags
git clone -b <branch> <git@gitlab.xxx.cn:xxx/xxx.git>
# stage file:
git add <files>
# show status
git status -s
# commit
git commit -m "<commit-message>"
# show modify
git diff
# show modify with last staged
git diff --staged
# show latest two commit log
git log -p -2
# show history commit state
git log --stat
# show history commit in one line
git log --pretty=oneline
# show history commit with branch and graph info
git log --oneline --decorate --graph --all
# show commit changes
git show <commit-id>
# cancel stage
git reset HEAD CONTRIBUTING.md
# revert modify
git checkout -- CONTRIBUTING.md
# show all remote registory
git remote -v
# add remote registory
git remote add <shortname> <url>
# show all tag
git tag
# add tag
git tag <tag-name> <commit-id>
# push tag or push all tag
git push origin <tag-name>
git push origin --tags
# delete local tag
git tag -d <tag-name>
# delete oringin tag
git push origin --delete <tag-name>
# checkout tag
# usually checkout a new branch base on tag
git checkout -b <branch-name> <tag-name>
# create new branch and checkout
git checkout -b <new-branch>
# equal
git branch <new-branch>
git checkout <new-branch>
# create new branch and track remote branch
git checkout -b <new-branch> --track origin/<remote-branch>
# delete branch
git branch -d <branch>
# show merged branch
# no “*” means that the branch is merged, it can be delete safely
git branch --merged
# push to remote
git push origin <local-branch>:<remote-branch>
git checkout experiment
git rebase master
# equal
git rebase <basebranch> <topicbranch>
# rebase when pull
git pull --rebase
# equal
git fetch
git rebase <remote-branch>
# stash modify
git stash save
# list stash
git stash list
# apply stash
git stash apply stash@{2}
新增的文件不会被stash,需要先 git add
添加到版本控制之中
# 添加子模块
git submodule add <url>
# 更新子模块
modify .gitmodules
git submodule sync
git submodule update --recursive
# 删除子模块
删除项目源码子目录
删除 .gitmodules 文件中 submodule 的信息
删除 .git/config 文件中 submodule 的信息
删除 .git/modules/ 目录下 sumodule 的子目录
$ git init
$ git remote add -f origin https://github.com/alibaba/druid.git
$ git config core.sparsecheckout true
$ echo "doc/user/demo" >> .git/info/sparse-checkout
$ git pull origin master
# 撤销git commit,撤销git add,保留编辑器改动代码
$ git reset --mixed <commit ID> <file>
$ git reset <commit ID> <file>
# 撤销git commit,不撤销git add,保留编辑器改动代码
$ git reset --soft <commit ID> <file>
# 撤销git commit,撤销git add,删除编辑器改动代码
# 慎用!!!
$ git reset --hard <commit ID> <file>
git commit --amend
# 生成 patch
git diff -u a/xxx b/xxx > diff.patch
# 应用 patch
git patch a/xxx < diff.patch
git diff:
git patch: