Git操作

分支操作

# 创建本地分支
git checkout -b xxx
# 查看本地有哪些分支
git branch
# 删除本地分支
git branch -d xxx
# 切换分支
git checkout xxx
# 删除远程分支
git branch -r -d origin/xxx
git push origin:xxx
# 修剪本地分支,当远程分支已经删除,本地还没删除时,可能有pull失败的情况,用一下这个
git remote prune origin

MR(Merge Request)

# 创建并切入到本地临时分支
git checkout -b mybr
# 在 mybr 分支上做修改
# 修改后提交
git add *
git commit -m "xxxx"
# 将本地分支推到远程
git push origin mybr
# 在 gitlab/github 上create pull request 或 merge request

# 【可选】删除本地分支
git branch -d mybr
# 【可选】删除远程分支
git branch -d -r mybr
git push orgin:mybr

修改commit时间

# set the date of the last commit to current
git commit --amend --no-edit --data "$(date)"
# set the date of the last commit to specified time
git commit --amend --no-edit --date "Mon 20 Aug 2018 20:19:19 BST"
# set the date of any commit to current
## 1. rebase
git rebase <commit-hash>^ -i
## 2. replace pick with e on the line with whit that commit (the first one)
## 3. :wq quite editor
git commit --amend --no-edit --date "$(date)"
git rebase --continue

多个Commit合并

git rebase -i <commit-hash> # <commit-hash> is the commit before the last you wanna squash
# remain the first line pick xxx. and follow commit using s instead. e.g.:
# pick d94e78 Prepare the workbench for feature Z     --- older commit
# s 4e9baa Cool implementation 
# s afb581 Fix this and that  
# s 643d0e Code cleanup
# s 87871a I'm ready! 
# s 0c3317 Whoops, not yet... 
# s 871adf OK, feature Z is fully implemented      --- newer commit

从仓库拉取文件时,只拉取子目录

git init <repo>
cd <repo>
git remote add origin <url>
git config core.sparsecheckout true
echo "finisht/*" >> .git/info/sparse-checkout
git pull --depth=1 origin master

多commit合并

  • cherry-pick 方法
git checkout target_branch
git cherry-pick <commit-id> # 一个一个按顺序cherry pick
git cherry-pick <commit-id-start>..<commit-id-end> # 用..链接两个commit id来cherrypick一段范围的commit 
# 出现冲突 可使用git merge-tool GUI工具解决
  • rebase方法
    • 假设需要好合并source_branch的1234到5678的commits到target_branch
git checkout -b new_local_branch 5678 # 本地新建立一个分支,到 5678 commit
git rebase --onto target_branch 1234^ # 此操作将改变本地新建的分支
# new_local_branch就是1234~5678的commit合并到target_branch的结果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值