简体   繁体   English

错误:未能将一些引用推送到 &#39;git@github.com:<name> /<project> .git&#39;

[英]error: failed to push some refs to 'git@github.com:<name>/<project>.git'

Hi I'm a total newbie using git I read a few StackOverflow Q&A on this error but didn't understand how my problem relates to others with the same error message.嗨,我是一个使用 git 的新手,我阅读了一些关于此错误的 StackOverflow 问答,但不明白我的问题与其他具有相同错误消息的问题有何关联。 So I didn't dare to chance it and loose 6 hours of work that I need to push to my remote GitHub repo.所以我不敢冒险,放弃了我需要推送到远程 GitHub 存储库的 6 个小时的工作。

. .

1.) The Beginning 1.) 开始

So my code in the local working directory got broken and I couldn't figure out what the problem was.所以我在本地工作目录中的代码被破坏了,我无法弄清楚问题是什么。 So I wanted git to go back to the latest working version of my project.所以我想让 git 回到我项目的最新工作版本。 I then found this tutorial and managed to get my local project to go back to a working version.然后我找到了这个教程并设法让我的本地项目回到工作版本。 https://www.git-tower.com/learn/git/faq/restore-repo-to-previous-revision https://www.git-tower.com/learn/git/faq/restore-repo-to-previous-revision

$ git reset --hard <SHA-1 HASH NUMBERS>

. .

2.) Trying to push 2.) 试图推动

These are the commands I usually follow when I want to push local code to my remote repo.当我想将本地代码推送到我的远程仓库时,这些是我通常遵循的命令。

# Pushing Changes - Staging
$ git diff
$ git status
$ git add -A
$ git status
$ git commit -m "Modified multiply function."

# Pushing Changes - Repository
$ git pull origin master
$ git push origin master

. .

3.) The error 3.) 错误

I don't understand what the error message is trying to tell me.我不明白错误消息试图告诉我什么。 Can somebody help me by showing what sequence of commands I need to run to be able to push code like usual again?有人可以通过显示我需要运行哪些命令序列才能再次像往常一样推送代码来帮助我吗?

penguin@linux$ git push origin master

To git@github.com:<name>/<project>.git

 ! [rejected]        master -> master (non-fast-forward) error: failed to push some refs to 'git@github.com:<name>/<project>.git'

hint: Updates were rejected because a pushed branch tip is behind its remote 
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Let's address this rather long comment (which I should do in another comment, but this won't fit at all so I'm answering a different question, namely, "how does detaching and reattaching HEAD work"):让我们解决这个相当长的评论(我应该在另一条评论中做这件事,但这根本不合适所以我要回答一个不同的问题,即“分离和重新连接 HEAD 是如何工作的”):

Tnx torek I managed to force away that error with the link you provided. Tnx torek 我设法通过您提供的链接强制消除了该错误。 I also had some detached HEAD issues which I got rid by following Razan Paul's post here Fix a Git detached head?我也有一些独立的 HEAD 问题,我通过关注 Razan Paul 的帖子在这里解决了修复 Git 独立的头部问题? But all my changes while in detached HEAD mode was lost after the fixes.但是在修复后,我在分离 HEAD 模式下所做的所有更改都丢失了。 I have copies so I can just recreate them manually I guess.我有副本,所以我想我可以手动重新创建它们。 So in the future when I want to go back to the latest working version on my local working directory.所以将来当我想回到本地工作目录上的最新工作版本时。 What procedure will suite my situation best?什么程序最适合我的情况? I see so many different suggestions I don't know what's best for me我看到了很多不同的建议,我不知道什么最适合我

(incidentally, when addressing a comment to someone in particular you may want to use the @<name> syntax so they get alerted). (顺便说一句,当向某个人发表评论时,您可能希望使用@<name>语法,以便他们收到警报)。

What you need is a proper Git book.你需要的是一本合适的 Git 书。 The best free one is, in my opinion, the Pro Git 2 book .在我看来,最好的免费书籍Pro Git 2 书 (There are not many, if any, other free ones so "best" could be vacuously true here. :-) ) A shorter, non- book reference that I find helpful is Think Like (a) Git , because Git is built atop some very simple bits of graph theory—but unless you know this, and until you realize this, you're stuck in this xkcd cartoon . (没有很多,如果有的话,其他免费的,所以“最好”在这里可能是空洞的。:-))我觉得有帮助的较短的非书本参考是Think Like (a) Git ,因为 Git 是构建在上面的一些非常简单的图论——但除非你知道这一点,直到你意识到这一点,否则你会被困在这个 xkcd 卡通中

(I even have my own start on a book , but I have very little time to work on it these days.) (我什至有自己的书开始,但这些天我几乎没有时间来处理它。)

There are many work-flows you can use in Git.您可以在 Git 中使用许多工作流程。 The first thing to remember, though, is that Git's "unit of storage" as it were—the thing it desperately tries to protect, so that you can get it back—is the commit .不过,首先要记住的是,Git 的“存储单元”实际上是commit That's why you were having trouble pushing: you were telling some other Git: throw away some commits .这就是为什么你在推送时遇到问题:你在告诉其他一些 Git: throw away some commits Git does not want to do that. Git 不想这样做。

Commits are uniquely identified by their hash IDs.提交由其哈希 ID 唯一标识。 These hash IDs are not very useful to humans, so Git gives us things like branch names to remember particular IDs—but in some cases you may have to resort to raw IDs, which you can cut and paste with your mouse, for instance.这些哈希 ID 对人类不是很有用,所以 Git 给了我们诸如分支名称之类的东西来记住特定的 ID——但在某些情况下,你可能不得不求助于原始 ID,例如,你可以用鼠标剪切和粘贴。

Git's desire to keep commits around means that when you make commits using a detached HEAD, Git tries to keep those too. Git 希望保留提交意味着当您使用分离的 HEAD 进行提交时,Git 也会尝试保留这些提交。 It will do so for, by default, at least 30 days.默认情况下,它会这样做至少 30 天。 You can find the hash IDs using git reflog .您可以使用git reflog找到哈希 ID。 If you use git reset --hard to make Git "forget" commits that were on a branch (rather than a detached HEAD), Git keeps those IDs around for at least 30 days as well, on the branch name 's reflog.如果您使用git reset --hard使 Git“忘记”在分支(而不是分离的 HEAD)上的提交,Git 也会在分支名称的 reflog 上保留这些ID 至少 30 天。 There's one reflog for each branch name, and one for HEAD itself.每个分支名称有一个 reflog, HEAD本身有一个。

Last, an attached HEAD—attached being the opposite of detached—is when you are, as git status will say, "on" some particular branch:最后,一个附加的HEAD——attached 是 detached 的反面——是当你在某个特定的分支上时,正如git status会说的那样:

$ git status
On branch master
...

In this case, the branch name master is how Git actually identifies which commit you have checked out right now;在这种情况下,分支名称master是 Git 实际识别您现在签出的提交的方式; and the name HEAD is simply attached to the name master .并且名称HEAD简单地附加到名称master

To make this all work right, Git works backwards .为了使这一切正常工作, Git向后工作

A brief introduction to graphs图的简单介绍

To really comprehend what all this means, you need to draw the commit graph .要真正理解这一切意味着什么,您需要绘制提交图 Remember that each commit has its own unique hash ID—those big ugly 40-character-long strings of a hexadecimal number, like 5be1f00a9a701532232f57958efab4be8c959a29 —but that's kind of unweildy, so you might want to just use single letters for small drawings:请记住,每个提交都有自己唯一的哈希5be1f00a9a701532232f57958efab4be8c959a29那些丑陋的 40 个字符长的十六进制数字字符串,比如5be1f00a9a701532232f57958efab4be8c959a29有点笨拙,所以你可能只想使用单个字母来绘制小图:

A <-B <-C   <--master

This is a pretty-new repository with just three commits in it.这是一个非常新的存储库,其中只有三个提交。 We made A first, then we made B , then we made C .我们先做了A ,然后我们做了B ,然后我们做了C (We'll run out of names for commits when we get to our 27th commit, so you can see why Git uses longer hash IDs.) (当我们进行第 27 次提交时,提交的名称将用完,因此您可以了解 Git 使用更长哈希 ID 的原因。)

Since Git works backwards, the name master identifies, not commit A , but rather commit C .由于 Git 向后工作,因此名称master标识的不是提交A ,而是提交C We say that the name master points to C .我们说名称master指向C

Commit C , being our third commit, has inside it the name—the hash ID—of our second commit, commit B .提交C是我们的第三次提交,其中包含我们第二次提交提交B的名称(哈希 ID)。 We say that C 's parent commit is B .我们说C提交是B So commit C points to B .所以提交C指向B Likewise, B has inside it the hash ID of commit A , so B points back to A .同样, B里面有提交A的哈希 ID,所以B指向A

Commit A was the first commit.提交A是第一个提交。 It can't point back to a previous commit, so it just doesn't.不能指向以前的提交,所以它只是没有。 This makes commit A special: it's a root commit .这使得 commit A特别:它是一个root commit Every non-empty repository has at least one of these, the first commit ever made.每个非空存储库至少有一个,这是有史以来的第一次提交。

When you run git log , Git will start with your current commit—commit C , here—and work backwards: it shows you C , and then since C points back to B , Git shows B too.当你运行git log ,Git会与您当前提交提交启动C ,在这里和工作倒退:它表明你C ,然后因为C点回B ,Git的节目B太。 Since B points back to A , Git shows A as well;由于B指向A ,Git 也显示A but A is a root commit, so Git can stop.但是A是根提交,因此 Git 可以停止。

How branches grow树枝如何生长

Since we're on master , let's make a new commit D .由于我们在master ,让我们进行新的提交D We'll do whatever we want with the source, git add files, and run git commit to create D .我们将对源做任何我们想做的事情, git add文件,然后运行git commit来创建D

Where will D point back to? D将指向何处? Well, obviously it has to point back to C .好吧,显然它必须指向C So Git makes D have a parent commit of C :所以 Git 让D有一个C提交:

A <-B <-C <-D

Git's final step here is to change the name master so that it holds the hash ID of commit D , giving us this picture: Git 在这里的最后一步是更改名称master以便它保存提交D的哈希 ID,给我们这张图:

A <-B <-C <-D   <--master

Note that all the arrows necessarily point backwards .请注意,所有箭头都必须向后指向。 Moreover, all the arrows that are inside commits are frozen for all time: D points back to C , never anywhere else.此外,提交中的所有箭头都被永久冻结: D指向C ,永远不会指向其他任何地方。 The only arrow that changes is the one coming out of the branch name!唯一变化的箭头是从分支名称中出来的箭头! So I tend to draw them without the internal arrows, to save space and make it fit better:所以我倾向于在没有内部箭头的情况下绘制它们,以节省空间并使其更适合:

A--B--C--D   <-- master

This is another key to understanding Git: branch names move, over time.这是理解 Git 的另一个关键:分支名称随着时间的推移而移动。 Branches acquire new commits, and the names point to the last commit on the branch—what Git calls the tip commit .分支获取新的提交,名称指向分支上的最后一次提交——Git 称之为提示提交 Git uses this tip commit to find the next-older commit for that branch: the tip's parent. Git 使用此提示提交来查找该分支的下一个较旧的提交:提示的父级。 Git uses that commit to find its previous commit, and so on, all the way back to the root commit. Git 使用提交来查找先前的提交,依此类推,一直返回到根提交。

The name HEAD normally just identifies a branch name名称HEAD通常只是标识一个分支名称

Let's complicate-up the above repository by adding a branch coming out of C :让我们通过添加一个来自C的分支来使上述存储库复杂化:

A--B--C--D   <-- master
       \
        E   <-- develop

Here, we now have two branches.在这里,我们现在有两个分支。 The name develop identifies commit E , whose parent is C .名称develop标识提交E ,其父项是C If we now run:如果我们现在运行:

git checkout master

we'll be on branch master , as git status will say;我们将on branch master ,正如git status所说; if we create a new commit F now, its parent will be D .如果我们现在创建一个新的提交F ,它的父项将是D If we instead git checkout develop and create a new commit F , its parent will be E instead.如果我们改为git checkout develop并创建一个新的提交F ,它的父代将是E So Git needs to know: which branch are we on?所以 Git 需要知道:我们在哪个分支? This is where we need to draw in the name HEAD :这是我们需要在名称HEAD绘制的地方:

A--B--C--D   <-- master (HEAD)
       \
        E   <-- develop

or:或者:

A--B--C--D   <-- master
       \
        E   <-- develop (HEAD)

When your HEAD is attached like this, it's attached to a branch name .当您的HEAD像这样附加时,它附加到一个分支名称 When you make new commits, Git will change the branch name so that it points to the new commit you just made.当您进行新提交时,Git 将更改分支名称,使其指向您刚刚进行的新提交。

A "detached HEAD" simply holds the commit hash ID directly “分离的 HEAD”只是直接保存提交哈希 ID

If you find yourself in detached HEAD mode, it's usually because you checked out an older commit directly.如果您发现自己处于detached HEAD模式,通常是因为您直接检出了较旧的提交。 For instance, let's check out commit B , while keeping everything we have:例如,让我们检查提交B ,同时保留我们拥有的所有内容:

A--B   <-- HEAD
    \
     C--D   <-- master
      \
       E   <-- develop

I had to bend the graph lines around to fit HEAD in, but now HEAD points directly to commit B .我不得不弯曲图形线以适应HEAD ,但现在HEAD直接指向提交B

If we now make a new commit F , its parent will be B , and Git will make HEAD point directly to F :如果我们现在进行新的提交F ,它的父项将是B ,并且 Git 将使HEAD直接指向F

A--B--F   <-- HEAD
    \
     C--D   <-- master
      \
       E   <-- develop

Note that whenever Git creates a new commit, the commit to which HEAD points—directly, if detached, or indirectly, if attached to a branch name—also changes!请注意,每当 Git 创建一个提交时, HEAD指向的提交(直接,如果分离,或间接,如果附加到分支名称)也会改变! The new commit's parent is always whatever was HEAD just a moment ago, and now HEAD is the new commit, which is once again the current commit.新提交的父级始终刚才的HEAD ,现在HEAD提交,它再次是当前提交。

(This is also why parents don't know their children's hash IDs, but a child does know its own parent's ID. Git has to work backwards, because children know their parents: the parent commits exist when the child is created; but parent commits—which are read-only and frozen in time—don't know what children they will acquire in the future.) (这也是为什么父母不知道他们孩子的哈希 ID,但孩子知道自己父母的 ID。Git必须向后工作,因为孩子知道他们的父母:当孩子被创建时,父母提交存在;但父母提交——它们是只读的并且被时间冻结——不知道他们将来会获得什么孩子。)

As soon as we re-attach HEAD , though, we "lose" the ID of commit F:但是,一旦我们重新附加HEAD ,我们就会“丢失”提交 F 的 ID:

A--B--F   <-- ???
    \
     C--D   <-- master (HEAD)
      \
       E   <-- develop

Who remembers F 's ID?谁还记得F的 ID? The answer is: only the reflogs .答案是:只有 reflogs

If you want to hang on to commit F , you should find its ID in your reflogs and attach a name to it—a branch or tag name, usually:如果你想继续提交F ,你应该在你的 reflogs 中找到它的 ID 并为其附加一个名称——一个分支或标签名称,通常:

A--B--F   <-- newbranch
    \
     C--D   <-- master (HEAD)
      \
       E   <-- develop

Now newbranch remembers the ID of F .现在newbranch记住了F的 ID。 If you'd made two commits, F and G , while you had a detached HEAD, you'd have to find the later one somehow and make sure your name points to G , so that you have:如果你做了两次提交, FG ,而你有一个分离的 HEAD ,你必须以某种方式找到一个并确保你的名字指向G ,这样你就有:

        G   <-- newbranch
       /
A--B--F
    \
     C--D   <-- master (HEAD)
      \
       E   <-- develop

and not:并不是:

        G   <-- ???
       /
A--B--F   <-- newbranch
    \
     C--D   <-- master (HEAD)
      \
       E   <-- develop

This is why making a lot of commits on a detached HEAD is not a great idea: it can be really hard to tell, from hash IDs, which commit is the tip-most .这就是为什么在分离的 HEAD 上进行大量提交并不是一个好主意:从哈希 ID 很难判断哪个提交是最重要的.

The git it clearly mentioning that the current branch in which your working is not updated with remote repository as others have committed their code to that repository. git 它清楚地提到您的工作所在的当前分支未使用远程存储库更新,因为其他人已将其代码提交到该存储库。

First you need to take a pull of remote repository and then commit your code首先,您需要拉取远程存储库,然后提交您的代码

NOTE : Here origin is your remote repository location which you might have added注意:这里的origin是您可能已添加的远程存储库位置

Eg: Considering that your working on master branch例如:考虑到您在 master 分支上工作

git pull origin master  /* To update with the remote repository */

git push origin master /* To push your updated code */

暂无
暂无

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

相关问题 Git 克隆使用 ssh 不适用于消息“git@github.com:没有这样的文件或目录” - Git clone using ssh does not work with message "git@github.com: No such file or directory" ssh -vT git@github.com工作正常,代理位于github ssh密钥中。 问题出在哪里? - `ssh -vT git@github.com` works fine, and agent is in github ssh keys. Where is the issue? `gh cli` 失败`git@github.com: Permission denied (publickey)` 但`gh desktop` 没有问题 - `gh cli` fails `git@github.com: Permission denied (publickey)` but no problem with `gh desktop` git push错误:无法读取“ https://github.com”的用户名:权限被拒绝 - git push error: could not read Username for 'https://github.com': Permission denied Git,致命:无法访问“https://github.com/path/to/repo.git”:gnutls_handshake() 失败:pull 函数出错 - Git, fatal: unable to access 'https://github.com/path/to/repo.git': gnutls_handshake() failed: Error in the pull function 当我执行“git push origin master”时,Github 出错(错误:src refspec master 不匹配) - Error in Github when I do “git push origin master” (error: src refspec master does not match any) git:&#39;http-push&#39;不是git命令。 致命:git-http-push失败了 - git: 'http-push' is not a git command. fatal: git-http-push failed git推后,该项目从gitweb消失 - The project disappear from gitweb after a git push 在使用 @io_bazel_rules_docker 时运行 'git fetch origin refs/heads/*:refs/remotes/origin/* refs/tags/*:refs/tags/*' 时出错 - error running 'git fetch origin refs/heads/*:refs/remotes/origin/* refs/tags/*:refs/tags/*' while working with @io_bazel_rules_docker git push突然失败(git-remote-https:重定位错误) - git push failing suddenly (git-remote-https: relocation error)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM