添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more

For some reason, I can't push now, whereas I could do it yesterday. Maybe I messed up with configs or something.

This is what happens:

When I use the git push origin master

What my working directory and remote repository looks like:

looks like your local repo is not in sync with the git repo. did you try to do git pull ? R11G Jun 9 '14 at 6:25 yes, but I have no idea with the following syntax after git pull it says git pull <remote> <branch>, can you let me see an example syntax for git pull? leipzy Jun 9 '14 at 6:29 I got that error on a new repo. This helped: stackoverflow.com/a/6518774/2067690 HumanInDisguise Jul 8 '15 at 20:38

(Note: starting Oct. 2020 , any new repository is created with the default branch main , not master . And you can rename existing repository default branch from master to main .
The rest of this 2014 answer has been updated to use " main ")

If the GitHub repo has seen new commits pushed to it, while you were working locally, I would advise using:

git pull --rebase
git push

The full syntax is:

git pull --rebase origin main
git push origin main

With Git 2.6+ (Sept. 2015), after having done (once)

git config --global pull.rebase true
git config --global rebase.autoStash true

A simple git pull would be enough.
(Note: with Git 2.27 Q2 2020, a merge.autostash is also available for your regular pull, without rebase)

That way, you would replay (the --rebase part) your local commits on top of the newly updated origin/main (or origin/yourBranch: git pull origin yourBranch).

See a more complete example in the chapter 6 Pull with rebase of the Git Pocket Book.

I would recommend a:

# add and commit first
git push -u origin main

That would establish a tracking relationship between your local main branch and its upstream branch.
After that, any future push for that branch can be done with a simple:

git push

See "Why do I need to explicitly push a new branch?".

Since the OP already reset and redone its commit on top of origin/main:

git reset --mixed origin/main
git add .
git commit -m "This is a new commit for what I originally planned to be amended"
git push origin main

There is no need to pull --rebase.

Note: git reset --mixed origin/main can also be written git reset origin/main, since the --mixed option is the default one when using git reset.

is it OK to execute your suggested git pull --rebase...? coz I already done > git reset --mixed origin/master > git add . > git commit -m "This is a new commit for what I originally planned to be an amendmend" > git push origin master suggested here stackoverflow.com/questions/18588974/… btw your answer looks helpful sir – leipzy Jun 9 '14 at 6:51 Really super.. below commands worked for me... git reset --mixed origin/master git add . git commit -m "This is a new commit for what I originally planned to be amended" git push origin master Thank you @VonC – Hari Narayanan Dec 5 '17 at 5:28

EDIT: Based on @Mehdi ‘s comment below I need to clarify something about —force pushing. The git command above works safely only for the first commit. If there were already commits, pull requests or branches in previous, this resets all of it and set it from zero. If so, please refer @VonC ‘s detailed answer for better solution.

Works but bad, please don't use it unless you know what you're doing. (probably you don't know what you're doing if you're looking in S.O) – Mehdi Jan 19 '18 at 9:42 If you're going to try -f/--force it's always safer to use --force-with-lease instead, which will abort if there are downstream changes that would get clobbered by the push. --force-with-lease is required for lots of everyday rebasing situations, but --force should almost never be needed. – Joshua Goldberg Jan 23 '19 at 19:13 just ran into this. Commit command didn't work during the git add. good call. Thanks – jgritten Aug 2 '18 at 2:44 Thanks man! That's it. I thought I committed my changes. Now git push -u origin master works fine. – tleo Jul 21 '19 at 11:47

I had same problem. I was getting this problem because i had not made any commit not even initial commit and still i was trying to push.

Once i did git commit -m "your msg" and then everything worked fine.

That doesn't make much sense. The original question is about the local git being behind. In no way "being behind" can be resolved my making a local commit! – GhostCat Jan 4 '17 at 13:19 I just had this problem and I forgot to commit. Error message should be more clear – Ivan Topić Nov 27 '18 at 0:26 It does apply to me since I was getting that exact error message and this solution fixed my problem. – Diego Fortes Oct 14 '19 at 17:38 Wow, this actually worked, but why? I had a hyphen in my local branch name: my-branch_wont_push. Once I renamed it to my_branch_wont_push, then git push -u origin my_branch_wont_push worked for me. – cdabel Apr 23 '20 at 6:20 Thanks. As @cdabel mentioned, the hyphen was the culprit. Changed it to underscore and my push went through. – Annie Lagang Oct 24 '20 at 7:48
  • git init

  • git remote add origin https://gitlab.com/crew-chief-systems/bot

  • git remote -v (for checking current repository)

  • git add -A(add all files)

  • git commit -m 'Added my project'

  • git pull --rebase origin master

  • git push origin master

  • I find the solution to this problem in github help.

    You can see it from:Dealing with non-fast-forward errors

    It says:

    You can fix this by fetching and merging the changes made on the remote branch with the changes that you have made locally:

    $ git fetch origin
    # Fetches updates made to an online repository
    $ git merge origin branch
    # Merges updates made online with your local work
    

    Or, you can simply use git pull to perform both commands at once:

    $ git pull origin branch
    # Grabs online updates and merges them with your local work
                    This is the normal process whenever things are working as expected. It doesn't help anything when git thinks it is already up to date as @rubyandcoffee asked.
    – Tim
                    Dec 22 '17 at 16:51
    
  • git add .
  • git commit -m 'Add your commit message'
  • git remote add origin https://User_name@bitbucket.org/User_name/sample.git

    (Above url https://User_name@bitbucket.org/User_name/sample.git refers to your bit bucket project url )

  • git push -u origin master
  • check if your git hub account link with your local git by using:

    git config --global user.email "you@example.com"
    git config --global user.name "Your Name"
    

    I created an empty repo in GitHub, and have my code locally. I faced the same issue now, as I followed the below sequence,

    git init
    git commit -m 'Initial Commit'
    git remote add origin https://github.com/kavinraju/Repo-Name.git
    git add .
    git push -u origin master
      

    ISSUE WAS: I tried to commit before staging the files I have.

    SO WE NEED TO STAGE THE FILES AND THEN COMMIT.

    This is the correct sequence.

    git init
    git add .
    git commit -m 'Initial Commit'
    git remote add origin https://github.com/kavinraju/Repo-Name.git
    git push -u origin master
    

    Since I execute the wrong sequence first, I just execute the below commands

    git add .
    git commit -m 'Initial Commit'
    git push -u origin master
    

    Because maybe have nothing to push (really, no thing to push). Do like this

    git remote add origin https://github.com/donhuvy/accounting133.git
    git remote -v
    git add .
    git commit -m"upload"
    git push --set-upstream origin master
    

    Change the remote repository url in your case. Command git remote -v you can skip, just for checking.

    I faced this issue but after 2 days I got a solution. Just run these two commands if you are deploying your site on GitHub pages first time.

    git commit -m "initial commit"
    git push origin +HEAD
                    I had this issue exactly, I was on feature22 and was doing git push origin feature22-fix, but feature22-fix didn't exit neither local nor remote, so I had to first checkout the  branch locally, then push
    – Honey
                    Jun 24 '19 at 18:37
    

    It may happen when you don't have any files. Try to create a text file then follow the following commands

    git add .
    git commit -m "first commit"
    git push --set-upstream origin master
    

    The fact that GitHub changed master to main made me encounter this issue. So from now on, the solution to push to origin is:

    git push -u origin main
                    Do you means the remote repo on GitHub has only a branch main, not master? I have created a brand new repo on GitHub, and its main branch remains... master (for now)
    – VonC
                    Jun 29 '20 at 20:21
    

    In my case there was a problem with a git pre-push hook.

    Run git push --verbose to see if there are any errors.

    Double check your git-hooks in the directory .git/hooks or move them temporarily to another place and see if everything works after that.

    Github changed the default branch name from master to main. So if you created the repo recently, try pushing main branch

    git push origin main
    

    This is a common mistake, beginners can make.

    Github Article

    If you are attempting to initialize a directory with an existing GitHub repository, you should ensure you are committing changes.

    Try creating a file:

    touch initial
    git add initial
    git commit -m "initial commit"
    git push -u origin master
    

    That will place a file named initial that you can delete later.

    Hope this answer helps! Goodluck!

    In my case, it was my husky package that disallows the push.

    > husky - pre-push hook failed (add --no-verify to bypass)
    > husky - to debug, use 'npm run prepush'
    error: failed to push some refs to 'https://username@bitbucket.org/username/my-api.git'
      

    To push it forcefully, just run git push origin master --no-verify

    I ran npm run prepush to see debug the error, and this was the cause:

    npm ERR! code ELOCKVERIFY
    npm ERR! Errors were found in your npm-shrinkwrap.json, run  npm install  to fix them.
    npm ERR!     Invalid: lock file's loopback-utils@0.8.3 does not satisfy loopback-utils@^0.9.0
    

    Ran npm install and commit it, and the problem is fixed.