简体   繁体   English

在github而不是整个分支上推送文件

[英]Pushing a file on github, not the whole branch

Here are the steps I am following to push/commit my changes of file.txt from my local to my staged branch : 这是我按照以下步骤将file.txt更改从本地推送到/提交到暂存分支的步骤:

  1. git add file.txt
  2. git commit -m "my file is changed"
  3. git push <my-remote> <my-branch>

The problem is, the whole branch is pushed and merged, not only the changes in the file. 问题是,整个分支被推送并合并,而不仅仅是文件中的更改。 How do I avoid this ? 如何避免这种情况? And how do I delete from the history of my commits this specific one ? 以及如何从提交历史记录中删除此特定提交? so I can send a clean pull request to the master. 这样我就可以向主服务器发送干净请求请求。

That's not how git works. git不是这样工作的。 Push is used to send all your changes that the remote branch does not have. 推送用于发送远程分支没有的所有更改。 If you want to throw away all your other changes besides the commit named "my file is changed", you can do 如果您要放弃除名为“我的文件已更改”的提交以外的所有其他更改,则可以执行

git rebase -i $(git rev-parse origin/branch_name)

Set all the other commits to "d" for drop except for "my file is changed". 将除“我的文件已更改”以外的所有其他提交都设置为“ d”。 This will delete all the other commits and you can now push to push only that single commit. 这将删除所有其他提交,您现在可以按入仅推送该单个提交。

However, if your goal is to keep those other commits, I'd suggest checking out a new branch from the head of your remote branch and cherry-picking your single commit onto there, creating your pull request from this new branch instead. 但是,如果您的目标是保留其他提交,则建议您从远程分支的开头检出一个新分支,并在该分支中挑选单个提交,然后从该新分支创建拉取请求。

git checkout -b new_branch_name origin/branch_name
git cherry-pick commit_sha
git push -u origin new_branch_name

If you need to then remove the single commit from your original branch, just perform the first command I listed and drop that single commit instead of the others. 如果需要从原始分支中删除单个提交,只需执行我列出的第一个命令,然后删除该单个提交即可。

当dev是您的远程分支时,请尝试

git push origin HEAD:refs/for/dev

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM