11

I am trying to push a newly created repository to gitlab. Here's what I did:

  1. Create a project in gitlab. Example URL: https://gitlab.example.com/group1/project1.git

  2. Initiated a local bare repo:

    cd ~/projects
    git init --bare ./project1.git
    cd project1.git
    

Question:

Next step is to push all of my local repo into gitlab. But I want to specify username and password using https. How do I change the following to do it?

git remote add origin https://gitlab.example.com:group1/project1.git
git push --all
git push --tags
code4kix
  • 3,937
  • 5
  • 29
  • 44

3 Answers3

36

Like others mentioned here, ssh is preferred. But for those you want to use just http/https, you can specify credentials like this:

git remote add origin https://username:password@gitlab.example.com:group1/project1.git

Verified for gitlab 9.0

Note: Please replace '@' symbols in username with '%40' to work this.

Sakkeer Hussain
  • 459
  • 6
  • 19
code4kix
  • 3,937
  • 5
  • 29
  • 44
2

When you created the repo in gitlab, by deafault it will provide to clone git repo using two protocols, ssh and https. Where in https it will prompt your user credential every time you pull or push.

I prefer to use ssh. Where as in ssh you can push lot of files to repo. In https you have size restriction. Perhaps you want push 4gb files to repo.

Follow these steps to set ssh as remote:

git remote rm https://gitlab.example.com:group1/project1.git

git remote set-url origin ssh://user@host:1234/srv/git/example

Useful information:

Worth reading about setting up git remote:

https://help.github.com/articles/changing-a-remote-s-url/

danglingpointer
  • 4,708
  • 3
  • 24
  • 42
2

If you get fatal: remote origin already exists error. Check what origin is set up and remove it

git remote -v
git remote rm origin

Add remote origin for https. You can set up username:password in url but credentials will be seen plain text in git remote -v

git remote add origin https://username:passowrd@git.example.com/group1/project1.git
git remote -v
git push -u origin --all
git push -u origin --tags

I need to use \ after url and before group instead of :, otherwise I get an error URL using bad/illegal format or missing URL.

If you have special characters in password and get an error bash: !: event not found, for example, for '!' use '%21' (google for 'HTML URL encoding reference').

It is possible to add remote without credentials in url

git remote add origin https://gitlab.example.com/group1/project1.git
git config credential.helper store
git config --list

it will prompt for credentials on first login and should not afterwards. store option stores credentials same in plain text in ~/.git-credentials. On Windows to store credentials in "Credential Manager"

 git config credential.helper wincred

you can delete ~/.git-credentials then. To clear credential helper

git config credential.helper ""
Vladislav Povorozniuc
  • 2,149
  • 25
  • 26