入门git-笔记2
官方中文文档 https://git-scm.com/book/zh/v2
推荐学习
git小游戏 https://learngitbranching.js.org/
git 系列命令
温馨提示:文章内有大图,谨慎浏览
当你本地想建立一个本地仓库(repo),并与远程仓库进行同步时1
2
3
4
5
6
7
8
9
10
11
12
13
14
15# 初始化
mkdir repo
cd repo
git init
# 添加文件操作。。。
# 提交到本地仓库
git add .
git commit -m "commit description"
# 接下来,连接远程仓库(如果在github或gitee创建个空仓库时,它会提醒你如何将本地仓库和远程仓库连接,username体寒为你账号的用户名,xxx为你的仓库名)
git remote add origin [email protected]:username/xxx.git
# 将本地变更提交到远程仓库,后续还需要提交只需要运行 git push 即可
git push -u origin master
git clone
1 | # 示例 |
git add
1 | # 常用,将所有变更添加到缓存区 |
git commit
1 | # 常用,将缓存区的文件提交到本地仓库 |
git push
1 | # 常用,将本地仓库 master 分支提交至远程仓库 master 分支 |
git branch
1 | # 创建 foo 分支 |
git fetch
1 | # 获取远程仓库所有最新代码 |
git merge
1 | # 合并两个分支 |
git pull
1 | # 相当于 git fetch + git merge |
git rm
1 | # 删除本地文件,并添加到暂存区 |
git cherry-pick
1 | # 与git rebase 类似,不过这个命令可以自定义选择的提交记录进行复制到指定位置,rebase只能将指定分支前的提交记录复制到指定位置 |

git rebase
1 | # 变基操作 |
git reset
1 | # 撤销变更 |
git log
1 | # 查看历史提交记录 |
rebase和merge
引用自:https://learngitbranching.js.org/?locale=zh_CN
以下是关于 rebase 的优缺点:
优点:
- Rebase 使你的提交树变得很干净, 所有的提交都在一条线上
缺点:
- Rebase 修改了提交树的历史
比如, 提交 C1 可以被 rebase 到 C3 之后。这看起来 C1 中的工作是在 C3 之后进行的,但实际上是在 C3 之前。
一些开发人员喜欢保留提交历史,因此更偏爱 merge。而其他人(比如我自己)可能更喜欢干净的提交树,于是偏爱 rebase。
git config –list –show-origin
展示当前个人git配置