git 备忘

0.记得用head head^ 如果不想手动复制粘贴commit id

1.删掉对文件的跟踪,而不删除文件本身

git rm --cached doc

另外, 已经tracked的文件,在.gitignore 标注后,依然是tracked的,此时就需要使用这个命令将 此文件untrack

2.记住pull push的remote 和 分支

git push -u origin master

3.在创建分支时可以指定源

#正常的创建分支
git branch test 
git checkout test
#或者
git checkout -b test

#如果你要指定从某个源(tag branch fetch过的remote branch  及提交hash值)
git branch test origin/test
git checkout -b  test origin/test

4.删除远程分支

git push origin :test

5.场景
一个个人的改动,不想提交,又不想每次git add .后手动git reset filepath

方法一:
修改.git/info/exclude ,具体格式同.gitignore

方法二:
如果是目录,则直接在目录下加个.gitignore文件,文件中写入/*容即可
注:如果.gitignore 同时中有!.gitignore则会有提交空目录的效果(正常情况下空目录并不会加入版本控制)

方法三:
对于已经track的文件,百度上可以搜到这个方法,不过感觉这个比较hack,只适合在极端状况下使用,另外这个命令 有个前提是 :文件不是从存在和存在间做切换

git update-index --assume-unchanged <PATH>
#恢复追踪的命令
git update-index --no-assume-unchanged <PATH>

参考:https://segmentfault.com/q/1010000000430426

6.git reset git checkout

用在commit id 上:

git reset commit_id # 移动head
git reset --hard commit_id #本地代码切换到commit_id上
git reset --soft commit_id #本地代码不变,只是把head 换到 commit_id 上,diff staged
git reset commit_id #本地代码不变,只是把head 换到 commit_id 上,diff 没有staged
如果对git reset 的那个commit 结果 做 git diff 的结果会比较诡异,相当于改写了commit_id 对应的commit 的历史

git checkout commit_id #把commit id 的代码拿出来放在一个没有名字的新branch 上,就和checkout branch_name 是差不多

用在文件上:
git reset file_name # 用 head中的文件的覆盖staged,staged 换到workspace
git checkout file_name # 用staged 覆盖当前文件

在这里插入图片描述

7.git revert

git reset 的区别:
reset 是 commit_id 后的提交都会被删除,如同a[3:]
但是revert,可以指定范围,如同a[3:5]
另外,
git revert 会生成新的提交,而 git reset 后往往 要 git push -f

另外就是 revert 的参数:

git revert commit_id_1..commit_id_2

下面是一个例子,最后的三个提交分别增加了 1.txt 2.txt 3.txt这三个文件,我们想revert 1.txt 和 2.txt 这两次提交

$ ls
1.txt    2.txt    3.txt		file.txt
$ git log
commit 378c34e402e57aa06e53649bfb5b6ab6ceedca7f
Author: harryhare <harryhare@xxx.com>
Date:   Thu Oct 25 18:18:41 2018 +0800

    add 3.txt

commit 83522a6da0d47fa1803905524e1a9aee9dd381ef
Author: harryhare <harryhare@xxx.com>
Date:   Thu Oct 25 18:18:26 2018 +0800

    add 2.txt

commit e52180755b91f1e0dda6dc6abbdecda25141f484
Author: harryhare <harryhare@xxx.com>
Date:   Thu Oct 25 18:18:12 2018 +0800

    add 1.txt

commit 63d6fe068d4ad7ffe17a7a1ba2ab21ad5beb1422
Author: harryhare <harryhare@xxx.com>
Date:   Thu Oct 25 18:15:56 2018 +0800

    add 333
$ git revert 63d6f..83522
[master 8860251] Revert "add 2.txt"
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 2.txt
[master f32e9c1] Revert "add 1.txt"
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 1.txt
$ ls
3.txt		file.txt

两个需要注意的地方,log 下方的 commit 是在 .. 前的,也就是 先做的commit .. 后做的提交
另外就是 63d6f 本身是没有被 revert 的,有点 array.end() 的 不包含的感觉。不过,从 git log 上看就很直观, 只是commit_id1 和 commit_id_2 两行之间的log 对应的内容都会被删除
如果不想分别提交两个revert,可以加参数 -n(no-commit)

$ git revert -n 63d6..8352
$ git commit
[master 3856390] Revert "add 1.txt""add 2.txt"
 2 files changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 1.txt
 delete mode 100644 2.txt

8.git rebase 的坑

由于git rebase master后再提交,从master merge branch会是fast forward,于是我入了坑。
但是当一个开发branch ,久久不能merge,rebase就成了痛苦。
所以 和git merge 相比,有不同的使用场景
如果是小的fix,可以用rebase,就像前面说的那样,没有额外的merge commit,master 上merge 是fast forward 的。这里还有一个问题,如果是自己的branch 在checkout后 某个共同的地方被 master 多次共同修改,则每个master上的改动都需要手动解决冲突,然后再git rebase --continue
所以如果是很长时间不能并入master的分支,则经常merge master branch 是更好的选择。

9.查找自己有关的提交

git log --author harryhare

10.创建 pull request 是平台相关的命令,git 中并没有相应的操作,有个长的很像的 git request-pull,但是却是不同的功能

Generates a summary of pending changes

git request-pull master git@github.com:harryhare/test2.git test
The following changes since commit 310dfab62006a4ab821672cac23d5494f1ae6de3:

  add 10.txt (2018-10-26 10:33:28 +0800)

are available in the git repository at:

  git@github.com:harryhare/test2.git test

for you to fetch changes up to 310dfab62006a4ab821672cac23d5494f1ae6de3:

  add 10.txt (2018-10-26 10:33:28 +0800)

----------------------------------------------------------------

11.切换用户

如果是用ssh 方式,切换key就可以了
windows下key的位置在C:\Users\harryhare\.ssh

至于 config 中的 nameemail 应该只是用来显示的

12.擅长本地缓存的远程分支
git branch -a 或者git branch -r 时会有些远程已经删除的分支。

git remote prune origin

或者

git fetch -p
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值