各种疑难 git 命令的 tips

git archive 以及 git rev-parse

head=`git rev-parse --verify --short HEAD`; echo $head
curr_path=`basename $PWD`; echo $curr_path
curr_path=`basename $PWD` && head=`git rev-parse --verify --short HEAD` && git archive --format=tar.gz HEAD > $curr_path-$head.tar.gz

git rebase 相关

原生的 rebase 命令

git rebase <commit head> <commit end> --onto <you want to>

<you want to> 是想让某段提交安插在哪个节点上。
<commit head><commit end> 是一段区间,要剪切走的区间。
注意是这段区间是前开后闭的!所谓前开后闭就是,<commit head> 节点不会参与移动,移动的是 <commit head>的下一个提交到 <commit end> 之间的所有节点。

tips:

  • 建议在 <commit end> 处建个分支,这段提交就总是会被找到。
  • 建议总是使用 commit hash 值进行位置的指定,使用分支名不靠谱。


举例:
使用gitk可以查看分支树
上面是使用 gitk 查看的分支树。
也可以使用下面的命令:

git log --oneline --graph -n 5 dev master

得:

* a468e06 (HEAD -> dev) add e
* d6c1c3a add d
| * e0e7e91 (master) add c
|/
* f08ddb9 add b

假设需求是把 a468e06 这单个节点移到 e0e7e91 上,但是不移动 d6c1c3a,那么可以使用命令:

git rebase d6c1c3a a468e06 --onto e0e7e91

rebase 完的那个节点是没有分支名的,建议 rebase 完立马为它创建分支名并跳转:

git checkout -b tmp

再次查看 git 树:

 git log --oneline --graph -n 5 dev master tmp

得:

* 576319f (HEAD -> tmp) add e
* e0e7e91 (master) add c
| * a468e06 (dev) add e
| * d6c1c3a add d
|/
* f08ddb9 add b

git 打 patch 的相关命令

git format-patch

git format-patch 可以保留 commit 信息,这一点很重要。
当然,代价就是每个 commit 节点都会生成一个对应的 patch 文件。
如果想合并提交并且还带 commit 信息的话,可以在打 patch 之前,使用 git rebase -i 来合并提交。

# 命令的标准型
git format-patch commitId1..commitId2
# 更早的提交放到前面
# 前开后闭,即 commitId1 不会被打 patch,commitId2 会被打 patch
# 比 commitId 更晚的所有节点都会生成 patch
git format-patch commitId
# commitId 本身不会生成 patch
# commitId 之前的 n 个提交
git format-patch -n commitId
# commitId 本身会生成 patch

# 特别的,可以用如下命令生成指定 commitId 的 patch
git format-patch -1 commitId

git applygit am

patch 是基于 diff 生成的。也就是说,是差异。所以合并 patch 需要基础版本是一致的,不一定是相同的 commit,但是产生 diff 的相关的代码段需要是相匹配的。

可以使用 git apply --check xxx-xxx.patch 进行检查,是否能完成 patch 合并
如果满足条件,可以使用 git apply xxx-xxx.patch 完成 patch 合并,但是 git apply 会丧失 commit 信息。
个人是不推荐使用 git apply 的。

使用 git am xxx-xxx.patch 可以完成 patch 合并,并且保留 commit 信息。但是。。。。
敲完 git am,再用 git status 的话,就像什么都没有发生一样,因为刚合并的节点没有分支名
可以用 git checkout -b <new commit hash>,创建分支。

导出某个节点的代码

git 导出某个节点的代码

git archive --format zip --output "./output.zip" master -0

两个分支整体比较

git difftool --dir-diff --no-symlinks 分支1 分支2

Windows 下设置 meld 工具:
打开文件:/c/Users/chenzhiwei/.gitconfig,并修改:

[diff]
        tool = meld
[difftool "meld"]
        path = "C:/Program Files (x86)/Meld/Meld.exe"

git 仓库创建以后

Git global setup
git config --global user.name "xxx"
git config --global user.email "xxx@xxx.com"

Create a new repository
git clone git@git.xxx.xxx:xxx/yyy.git
cd yyy
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

Push an existing folder
cd existing_folder
git init
git remote add origin git@git.xxx.xxx:xxx/yyy.git
git add .
git commit -m "Initial commit"
git push -u origin master

Push an existing Git repository
cd existing_repo
git remote rename origin old-origin
git remote add origin git@git.xxx.xxx:xxx/yyy.git
git push -u origin --all
git push -u origin --tags
vim ~/.gitconfig

# 然后添加如下代码
[diff]
        tool = meld
[difftool "meld"]
        path = "C:/Program Files (x86)/Meld/Meld.exe"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值