常用git操作

举报
村头树下 发表于 2021/01/06 10:57:17 2021/01/06
【摘要】 1. 修改上次提交的commitgit add taget/file/path/filenamegit commit --amendgit push -f origin branch_name2. 合并最后几个commit为一个commit假设想合并06dc与d67a为同一个commitcommit 06dc7f1d7b00f3644e85c6ed3962252bd53a25bdAuthor...

1. 修改上次提交的commit

git add taget/file/path/filename
git commit --amend
git push -f origin branch_name

2. 合并最后几个commit为一个commit

假设想合并06dc与d67a为同一个commit

commit 06dc7f1d7b00f3644e85c6ed3962252bd53a25bd
Author: xxxxxx
Date: 1-3

commit d67a77a4537e13c61a703d8914dfa499042015c3
Author: xxxxxx
Date: 1-2

commit 622332be1af0854bf54cc0d4bcce0b6dfb758ba6
Author: xxxxxx
Date: 1-1

执行

git  rebase -i  622332be1af0854bf54cc0d4bcce0b6dfb758ba6

决定合并的目标节点不动,其他的节点的pick改为s, 退出

pick d67a77a xxxxx
pick 06dc7f1 xxxxx

# Rebase 622332b..06dc7f1 onto 622332b
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

然后需要再设置新的message

# This is a combination of 2 commits.
# The first commit's message is:

xxxxxxxx

# This is the 2nd commit message:

xxxxxxxxx

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.

根据所需更改即可

3. 选择性commit

git commit -p

然后对于想要commit的部分按y选择,不需要commit的部分按n丢弃

4.cherry-pick

A分支想要直接同步B分支的commit

先列出B的commit

git log B
commit d3dc161af5031f7c71a2c8fe9848b4265c7804fd
xx
commit d67a77a4537e13c61a703d8914dfa499042015c3
xx
commit 622332be1af0854bf54cc0d4bcce0b6dfb758ba6
xx
commit fc1d2b6b5b12866e3beab0addbc15d820a5a41e9
xxx

假设想要将最新的一条commit cherry-pick过去:

git cherry-pick d3dc161af5031f7c71a2c8fe9848b4265c7804fd

假设想将最新的三条一块cherry-pick过去:

git cherry-pick commit 622332be1af0854bf54cc0d4bcce0b6dfb758ba6^..d3dc161af5031f7c71a2c8fe9848b4265c7804fd


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。