git 常用操作

October 10, 2018

创建 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>

忽略已经提交的文件或文件夹

git add .
git rm --cached file_path
git commit -m 'delete remote somefile'
git push

操作还原

有时候提交了一笔由于各种操作导致丢失了。 1、查找操作记录

git reflog

下面是操作日志:

31b7aab (HEAD -> mine) HEAD@{0}: commit: 添加围棋
31b7aab (HEAD -> mine) HEAD@{0}: commit: 添加围棋
b6c254f HEAD@{1}: commit: 删除五子棋背景图,加快加载速度
24ed419 (origin/mine) HEAD@{2}: commit: 添加几款游戏
0ecc527 HEAD@{3}: commit: 添加中国象棋
832fe12 HEAD@{4}: commit: 添加五子棋游戏

找到自己的那笔提交,然后 reset 下

git reset --hard b6c254f

提交规范

  • feat:新功能(feature)
  • fix:修补 bug
  • docs:文档(documentation)
  • style:格式(不影响代码运行的变动)
  • refactor:重构(即不是新增功能,也不是修改 bug 的代码动)
  • test:增加测试
  • chore:构建过程或辅助工具的变动

github 403 的解决办法

我使用了是有用的,不知道什么原因

git config --global --unset http.proxy
git config --global --unset https.proxy

Profile picture

Written by Vance who lives and works in Shenzhen, China, and is working hard to improve. You should follow them on csdn