Git and Remotes

Lessons learned :

  • When using SSH as the protocol to interact with the Git repository, make sure you include the ".git" suffix as part of the URL specification to the "git remote add" command.  You can verify this by using git remote -v and look for .git suffix.  
$ git remote add origin git@gitlab.com:<namespace>/<repo>.git

$ git remote -v
origin	git@gitlab.com:<namespace>/testrepo.git (fetch)
origin	git@gitlab.com:<namespace>/testrepo.git (push)
  • For a visual, pattern-matching rule of thumb, if the git remote URL starts with git, it needs to end with .git.  (Otherwise, it is an HTTP protocol and it should not end in .git)
  • When setting the upstream connection for a branch, use "git push -u [--set-upstream]":
$ git push -u <remote-URL.git> <branch> # -- or 
$ git push --set-upstream <remote-URL.git> <branch>

# for example
$ git push -u git@gitlab.com:<namespace>/testrepo.git master

# or, rely on a remote alias (as "origin" set above)
$ git push -u origin master
  • When using the command line, it's better to be explicit about which remote and what branch rather than rely on Git defaults for things like push, pull, etc
$ git push origin master

It's possible to do the following to create a new GitLab project from the command line:

$ mkdir testrepo
$ cd !$
$ git init
$ git remote add origin git@gitlab.com:<namespace>/<project>.git

... work, git add, git commit
$ git push -u origin master

# verify upstream branch is configured
$ git branch -vv
* master 96d323a [origin/master] :tada: fixed

# more validation of the repo config
$ more .git/config
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
[remote "origin"]
	url = git@gitlab.com:<namespace>/testrepo.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master

Notes:

  • By default the repo will be created in GtiLab as a "private" project.  This can be changed later.
  • When using the HTTP protocol to interact with the Git Repo, you do NOT include the ".git" suffix as part of the <URL>.  But, when using SSH protocol you MUST include the ".git" suffix as part of the <URL>.  
  • A lot of the git-scm documentation mixes the use of https vs ssh.  So, if you're not paying attention, you may miss this subtlety.

Questions:

  • Can we set upstream repos/branches for each branch?  (I'm pretty sure we can, but I need to verify.)  

References: