Saturday, December 27, 2014

Add a new GIT project to BitBucket

BitBucket is a great solution for adding a project to source control, like GIT. But I had some problems adding an existing project to a remote repository. What BitBucket suggested me didn't work. This is how to solve this problems.
Suppose you have created a new repository called "myproject" on BitBucket.
The full repository url could be something like:

https://redpelle@bitbucket.org/redpelle/myproject.git

BitBucket suggests this procedure in order to add files to your repository (that is my case didn't work):

BitBucket procedure:

cd /path/to/my/project
git remote add origin https://redpelle@bitbucket.org/redpelle/myproject.git
git push -u origin --all # pushes up the repo and its refs for the first time
git push -u origin --tags # pushes up any tags

Instead of that, here is the procedure that worked for me (type from command line).

Correct procedure:

cd /path/to/my/project
git init
git remote add origin https://redpelle@bitbucket.org/redpelle/myproject.git
git add -A
git commit -am "Fist commit"
git push origin master

This allows you to go to your directory, init the git environment is that directory, add the remote origin, add all files to git, in order to let them get versioned, commit all changes, with a message "First commit" and then push all these files on remote repository.

If you already have files in remote repository, when pushing an error could be thrown, like this:

From https://bitbucket.org/redpelle/myproject
 * [new branch]      master     -> origin/master
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  

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

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

In this case you can just run the command:

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

NOTE: It could happen that using git pull you have an error:
fatal: refusing to merge unrelated histories
It can be caused by the presence of a .gitignore file. In this case you can run:

git pull origin master --allow-unrelated-histories
then you can manually solve the conflict or, if you want to solve conflicts by keeping remote version of conflicting files, you can run directly:
git pull -X theirs origin master --allow-unrelated-histories
then you can push again:
git push origin master


No comments:

Post a Comment

(c) Copyright 2020 - MyTroubleshooting.com