Git push rejected “non-fast-forward”

后端 未结 12 1482
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 02:01

I am fairly new to git yet currently using it to manage our code in a team environment. I had some rebasing issues and I fixed them using

git ch         


        
相关标签:
12条回答
  • 2020-11-30 02:31

    Probably you did not fetch the remote changes before the rebase or someone pushed new changes (while you were rebasing and trying to push). Try these steps:

    #fetching remote 'feature/my_feature_branch' branch to the 'tmp' local branch 
    git fetch origin feature/my_feature_branch:tmp
    
    #rebasing on local 'tmp' branch
    git rebase tmp
    
    #pushing local changes to the remote
    git push origin HEAD:feature/my_feature_branch
    
    #removing temporary created 'tmp' branch
    git branch -D tmp
    
    0 讨论(0)
  • 2020-11-30 02:33
    1. Undo the local commit. This will just undo the commit and preserves the changes in working copy
    git reset --soft HEAD~1
    
    1. Pull the latest changes
    git pull
    
    1. Now you can commit your changes on top of latest code
    0 讨论(0)
  • 2020-11-30 02:34

    I'm late to the party but I found some useful instructions in github help page and I wanted to share them here.

    Sometimes, Git can't make your change to a remote repository without losing commits. When this happens, your push is refused.

    If another person has pushed to the same branch as you, Git won't be able to push your changes:

    $ git push origin master
    To https://github.com/USERNAME/REPOSITORY.git
     ! [rejected]        master -> master (non-fast-forward)
    error: failed to push some refs to 'https://github.com/USERNAME/REPOSITORY.git'
    To prevent you from losing history, non-fast-forward updates were rejected
    Merge the remote changes (e.g. 'git pull') before pushing again.  See the
    'Note about fast-forwards' section of 'git push --help' for details.
    

    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 YOUR_BRANCH_NAME
    # Merges updates made online with your local work
    

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

    $ git pull origin YOUR_BRANCH_NAME
    # Grabs online updates and merges them with your local work
    
    0 讨论(0)
  • 2020-11-30 02:35
    1. move the code to a new branch - git branch -b tmp_branchyouwantmergedin
    2. change to the branch you want to merge to - git checkout mycoolbranch
    3. reset the branch you want to merge to - git branch reset --hard HEAD
    4. merge the tmp branch into the desired branch - git branch merge tmp_branchyouwantmergedin
    5. push to origin
    0 讨论(0)
  • 2020-11-30 02:43

    In Eclipse do the following:

    GIT Repositories > Remotes > Origin > Right click and say fetch

    GIT Repositories > Remote Tracking > Select your branch and say merge

    Go to project, right click on your file and say Fetch from upstream.

    0 讨论(0)
  • 2020-11-30 02:44

    Write lock on shared local repository

    I had this problem and none of above advises helped me. I was able to fetch everything correctly. But push always failed. It was a local repository located on windows directory with several clients working with it through VMWare shared folder driver. It appeared that one of the systems locked Git repository for writing. After stopping relevant VMWare system, which caused the lock everything repaired immediately. It was almost impossible to figure out, which system causes the error, so I had to stop them one by one until succeeded.

    0 讨论(0)
提交回复
热议问题