Upload your local project to GitLab, GitHub or Bitbucket using the command line


In this tutorial we will upload our local project to GitLab. GitHub or Bitbucket support Git as version control system as well so you can use the same commands for all three plattforms

Before we start working with Git, it is important to know that both your local folder and the remote folder have to be a Git folder. A Git folder always contains a folder named .git that contains other folders, such as hooks, info, logs, objects, refs and other files

We change our current folder to our local project and initialize the project directory as a Git folder

$ cd /path/to/repository
$ git init

Stage all files for the first commit

$git add .

You can also verify your status

$git status

Commit files

$git commit -m ‘First commit’

Important: Using double quotation marks e.g. “First commit” leads to the following error:

error: pathspec ‘commit’ did not match any file(s) known to git

Use single quotation marks instead e.g. ‘First commit’

If this is your first commit, you may be asked for your credentials

$git commit -m ‘First Commit’
Author identity unknown

*** Please tell me who you are.

Run

git config –global user.email “you@example.com”
git config –global user.name “Your Name”

to set your account’s default identity.
Omit –global to set the identity only in this repository.

fatal: unable to auto-detect email address (got ‘you@computername.(none)’)

Add the url of the remote Git repository

$git remote add origin https://gitlab.com/all-projects/projectX.git

Note: To get your remote URL, you need to create a project in GitLab, GitHub, or Bitbucket

You can verify the url of the remote repository with

$git remote -v

Finally, upload all files to your GitLab remote repository

$git push origin master

Sometimes many other developers are working on the same project and have comitted changes to the same remote Git repository, in this case, Git issues a non-fast-forward error´´. The solution to that problem is just to update your local repository

$git pull origin master
#git pull is a shortcut for git fetch + git merge

and then try again

$git push origin master

Another common mistake occurs when you try to update your local project (fatal: refusing to merge unrelated histories). In this case you can use following flag –allow-unrelated-histories

$git pull origin master –allow-unrelated-histories

Remember, it is considered a bad practice to use the –force flag

$git push –force origin master

Because using –force can mess up the commit history and cause problems in the future

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">