How to sync up new Git repository on GitHub or GitLab

From time to time you necessary to set up new Git repository. This article is about a list of commands which are helpful for creating a new repository and syncing it with GitHub or GitLab.

This article focuses only on users with Mac or Linux. For creating new repository or pairing existing with GitHub account or custom GitLab on Windows, please visit official tutorial at GitHub website.

 

Setting global git config credentials

First of all, we need to be sure that we have set global config information. This is one-time configuration.

git config --global user.name "Your Name"
git config --global user.email "your.name@email.com"
Creating the new repository and pushing new it to GitHub or GitLab

This is the short list of terminal commands for creating a new local repository and syncing the changes with GitHub or GitLab repository.

cd folder
# Change to desired directory 
git init
# Initiate local Git repository
git remote add origin git@github.com:UserName/project.git || git@gitlab.domain.com:UserName/project.git
# Sets the new origin repository
git remote -v
# Verifies the new remote URL
git add .
# Adds all files in the local repository to stage for commit. To unstage a file, use 'git reset HEAD YOUR-FILE'.
git commit -m "init commit"
# First local commit with message. It commits the tracked changes and prepares them to be pushed to a remote repository. 
# To remove this commit and modify the file, use 'git reset --soft HEAD~1'; and commit and add the file again.
git push -u origin master
# Pushes the changes in your local repository up to the remote repository you specified as the origin

Repository SSH link can be found at GitHub or GitLab project landing page or quick setup page.

Copy/Clone existing GitHub or GitLab repository to local folder

This is the short list of terminal commands for cloning existing GitHub or GitLab repository to local repository.

cd folder
# Change to desired directort
git clone git@github.com:UserName/project.git || git@gitlab.domain.com:UserName/project.git
# Clone/Copy all files

Cloning the repository does not make automatically git repository. For that, we need to repeat the step (above) of initializing local Git repository and setting the origin repository on GitHub or GitLab.

Security reminder

Never git add, commitpush sensitive information (like passwords, keys, configs, credit card numbers PINs and numbers, etc.) to your remote repository.

This entry was posted in Solutions and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.