Search code examples
gitgithubnetbeans

Git error while push, it recommand to pull, but pull also fails


Question: When git push throws error.

sudhakar@featuriz-ubuntu:~/NB/2023/pro$ git push                                                                                                                      
Password for 'https://featuriz@github.com': 
To https://github.com/featuriz/pro.git
 ! [rejected]        development -> development (non-fast-forward)
error: failed to push some refs to 'https://github.com/featuriz/pro.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

As per this when I git pull

sudhakar@featuriz-ubuntu:~/NB/2023/pro$ git pull
Password for 'https://featuriz@github.com': 
From https://github.com/featuriz/pro
 * [new branch]      Modified   -> origin/Modified
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint: 
hint:   git config pull.rebase false  # merge (the default strategy)
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

NOTE:- What I want is, my desktop content. It should be updated to github. local -> remote

As per Git Doc:-

The git pull command is used to fetch and download content 
from a remote repository and immediately update 
the local repository to match that content. 
Merging remote upstream changes into your local repository is a 
common task in Git-based collaboration work flows.

I'm afraid to play with git commands


Solution

  • This means there are new commits on the remote branch origin/development ("upstream"), but also new commits on your local branch development.

    There are different ways to resolve this, depending on what you really want:

    a) I want to push my new commits without deleting the new commits from the remote branch. I'm okay with the creation of a merge-commit that will consolidate the two different commit histories back into one.

    git pull --no-ff
    git push origin HEAD
    

    b) I want to push my new commits without deleting the new commits from the remote branch. I do not want a merge-commit to be created. Instead I just want to (automatically) recreate my commits on top of the new commits from the remote, so that there is no split in history (and therefore no merge required).

    git pull --rebase
    git push origin HEAD
    

    c) I want to push my new commits and DELETE(!!!) the new commits from the remote branch.

    git push --force-with-lease origin HEAD
    

    Note that in cases a) and b) there might be merge-conflicts that need resolving if the new commits of both sides modified the same files/code.

    However, in case of c) the new commits from the remote side will be removed from the remote branch. Which is usually not what you want in a repository you share with other people, except when those commits were added by somebody by mistake.