目录索引
- 创建 ssh key
- 检查是否存在 ssh key
cd ~/.ssh
ls
如果已经存在id_rsa.pub 或 id_dsa.pub说明已经有证书。
- 新建ssh key
ssh-keygen -t rsa -C "your_email@example.com"
代码参数含义:
-t 指定密钥类型,默认是 rsa ,可以省略。
-C 设置注释文字,比如邮箱。
-f 指定密钥文件存储文件名
然后直接Enter就行。
- 查看公钥
cat ~/.ssh/id_rsa.pub
- 删除本地分支
git branch -d <BranchName>
- 创建并切换到分支
git checkout -b <myBranchName> <fromBrach>
fromBrach不写默认本分支
- 代码回滚
git reset --hard commitId
- 合并分支
git merge origin/master
其中origin/master为你要合并过来的分支。
- 终止合并
git merge --abort
- 切换代码源
- 删除原远程分支源
git remote rm origin
- 添加新的远程源
git remote add origin 远程仓库地址
然后下面的操作了
git pull origin master --allow-unrelated-histories
git branch --set-upstream-to=origin/master master
git push
- 合并不同源的代码
查看下源的状态
git remote -v
添加另一个源
git remote add upstream <url>
再次查看下配置是否成功
git remote -v
同步upstream的代码
git fetch upstream
代码合并到本分支
git merge upstream/v1.0
提交代码
git push
- 变基
git rebase upstream/v1.0
git rebase和git merge的区别就是git rebase依然会保证提交顺序,git merge会从分支覆盖过来。
所以git merge的提交线是不停的新分支拉出去再合并,而git rebase的提交线是一条直线。
- cherry-pick
git cherry-pick <commitHash>
将这个一笔提交转到当前分支
- 接上一笔提交
在上一笔提交中接着提交
git commit --amend
在上一笔提交中接着提交,覆盖提交用户信息
git commit --amend --reset-author
- 提交合并
// 合并从当前head到15f745b(commit id)
git rebase -i 15f745b
// 合并最近的两次提交
git rebase -i HEAD~2
弹出来修改记录后将pick改成s就不会有提交记录了。
- git pull --rebase
git pull origin master --rebase
- 推送代码
git push origin master
- 撤销修改
git checkout -- CONTRIBUTING.md
- 命令保存和恢复进度
保存当前分支的开发进度
git stash
恢复当前分支的开发进度
git stash pop
- 最后一条commit撤回到未提交的状态
git reset --soft HEAD^
- 远程强制覆盖本地
git fetch --all
git reset --hard origin/分支名
git pull
- 更改某次提交
-
将HEAD移到需要提交的commit上
git rebase 提交ID^ --interactive
然后按i,进入编辑模式。找到对应的commit,将首行的
pick
改成edit
,按esc
,按:wq
退出。 -
修改的文件
-
git add .
添加文件到缓存 -
git commit --amend
追加提交 -
git rebase --continue
-
冲突解决
1、编辑冲突文件,解决冲突 2、git add . 3、git commit --amend 4、git rebase --continue
- 反转提交
git revert <commit_id>