简体   繁体   中英

how to fix the non-fast-forward in git without push --force

i have problem that i tried to add commit to my repo at github that had commits but i get this see the image of the git bash i see hints of error and i pull before i tried to push again but noting happen, i see a lot of answers but none fix my problem also i dont need to use as i don't need to remove the commits in the repo


- the commands i used
1- $ git -u push origin master
2- $ git pull origin master


Notice : i have been pull before i take the screenshot and it load normally.
link of my repo at github github link

thank you.

I'm guessing you have cloned a project from a new directory or similar? Or started a new project without pulling first.

You can try pulling with with this flag --allow-unrelated-histories to merge two unrelated git tree histories. Although it's not great practice and can lead to issues in the future.

git pull origin master --allow-unrelated-histories

Your remote repo on Github has two commits, let's call them A and B. Your local repo has entirely different commits, let's call them C and D. It looks like this.

A - B [origin/master]

C - D [master]

You can use git log --decorate --all --graph to see a similar complete view of your repository.

master is your local repository's master branch, origin/master is where your local repo last saw master on Github.

Since they have nothing in common, Git will not merge them. How you deal with this situation depends on how you got into it, what's in C and D, and what you want to keep.

Do not start running commands until you decide what you want to keep.


One way to get into this is instead of using git clone <repo-url> you used git init and then git remote add <repo-url> . This results in an empty local repository. You'd need to git fetch before working to fetch the content of the remote repository.

See this answer for more on that situation.


If you want to preserve all your commits, rebase your local changes on top of the remote ones. This will make it as if you'd done your local work on top of the remote code all along.

$ git rebase origin/master

A - B [origin/master]
     \
      C - D [master]

$ git push

A - B - C - D [master]
              [origin/master]

There may be conflicts if C and D duplicated work in A and B, such as different README.md files. You'll have to resolve them.


If you want to junk the remote changes, git push --force to replace the whole master branch with your own.

$ git push --force

C - D [origin/master]
      [master]

If you want to junk your local changes, use git reset to move your local master branch to the same commit as the remote.

$ git reset --hard origin/master

A - B [origin/master]
      [master]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM