git如何只提交某个分支的commit

不及物动词 其他 267

回复

共3条回复 我来回复
  • 不及物动词的头像
    不及物动词
    这个人很懒,什么都没有留下~
    评论

    要只提交某个分支的commit,可以按照以下步骤操作:

    1. 确保你当前的工作目录在正确的分支上。使用以下命令切换到目标分支:
    “`
    git checkout 分支名
    “`

    2. 使用以下命令来查看你想要提交的commit的commit ID:
    “`
    git log
    “`
    浏览commit日志并记录下你想要提交的commit的commit ID。

    3. 使用以下命令来创建一个新分支,该分支将作为单独提交的基础:
    “`
    git branch 新分支名 提交的commit的commit ID
    “`
    这将在当前commit上创建一个新的分支,新分支名为你输入的名称。

    4. 使用以下命令切换到新的分支上:
    “`
    git checkout 新分支名
    “`
    现在你的工作目录将指向新创建的分支。

    5. 现在你可以进行修改和提交了。只有在这个新分支上的commit才会被提交,而不会影响其他分支。

    这样,你就可以只提交某个分支的commit了。记住,如果你希望将这些commit合并到其他分支中,可以使用合并命令(如git merge)将其合并到目标分支中。

    2年前 0条评论
  • fiy的头像
    fiy
    Worktile&PingCode市场小伙伴
    评论

    在Git中,你可以通过以下几种方式只提交某个分支的commit:

    1. 使用git cherry-pick命令:
    `git cherry-pick `
    这个命令可以选择某个分支的特定commit,并将其应用在当前分支上。你可以通过commit的哈希值或分支名来指定要应用的commit。

    2. 使用git rebase命令:
    首先,切换到当前分支上:
    `git checkout `
    然后,使用rebase命令将指定分支的commit应用到当前分支上:
    `git rebase `

    3. 使用git merge命令:
    首先,切换到当前分支上:
    `git checkout `
    然后,使用merge命令将指定分支的commit合并到当前分支上:
    `git merge `

    4. 使用git filter-branch命令:
    `git filter-branch –commit-filter ‘if [ `git rev-list –parents -n 1 $commit | grep ““` ]; then git commit-tree “$@”; else skip_commit “$@”; fi’`
    注意替换
    为要提交commit的分支名。

    5. 使用git revert命令:
    `git revert `
    这个命令将会创建一个新的commit,它的内容与指定commit相反。换句话说,它是通过撤销指定commit的更改来实现的。

    无论使用哪种方式,都需要先切换到想要提交commit的分支上(如果不在的话)。你可以使用`git branch -a`命令来查看当前仓库所有的分支,然后使用`git checkout `命令切换到要提交commit的分支上。

    2年前 0条评论
  • worktile的头像
    worktile
    Worktile官方账号
    评论

    要只提交某个分支的 commit,你可以使用以下步骤:

    1. 确认当前所处的分支,可以使用 `git branch` 命令来查看。

    “`bash
    $ git branch
    master
    * feature/foo
    “`

    在这个例子中,当前所处的分支是 `feature/foo`。

    2. 使用 `git log` 命令查看分支上的 commit 历史,并找出需要提交的 commit 的哈希值或者短哈希(commit hash)。

    “`bash
    $ git log
    commit c4fb707988b634f0e0f2e7cff5a14a5f2557ba0a (HEAD -> feature/foo)
    Author: John Doe
    Date: Mon Jan 1 12:34:56 2022 +0800

    Add new feature

    commit 1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b
    Author: John Doe
    Date: Sun Dec 31 23:59:59 2021 +0800

    Fix bug

    commit 9876543210abcdef
    Author: John Doe
    Date: Sat Dec 30 12:34:56 2021 +0800

    Initial commit
    “`

    在这个例子中,需要提交的 commit 的短哈希是 `c4fb707`。

    3. 创建一个新的分支来只提交指定的 commit,可以使用 `git checkout -b` 命令加上 commit 的哈希值来创建分支。

    “`bash
    $ git checkout -b new-branch c4fb707
    Switched to a new branch ‘new-branch’
    “`

    这将创建一个新的分支 `new-branch`,并切换到该分支。

    4. 确认你已经切换到了新分支,可以使用 `git branch` 命令再次检查。

    “`bash
    $ git branch
    master
    feature/foo
    * new-branch
    “`

    在这个例子中,当前所处的分支是 `new-branch`。

    5. 现在你可以对该分支进行修改、提交或合并操作了,只会影响到该分支上的 commit。

    6. 如果你想要将新分支的 commit 合并到原有的分支上,可以使用 `git merge` 命令。

    “`bash
    $ git checkout feature/foo
    Switched to branch ‘feature/foo’
    $ git merge new-branch
    “`

    这将把新分支的 commit 合并到 `feature/foo` 分支上。

    提交某个分支的 commit 的方法就介绍到这里了。希望对你有帮助!

    2年前 0条评论
注册PingCode 在线客服
站长微信
站长微信
电话联系

400-800-1024

工作日9:30-21:00在线

分享本页
返回顶部