Search code examples
gitbitbucketgit-pushgit-pull

Can't add new file to remote repo, says I need to pull but already did


For a personal project where I'm the only one creating code, I sometimes switch between two different IDEs. As such, the source code is stored locally in two different places. I would like to have a history/backup of the code on Bitbucket. Is Git able to handle this?

I have foo.cpp in the directory source_code. I do not want to copy everything from the remote repo into source_code. After adding and committing foo.cpp I try to run git push origin master and it gives the error

 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://bitbucket.org/...'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Like it recommends I do git pull origin master but it errors with

error: Pulling is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.

What exactly does unmerged mean in this context? How can I find specifically what files are the issue? I'm guessing the problem is I have files with same name but I only want one specific file tracked from the directory. Such a simple task I would expect to be easy.

When I do git status it gives

You are currently rebasing branch 'master' on '930b2f7'.
  (fix conflicts and then run "git rebase --continue")
  (use "git rebase --skip" to skip this patch)
  (use "git rebase --abort" to check out the original branch)

Unmerged paths:
  (use "git restore --staged <file>..." to unstage)
  (use "git add <file>..." to mark resolution)
        both added:      foo2.cpp

Bitbucket's version of foo2.cpp was correct so I deleted the local one.I then ran $ git restore --staged foo2.cpp which worked. Now git pull origin master works. But still I can't push.

 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://bitbucket.org/...'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Again, all I'm trying to do is add a file to the master branch on the remote repo. (Aside, is "remote repo" even the right term to use to refer to a specific project on a hosting service such as Bitbucket?)

Also it says (master|REBASE 2/3) which I have no clue what it means.


Solution

  • Git is Good.

    Git works very hard to ensure the integrity of your baseline, and of your baseline's history.

    You just had a Series of Unfortunate Events, I'm sorry to say :(

    You also brought up a couple of good questions:

    • Q1: It's normal to develop with an IDE. I use Netbeans. Do most people setup git at this location, i.e. do they go into the project directory and run git init...?

      SHORT ANSWER: You'll generally let the IDE create your local repo for you

    • Q2: Git is far too dangerous to use on important files without being certain of what each command does. How does one learn it safely?

      SHORT ANSWER: Experiment with some "hello world" projects and a good Git tutorial; you can always make backup copies of your project as you do so.

    SUGGESTIONS:

    1. Using Git with your IDE:

      • By all means, use your IDE (e.g. Netbeans). I happen to mostly use Eclipse for Java, and MSVS for Windows).
      • I would create your projects and initialize Git, all in the IDE.
      • Assuming you're on Windows, I would also download and install either/both of:
      • You'll discover that you can use ANY of these Git clients, in ANY combination, with your work.
    2. Git is completely agnostic about which client(s) you use.

      Once you've started a project and downloaded one or both of the "other" Git clients, browse to your project's directory and satisfy yourself they're all "seeing the same thing".

    3. Familiarize yourself with some basic Git workflows

      • Once you've created your project, find a good tutorial and start playing with Git features. Locally, and/or with Github or BitBucket.

      • Checkin, checkout, push, pull, "reset --hard", create branches, merge branches, etc. etc.

      • Each step of the way ... "cd" to your project's physical directory ... and make a secondary copy. Once you gain confidence, you'll discover this "extra backup" is unnecessary. Until then, it can be a lifesaver :)

      • Always remember that at any time, for any reason, as long as you have your hidden ".git" subdirectory, you can always restore your latest commit:

        git reset --hard

    'Hope that helps...