Skip to content

通用

  • ssh-keygen -t rsa -C "954241552@qq.com"
  • git config --global user.name "zhaoyz"
  • git config --global user.email 954241552@qq.com

https://git-scm.com/book/zh/v2

《Git版本控制管理》(第二版) 出版时间: 2015-3 豆瓣评分: 7.3(66人评价)
《Git权威指南》出版时间: 2011-6-27 豆瓣评分: 8.0(471人评价)
《GitHub入门与实践》出版时间: 2015-7 豆瓣评分: 7.8(496人评价)

https://www.liaoxuefeng.com/wiki/896043488029600

git commit 技巧
:star::bug::hammer: 可以显示图标

git init:将当前目录变成 git 可以管理的仓库
创建dev分支 : git branch dev
切换到dev分支 : git checkout dev
创建并切换到dev分支 : git checkout -b dev
删除dev分支 : git branch -d dev
推送dev分支 : git push origin dev
退出git log : 输入q

基本概念

git commit

修改 commit 信息: git commit --amend

git rebase

多次无用的 commit 就很让人不舒服。可能很多人觉得无所谓,无非是多了一些提交纪录。

导致的问题:

  • 不利于代码 review
  • 会造成分支污染: 如果有一天线上出现了紧急问题,你需要回滚代码,却发现海量的 commit 需要一条条来看

git rebase 如果冲突
需要先解决冲突
然后执行 git add -A
然后 git rebase --continue
然后才能 push

如何合并多次提交纪录

我们来合并最近的 4 次提交纪录,执行:

1
git rebase -i HEAD~4

git clone

git clone --depth=1 xxx

git clone --depth=1之后拉取其他分支:

1
2
3
$ git remote set-branches origin remote_branch_name
$ git fetch --depth 1 origin remote_branch_name
$ git checkout remote_branch_name

再重新切回 master:

1
2
$ git remote set-branches origin master
$ git pull origin master

push 失败

1
git pull --rebase
1
2
3
4
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

原因: 使用了 rebase, push 时需要加 -f 参数

The -f is actually required because of the rebase.
Whenever you do a rebase you would need to do a force push because the remote branch cannot be fast-forwarded to your commit.
You'd always want to make sure that you do a pull before pushing, but if you don't like to force push to master or dev for that matter, you can create a new branch to push to and then merge or make a PR.

https://stackoverflow.com/questions/39399804/updates-were-rejected-because-the-tip-of-your-current-branch-is-behind-its-remot

fetch

1
2
3
4
git fetch origin branch_xxxx
git branch new_brach_name_xxx FETCH_HEAD
git branch   
# 然后就能看到 new_brach_name_xxx 这个分支了,就是远程的 branch_xxxx 的修改