简体   繁体   中英

Why i can't push my code on my GitHub repositorty after set up my remote?

I am not so into GIT and I have the following problem: I created a new repository on GitHub . Then, into the folder of my project, I have done the following commands:

1) I set up the init repository into my project folder:

git init

2) I set up the reference to my GitHub repository:

git remote add origin https://github.com/AndreaNobili/SpringBoot-Excel-API.git

And retrieving the reference information I obtain this:

developer@developer-virtual-machine:~/git/SOC-dashboard$ git remote -v
origin  https://github.com/AndreaNobili/SpringBoot-Excel-API.git (fetch)
origin  https://github.com/AndreaNobili/SpringBoot-Excel-API.git (push)

so it seems to be fine.

3) First I tried to push my project code:

developer@developer-virtual-machine:~/git/SOC-dashboard$ git push origin master
Username for 'https://github.com': nobili.andrea@gmail.com
Password for 'https://my.mail@gmail.com@github.com': 
To https://github.com/AndreaNobili/SpringBoot-Excel-API.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/AndreaNobili/SpringBoot-Excel-API.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.

I inserted my credentials but I obtain the previous error message. What exactly means?

4) My idea is that I have to perform a pull of the remote repository before push my code (into the GitHub repository I see that there is the .gitignore file.

So I tried to do:

developer@developer-virtual-machine:~/git/SOC-dashboard$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> master

I also tried this:

developer@developer-virtual-machine:~/git/SOC-dashboard$ git pull origin master
Da https://github.com/AndreaNobili/SpringBoot-Excel-API
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

So what is wrong? What am I missing? How can I try to fix it and correctly push my code?

tl;dr When you made your repo on Github it already had a commit in it to add .gitignore. git init + git remote add did not fetch this or any commits, you had a blank local repository. Your local commits were not built on top of the remote commits, they had no ancestor in common, they were "unrelated". Git will not merge unrelated branches.

To fix it, git fetch origin to ensure you have the latest snapshot of the remote, and git rebase origin/master to rewrite your local commits on top of the origin's master branch. Then you can push.

In the future, download a new repository using git clone .


$ git push origin master
Username for 'https://github.com': nobili.andrea@gmail.com
Password for 'https://my.mail@gmail.com@github.com': 
To https://github.com/AndreaNobili/SpringBoot-Excel-API.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/AndreaNobili/SpringBoot-Excel-API.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.

All the information is there, but it's a bit cryptic. The important bit is this:

 ! [rejected]        master -> master (non-fast-forward)

A "fast-foward" is when there is no merge required to update a branch.

For example. You clone a repository that has three commits, A, B, and C. You get a complete copy of the repository at that moment.

$ git clone <some repo>

origin
A - B - C [master]

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

origin/master tracks the last place you saw the remote repository's master branch.

You add a few commits locally.

origin
A - B - C [master]

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

You push. This push can happen because Git only has to stack D and E on top of E. It is a "fast-foward".

$ git push origin master

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

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

The problem comes when your histories have diverged . This happens when someone else commits to the remote repository since you've made changes.

Let's go back to a fresh clone.

$ git clone <some repo>

origin
A - B - C [master]

local
A - B - C [master]

You add a few commits locally.

origin
A - B - C [master]

local
A - B - C - D - E [master]

Someone else pushes some commits.

origin
A - B - C - F - G [master]

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

You try to push and you'll get ! [rejected] master -> master (non-fast-forward) ! [rejected] master -> master (non-fast-forward) . Your histories have diverged and need to be merged. git push will not do that, you must git pull and deal with any conflicts.

$ git pull

origin
A - B - C - F - G [master]

local
A - B - C - F - G [origin/master]
         \       \
          D - E - M [master]

Now you can push.

$ git pull

origin
A - B - C - F - G
         \       \
          D - E - M [master]

local
A - B - C - F - G [origin/master]
         \       \
          D - E - M [master]

So who made that divergent change? You did. When you created the repository on Github it made an initial commit to add the .gitignore file .

If you'd cloned the repository you'd be fine, like above. But you instead initialized and added a remote.

$ git init
$ git remote add origin https://github.com/AndreaNobili/SpringBoot-Excel-API.git

origin
A [master]

local

origin has a commit, but your local repository has none. Git does not talk to remotes without your explicitly telling it to. You'd have to git pull at this point. Or do git clone instead.

Now you've added more commits.

$ git init
$ git remote add origin https://github.com/AndreaNobili/SpringBoot-Excel-API.git

origin
A [master]

local
B - C [master]

Note that there is no relation between your commits on origin and your local commits. They have no commit in common. They're "unrelated".

Your try to push and get ! [rejected] master -> master (non-fast-forward) ! [rejected] master -> master (non-fast-forward) because history has diverged.

You try to pull and get fatal: refusing to merge unrelated histories because your local commits and the commits on origin are unrelated. Git does not know how to merge them.


To fix this, first fetch the latest changes from origin.

$ git fetch origin

origin
A [master]

local
B - C [master]

A [origin/master]   # the unrelated commit fetched from origin

Then rebase your changes on top of them. rebase basically copies your changes onto an unrelated commit.

$ git rebase origin/master

origin
A [master]

local
A [origin/master]
 \
  B1 - C1 [master]

And now you can push.

(You can also do the fetch + rebase in one step: git pull --rebase origin master . A git pull is a fetch + a merge, or with --rebase a rebase.)

In the future, download a new repository with git clone .

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