Git Tip: git push ‘No refs in common and none specified’

本文详细介绍了在从Subversion切换到Git后遇到的一个常见问题,即尝试将本地仓库推送到一个为空的远程仓库时出现的错误。解释了原因在于两个仓库文件内容不匹配,并提供了简单的解决方案来修复问题。

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

源:http://blog.youkuaiyun.com/fudesign2008/article/details/8692696
评:


@see http://www.thebuzzmedia.com/git-tip-git-push-no-refs-in-common-and-none-specified/


Git is a source-control tool used by software developers.

I recently switched from Subversion to Git and while things have been mostly smooth, there have been a few “WTF?” moments. I am going to try and blog the few beginner ones I ran into in hopes of helping anyone else.

Today I ran a ‘git push’ to shove my commits from my local repository back into the main remote repo, the result was this:

$ git push
No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
error: failed to push some refs to 'git@github.com:painfreepr/<repo>.git'

The odd bit is that I had just done this with a previous repo about 30 mins ago and it worked fine; this was a new repository I was setting up. As it turns out this is the result of originally cloning an empty repository (link, link) which is exactly what I did. I had created a new repo on GitHub and wanted to pull the repo down in IntelliJ to then add some files to it via the GUI instead of from the command line; so I had checked out the empty repo right after creating it.

The fix, fortunately, is dead easy:

$ git push origin master

Doing this should provide output like:

$ git push origin master

Counting objects: 568, done.

Delta compression using up to 2 threads.

Compressing objects: 100% (559/559), done.

Writing objects: 100% (568/568), 2.28 MiB | 2.18 MiB/s, done.

Total 568 (delta 205), reused 0 (delta 0)

To git@github.com:painfreepr/<repo>.git

* [new branch] master -> master

It is my understanding that the core issue is that there are no files in commonbetween the original remote repo you cloned (empty) and the one on-disk (now full of files). Doing the git-push-origin-master shoves your repo up into the empty repository and gives you that common base again so you can do a ‘git push‘ without issue.

Happy Git’ing!
### 推送特定文件到 Git 远程仓库的 `feature-0401` 分支 在 Git 中无法直接推送单个文件到远程分支;Git 是基于提交(commit)进行操作的工具。为了实现仅推送某些更改至远程分支,可以通过创建一个新的提交来包含这些特定文件的变化,并确保该提交被应用到了目标分支上。 #### 方法一:通过暂存区管理文件更新 如果希望向现有的 `feature-0401` 分支添加新的改动而不影响其他未完成的工作: 1. 切换到 `feature-0401` 分支并获取最新的变更: ```bash git checkout feature-0401 git pull origin feature-0401 ``` 2. 只将需要推送的文件加入暂存区: ```bash git add path/to/file1.txt path/to/file2.c ``` 3. 创建一个新提交记录这次修改: ```bash git commit -m "Add specific files" ``` 4. 将此提交推送到远程服务器上的同名分支: ```bash git push origin HEAD:refs/heads/feature-0401 ``` 这种方法允许精确控制哪些文件会被发送出去,同时也保留了历史记录的一致性和可追溯性[^1]。 #### 方法二:创建临时分支用于单独处理 当不想污染当前工作目录中的其他开发活动时,可以选择建立一个专门用来集成选定文件的新分支: 1. 基于最新状态创建一个名为 `temp-feature-update` 的临时分支: ```bash git fetch origin git checkout -b temp-feature-update origin/feature-0401 ``` 2. 添加所需文件到这个新分支中: ```bash git add ./path/to/specific_file.py git commit -m "Include only the specified file changes." ``` 3. 合并回原分支前先同步最新版本: ```bash git rebase origin/feature-0401 ``` 4. 更新远端的目标分支: ```bash git push --force-with-lease origin temp-feature-update:feature-0401 ``` 注意,在执行强制推送(`--force`)命令之前应当小心确认不会覆盖他人可能已经做出的重要贡献[^2]。 以上两种方法都可以有效地达到目的——即把指定文件的内容安全可靠地传递给团队成员或其他协作者访问使用的远程存储库位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值