Skip to content

git cheatsheet

wangyu- edited this page Oct 20, 2019 · 1 revision

=还原到某个版本=

git checkout 版本号

但是还原后只能看到那个时间点之前的版本了(貌似只是log看不到了,如果有版本号还是可以checkout到新版本的)。要恢复到最新,用:

 git checkout -

貌似上面这个只是还原到上次,所以,最通用的方法还是用:

git checkout 分支名

比如

 git checkout master

=对比两个版本=

 git diff 479e608..3d70fd9c
 git diff 479e608 3d70fd9c

效果一样的

=搭建repo= 随便找个支持ssh的服务器(wangyu.uicp.cn),随便找个目录,例如/git-repo/

git init --bare wdr4310.git

git支持ssh协议 =push本地源码到服务器=

git push root@wangyu.uicp.cn:/git-repo/wdr4310.git master

=clone=

git clone root@wangyu.uicp.cn:/git-repo/wdr4310.git

=放弃修改=

git checkout <filename> 还原一个文件

git checkout .还原当前目录

git reset --hard HASH #返回到某个节点,不保留修改

git reset --soft HASH #返回到某个节点。保留修改

==不小心运行了git commit -a -m ==

 git reset HEAD^

还原到运行git commit 之前一模一样的状态

=创建branch=

git branch net_bridge_by_5ghz

=打tag=

git tag "works_for_half_a_year"

=切换到branch或tag=

git checkout <tagname> or <branchname>

=提交修改= ==修改了文件==

git add 1.txt

git commit -m "xxxxx"

这种方法可以提交部分改动

或者

git commit -a -m "xxxxxxx"

==新增了建== 新增了文件必须先用git add =拷贝.git目录实验= /目录已经被git控制,目前在分支1。 新建/test-only ,拷贝.git到test-only。

在test-only里执行,git checkout 切换到分支2。 结果test-only里面并没有出现新文件,而是/里的文件被修改了。

在/目录执行git checkout 分支1,git 认为当前已经在分支1了。并且列出了一些M。

结论:在test-only里执行的git checkout文件改的是/的,但是元数据读写的是/test-only/.git的。

结论:git是逐次向上查找.git目录的。父目录的.git可能会因为子目录里有.git而被忽略

/.git/config里有一行

 worktree = /

如果删除掉,再执行上面操作。test-only里就会出现文件。

结论:如果有worktree= xxx,那么git按绝对路径管理文件。 否则按相对路径。

在/test-only/aaa/里面执行 git init . ,config里并没有出现worktree=xxx这一行。 貌似在根目录里执行git init会默认加上这一行?? 已实验,在根目录git init会加上这一行,在其他目录不会。

==防止每次都输密码==

 git config --global credential.helper cache

==用vim做编辑器==

git config --global core.editor "vim"

==把分支Push到另一个repo==

root@debian9:~/Desktop/udp2raw-multiplatform# git remote add main_repo https://github.com/wangyu-/udp2raw-tunnel.git
root@debian9:~/Desktop/udp2raw-multiplatform# git push main_repo mp

如果要从另一个repo pull代码:

 root@debian9:~/Desktop/udp2raw-multiplatform# git pull main_repo mp

==cherry-pick==

 git cherry-pick 6a498ec

 git cherry-pick 6a498ec^..6a498ec   #只pick 6a498ec

 git cherry-pick 6a498ec..6a498ec   #什么都pick不到 因为是 (a,b]区间

==~和^的区别==

Both ~ and ^ on their own refer to the parent of the commit (~~ and ^^ both refer to the grandparent commit, etc.) But they differ in meaning when they are used with numbers:

~2 means up two levels in the hierarchy, via the first parent if a commit has more than one parent

^2 means the second parent where a commit has more than one parent (i.e. because it's a merge)

These can be combined, so HEAD~2^3 means HEAD's grandparent commit's third parent commit.

G   H   I   J
 \ /     \ /
  D   E   F
   \  |  / \
    \ | /   |
     \|/    |
      B     C
       \   /
        \ /
         A
A =      = A^0
B = A^   = A^1     = A~1
C = A^2  = A^2
D = A^^  = A^1^1   = A~2
E = B^2  = A^^2
F = B^3  = A^^3
G = A^^^ = A^1^1^1 = A~3
H = D^2  = B^^2    = A^^^2  = A~2^2
I = F^   = B^3^    = A^^3^
J = F^2  = B^3^2   = A^^3^2
Clone this wiki locally