How to push only one commit with Git

本文介绍如何使用Git仅推送特定的提交记录到远程仓库,而非整个分支。此方法适用于需要精确控制版本发布的情况,如仅发布一次构建。文章详细解释了获取目标提交的哈希值并使用它进行推送的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ref:  http://blog.dennisrobinson.name/push-only-one-commit-with-git/

The other day at work I needed to push only one commit out of several I had made locally to the remote server, for a release build.  Normally when one does a push, they push everything at once.  However, git does provide a way to push only one commit at a time.  The caveat is that the single commit you want to push must be directly above the tip of the remote branch (the oldest of your local commits).  If it is not, don’t worry as you can simply reorder your local commits to suit the situation.

SourceTree does not seem to provide any way to do what we want, but the command line interface of git does, and its actually pretty easy.  First you need to get the hash of the commit you want to push by using the command “git log” (Press “q” to exit the log).  A hash is a 40 character alpha-numeric string that looks something like  2dc2b7e393e6b712ef103eaac81050b9693395a4 .  Once you have the correct hash, use the push command as you normally would, except provide the hash as part of the command:

$ git push <remote name> <commit hash>:<remote branch name> # Example: $ git push origin 2dc2b7e393e6b712ef103eaac81050b9693395a4:master

1

2

3

4

$ git push <remote name> <commit hash>:<remote branch name>

 

# Example:

$ git push origin 2dc2b7e393e6b712ef103eaac81050b9693395a4:master

Important Note: This will push all commits up to and including the specified commit!  This means if you specify the commit that is at the top of your branch it will push everything, exactly the same as a regular push.  You need to reorder your commits first to make sure the commit you want to push is at the bottom (directly above the remote branch).  How to reorder commits with Git.

In place of the hash, you can also use standard Git revision names such as HEAD~1 or HEAD^, as well as abbreviated hash names.

### 如何删除Git中的特定Commit或清除提交历史 在Git中,可以通过多种方式删除特定的Commit或者清除整个提交历史。以下是具体的操作方法: #### 方法一:通过`git rebase -i`交互式变基删除特定Commit 1. 使用 `git log` 查看所有的提交记录并定位到目标Commit及其父级Commit的哈希值[^1]。 2. 执行以下命令启动交互式变基操作: ```bash git rebase -i <parent-commit-hash> ``` 这里的 `<parent-commit-hash>` 是指需要删除的目标Commit的上一次Commit的哈希值。 3. 在打开的编辑器窗口中,将对应行的 `pick` 替换为 `drop` 来标记需要删除的Commit。 4. 完成修改后保存文件并退出编辑器。此时,Git会自动重新应用剩余的Commits,并跳过被标记为`drop`的Commit。 #### 方法二:清除特定某次提交之前的全部提交记录 如果目的是完全清空某个时间点以前的所有提交记录,则可以采用如下策略: 1. 首先创建一个新的孤儿分支(orphan branch),此新分支不会继承任何现有历史数据[^2]: ```bash git checkout --orphan new-branch-name ``` 2. 添加所需保留的内容至暂存区并完成首次Commit: ```bash git add . git commit -m "Initial commit" ``` 3. 将这个新的分支推送到远程仓库覆盖旧的历史版本: ```bash git push origin new-branch-name --force ``` #### 方法三:压缩多个连续Commits为单个Commit 当希望简化多次小改动形成的多条独立Commits时,可考虑将其合并成单一的大Commit: 1. 开始一个基于最近几次Commits范围内的互动变基过程: ```bash git rebase -i HEAD~N ``` 此处 N 表示想要处理的最后几笔Commits数量[^3]。 2. 修改编辑器里展示的动作列表,在首项保持原样(pick),后续各项改为 squash 或 fixup ,从而指示它们应如何融入首个Commit之中。 注意以上每种方案都可能影响团队协作环境下的共享代码库状态,因此务必谨慎行事尤其是涉及强制推送(`--force`)的情况下。 ```python # 示例Python脚本用于演示基本逻辑流程控制而非实际功能实现 def main(): action = input("Choose an option (delete/clear/squash): ") if action == 'delete': parent_commit_hash = input("Enter the hash of the parent commit: ") command = f'git rebase -i {parent_commit_hash}' elif action == 'clear': new_branch_name = input("Specify a name for your orphaned branch: ") commands = [ f'git checkout --orphan {new_branch_name}', 'git add .', 'git commit -m "First Commit"' ] # Assume these are executed sequentially elif action == 'squash': n_commits_back = int(input("How many commits back do you want to start squashing? ")) command = f'git rebase -i HEAD~{n_commits_back}' if __name__ == "__main__": main() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值