0

OK, I have very limited experience with Git and very limited patience with it because the errors it provides aren't in plain and simple English. I'm just trying to upload my code to our internal Bitbucket server - plain and simple.

I created a repository in BitBucket and ran the following:

cd existing-project

git init

git add --all

git commit -m "Initial Commit"

git remote add origin https://username@bitbucket.company.org/scm/co/project-0987654321.git

git push -u origin master

The problem is, I realised I got the repository name wrong when creating it within Bitbucket, so I deleted the repository and recreated it with the correct name, and now when running the above on my local machine it says:

"On branch master

Initial commit

nothing to commit"

When I add the Bitbucket origin, it says:

"fatal: remote origin already exists"

And when I run the git push, it says:

"error: src refspec master does not match any

error: failed to push some refs to 'https://username@bitbucket.company.org/scm/co/project-0987654321.git'"

So how can I get it back to a "normal, ready to commit, good to go" state on the local machine? I've obviously butchered something in my inexperience.

EDIT:

"git status" says I'm:

On branch master

No commits yet.

So I run: "git branch --set-upstream-to origin/master" as per git status (nothing to commit, working directory clean), however with changes commited, and I get:

branch 'master' does not exist

It literally makes no sense. I've tried every variation of deleting the local .git repository and the Bitbucket repository and trying from scratch to no avail.

It's almost as if the files are "tainted" and will not upload.

Bring back the days of copy and paste... that's literally all I want to do. I've never known a system like this.

RobTheRobot16
  • 323
  • 4
  • 24
  • One other item might matter here, besides what I put in my answer: what version of Git are you running locally? If it's before Git 2.0, be aware that `git add --all` behaves differently in older versions of Git. That's probably not relevant since you don't show a bunch of subdirectories here, but it's worth a comment. – torek Sep 17 '20 at 22:10
  • `git push -u origin master:master`. There's nothing on the bitbucket repo yet. – jthill Sep 18 '20 at 03:45
  • @torek - git version 2.21.0.windows.1. – RobTheRobot16 Sep 18 '20 at 09:47

2 Answers2

2

This part:

On branch master

Initial commit

nothing to commit

is bad news: it means you're in a totally empty repository, i.e., one that has no commits, and there are no files to commit.

If you have files in your work-tree that have useful data, make sure you keep them for now, because right now, this Git repository has nothing at all in it. (Files in your work-tree are not actually in the repository.)

This status—initial commit and nothing to commit—should not happen after:

git commit -m "Initial Commit"

so something is seriously wrong here, but from what you've included in your question, it's not clear what. My suspicion is that in this phrase:

I deleted it and recreated it with the correct name

we've papered over some sort of mortal Git sin , perhaps via the pronoun "it". What "it" was it that you deleted? If you merely deleted the Bitbucket repository through their web interface, that should be OK, but then you should not be in this Initial commit state.

If you deleted both repositories—the local one and the Bitbucket one—and then re-created the local repository, that would also be OK, though you'd need to get all your content from somewhere (another copy of the files, perhaps). But that would have eliminated the:

fatal: remote origin already exists

error, so that can't be the case either.

What will do this is:

$ mkdir t
$ cd t
$ git init
Initialized empty Git repository in [path]
$ git add --all
$ git commit -m "Initial Commit"
On branch master

Initial commit

nothing to commit
$ 

This is happening because git add --all did not add anything. There are no files. The work-tree is entirely empty (except for the hidden .git directory that contains the repository itself):

$ ls -a
.       ..      .git

If this is the case, the solution is to obtain all the work-tree files from somewhere else, and use the git add --all step again before your first commit.

torek
  • 448,244
  • 59
  • 642
  • 775
  • Sorry, I've edited the initial post to make it clearer: I deleted the Bitbucket repository and recreated it with the correct name. I then tried running the commands that Bitbucket provided to push my local repo. Unfortunately, this is where I'm running into the errors. I've tried deleting the local .git directory as well to no avail, as well as copying the files to a completely different directory and then attempting to push the files to Bitbucket - still the same. Whatever I've done has really buggered the whole thing :) – RobTheRobot16 Sep 18 '20 at 09:55
  • You still have no commits yet. There are no files to commit, and `git commit` won't make an empty commit (you can convince it to do so, but there's no real point: a commit that contains no files isn't exactly *useful*). – torek Sep 18 '20 at 10:06
  • So I don't really understand how to get out of this loop from hell. "git status" says I'm "On branch master" with "No commits yet". OK... so I run: "git branch --set-upstream-to origin/master" as per https://stackoverflow.com/questions/37269222/git-status-nothing-to-commit-working-directory-clean-however-with-changes-co, and I get "branch 'master' does not exist". It literally makes no sense. – RobTheRobot16 Sep 18 '20 at 10:35
  • A repository is basically a collection of commits. When a repository has no commits in it—as is your case—it has no actual function, and you can't have any branches. You must make the first commit. Making that first commit makes the branch `master` come into existence. You're "on" master, but it doesn't exist yet. Creating that first commit is the bootstrapping step that allows sanity: until then, you're in this crazy state. – torek Sep 18 '20 at 11:04
  • For this particular reason, GitHub (at least, maybe Bitbucket too) has a mode where when you create a repository, it also adds one initial commit, so that you're not stuck in this weird state. I don't know if you used that mode, or if it even exists on Bitbucket. But you are using raw Git, which doesn't have that extra feature. – torek Sep 18 '20 at 11:05
  • So you say to get the work-tree files from somewhere else - but where? If I only have 1 version of these files and copy/pasting them into completely different directories doesn't work, I don't really understand how I can make this first commit. Like I say, it's like the files are "tainted". – RobTheRobot16 Sep 18 '20 at 13:28
  • I can't see your system and don't know why `git add --all` isn't adding work-tree files, but it clearly is not finding any files to add. What files are in your work-tree? (Where is your work-tree? The top level of your work-tree would normally also have the hidden `.git` directory in it.) – torek Sep 18 '20 at 18:27
  • At the moment my work-tree is in a folder called "Git Test" on my desktop. Within that folder I have the .git directory and all of the files/folders I'm trying to upload to Bitbucket. – RobTheRobot16 Sep 22 '20 at 08:40
  • Curious ... so, if you have a file named "file.ext" and you run an explicit `git add file.ext`, does `git add` print anything? If your shell is /bin/sh or compatible (e.g., bash-for-Windows), what does `GIT_TRACE=1 git add file.ext` show? – torek Sep 22 '20 at 08:56
  • OK, I give up on life =) Running your suggestion showed me that adding the explicit file was working, but for some reason git add --all wasn't. It was then I noticed the gitignore file... which had an asterisk in it. Thanks for the help! Time to choose another profession, I think... :) – RobTheRobot16 Sep 22 '20 at 10:29
  • 1
    That's also interesting, because that kind of `git add ` normally generates a gripe that it's not being added because it matches an ignore expression, along with advice to use `git add --force` if desired. However, the fact that Windows "cmd" shell does not expand `*` does explain why neither `git add *` nor `git add --all` would do anything here. – torek Sep 22 '20 at 12:39
1

Remove and add the origin again, then push your code. Or create a new remote with a name other than origin.

Edit: When you do your initial push to the new repo after recreating origin, do git push -u origin master to set the upstream reference for the master branch. git clone will do this for you automatically for the master branch (and any other branch that exists in the remote repo at the time of clone), but git add remote does not.

codewario
  • 19,553
  • 20
  • 90
  • 159
  • Thanks! Though having just tried that with "git remote rm origin" it still says the exact same errors. It seems to be that it's not detecting any commits from the local machine - that there aren't any changes pending. Is there something else I need to clear? – RobTheRobot16 Sep 17 '20 at 14:21
  • Does `git push -u origin master` work? That should set the upstream reference since there is no master branch to push to at the moment. – codewario Sep 17 '20 at 16:40
  • Nope. git push -u origin master gives the exact same error message: "failed to push some refs to..." – RobTheRobot16 Sep 18 '20 at 09:50