# 从远程库克隆
git clone <url>
# 初始化一个Git仓库
git init
# 添加文件到Git仓库
git add <file>
git commit -m <message>
# 查看工作区的状态
git status
# 如有文件被修改,查看修改内容
git diff
# 版本回退
git reset --hard <commit_id>
# 查看提交历史,以便确定回到哪个版本
git log
# 查看命令历史,以便确定要回到未来的哪个版本
git reflog
# 想直接丢弃工作区的修改
git checkout -- <file>
# 丢弃暂存区的修改
git reset HEAD <file>
# 撤销本次提交(版本回退)
git reset --hard <commit_id>
# 关联一个远程库
git remote add <shortname> <url>
# 显示远程库的信息
git remote show <remote>
# 推送分支(branch)内容到远程库(remote)
git push <remote> <branch>
![](https://img2020.cnblogs.com/blog/2012154/202005/2012154-20200504124200509-147100953.png)
分支管理
# 查看分支
git branch
# 创建分支
git branch <name>
#切换分支
git switch <name> 或者 git checkout <name>
# 创建加切换分支
git switch -c <name> 或者 git checkout -b <name>
# 合并某分支到当前分支
git merge <name>
# 删除分支
git branch -d <name>
# 当Git无法自动合并分支时,就必须首先解决冲突。
# 解决冲突:把Git合并失败的文件手动编辑为我们希望的内容,再提交。
# 看到分支合并图
git log --graph
![](https://img2020.cnblogs.com/blog/2012154/202005/2012154-20200503100755867-1910113928.png)
# 普通合并模式 --no-ff 参数,禁用fast-forward
git merge --no-ff -m "merge with no-ff." dev
![](https://img2020.cnblogs.com/blog/2012154/202005/2012154-20200503112210161-12514902.png)
git stash # 隐藏工作现场
git stash pop # 回到工作现场
git cherry-pick <commit_id> # 把bug提交的修改“复制”到当前分支
![](https://img2020.cnblogs.com/blog/2012154/202005/2012154-20200503114939781-790338394.png)
# 查看远程库信息
git remote -v
# 从本地推送分支
git push origin branch-name
# 在本地创建和远程分支对应的分支
git checkout -b branch-name origin/branch-name
# 建立本地分支和远程分支的关联
git branch --set-upstream branch-name origin/branch-name
# 从远程抓取分支
git pull
# 用于新建一个标签,默认为HEAD,也可以指定一个commit id
git tag <tagname> <commit id>
# 指定标签信息
git tag -a <tagname> -m "blablabla..."
# 查看所有标签
git tag
# 推送一个本地标签
git push origin <tagname>
# 推送全部未推送过的本地标签
git push origin --tags
# 删除一个本地标签;
git tag -d <tagname>
# 删除一个远程标签
git push origin :refs/tags/<tagname>