配置命令

全局设置提交代码时的用户信息
git config --global user.name "[name]"
git config --global user.email "[email address]"

新建代码库

在当前目录新建一个Git代码库
git init

从已存在的仓库克隆
git clone xxx.git [dist folder]

增加文件

添加指定文件到暂存区
git add [file1] [file2] ...

添加当前目录的所有文件到暂存区
git add .

代码提交

提交暂存区到仓库区
git commit -m [message]

分支

列出所有本地分支
git branch

列出所有远程分支
git branch -r

新建一个分支,但依然停留在当前分支
git branch [branch-name]

新建一个分支,并切换到该分支
git checkout -b [branch]

切换到指定分支,并更新工作区
git checkout [branch-name]

建立追踪关系,在现有分支与指定的远程分支之间
git branch --set-upstream [branch] [remote-branch]

合并指定分支到当前分支
git merge [branch]

删除分支
git branch -d [branch-name] // 合并后删除
git branch -D branch-name // 强制删除(未合并过的分支使用`-d`无法删除)


删除远程分支
git push origin --delete [branch-name]

git push origin :branch-name

分支重命名
git branch -m old-branch new-branch-name

查看帮助
git help branch

查看信息

显示有变更的文件
git status

显示当前分支的版本历史
git log

查看命令历史
git reflog

显示暂存区和工作区的差异
git diff

远程同步

查看远程库信息
git remote -v

在本地创建和远程分支对应的分支
git checkout -b branch-name origin/branch-name(本地和远程分支的名称最好一致)

建立本地分支和远程分支的关联
git branch --set-upstream branch-name origin/branch-name

取回远程仓库的变化,并与本地分支合并
git pull [remote] [branch]

上传本地指定分支到远程仓库
git push [remote] [branch]

撤销

恢复暂存区的指定文件到工作区
git checkout [file]

恢复暂存区的所有文件到工作区
git checkout .

重置暂存区与工作区,与上一次commit保持一致
git reset --hard commit_id

当改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令
git checkout [file]
不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,

第一步用命令
git reset HEAD [file]
第二步用命令
git checkout [file]

删除文件

删除工作区文件,并且将这次删除放入暂存区
git rm [file]

改名文件,并且将这个改名放入暂存区
git mv [file-original] [file-renamed]

保存现场

保存现场
git stash

查看现场列表
git stash list

恢复现场,并删除暂存
git stash pop

恢复现场
git stash apply