Git常用命令

基础配置

配置用户名

git config --global user.name "Your name"

配置邮箱

git config --global user.email "email@example.com"

配置颜色

git config --global color.ui true

配置命令别名

git config --global alias.alias_name command_name

正向流程

创建git仓库

git init

工作区增加或修改的文件,加入暂存区

git add file

暂存区提交到本地仓库当前分支

git commit
git commit -m "comment"

删除操作加入暂存区

git rm file

删除操作提交本地仓库当前分支

git commit

查看信息

查看git状态

git status

查看文件差异

git diff <file>
git diff HEAD -- file

查看git日志

git log
git log --pretty=oneline
git log --graph --pretty=oneline --abbrev-commit

查看git执行命令历史

git reflog

反向流程

本地仓库当前分支版本回退(HEAD表示当前分支最新版本,后面n个^表示最新版本向前退n个版本,~m表示m个^

git reset --hard HEAD
git reset --hard HEAD^
git reset --hard HEAD~100
git reset --hard commit_id

工作区文件撤销修改(已加入到暂存区则还原到暂存区状态,否则还原到当前分支HEAD版本)

git checkout -- file

远程仓库

本地仓库关联远程仓库

git remote add origin remote_repo

本地仓库当前分支更改推送远程仓库(-u关联本地仓库分支与远程仓库分支)

git push -u origin master
git push origin master

本地克隆远程仓库

git clone remote_repo

查看远程仓库

git remote
git remote -v

克隆远程仓库分支

git checkout -b branch_name remote_repo/branch_name

抓取并合并远程仓库更新

git pull

设置本地仓库分支的关联远程分支

git branch --set-upstream branch_name remote_repo/branch_name
git branch --set-upstream-to remote_repo/branch_name branch_name
git branch --set-upstream-to=remote_repo/branch_name branch_name

推送当前分支到远程仓库

git push --set-upstream remote_repo branch_name

分支操作

查看分支

git branch

创建分支

git branch branch_name

基于某分支创建分支

git checkout -b branch_name base_branch_name

切换分支

git checkout branch_name

创建且切换分支

git checkout -b branch_name

合并某分支到当前分支

git merge branch_name
git merge --no-ff -m "comment" branch_name

删除分支

git branch -d branch_name
git branch -D branch_name

删除远程分支

git push remote_repo --delete branch_name

工作区操作

保存工作区现场

git stash

查看保存现场列表

git stash list

回复工作区现场

git stash apply
git stash pop
git stash apply stash_id

删除工作区现场

git stash drop

标签操作

查看标签

git tag
git show tag_name

创建标签

git tag tag_name
git tag tag_name commit_id
git tag -a tag_name -m "comment" commit_id

删除标签

git tag -d tag_name

推送标签到远程仓库

git push remote_repo tag_name
git push remote_repo --tags

删除远程仓库标签

git push remote_repo :refs/tags/tag_name