Skip to content

Instantly share code, notes, and snippets.

@iAugur
Last active February 4, 2021 10:34
Show Gist options
  • Save iAugur/51e9e5304fa086433104468457c455d4 to your computer and use it in GitHub Desktop.
Save iAugur/51e9e5304fa086433104468457c455d4 to your computer and use it in GitHub Desktop.
# Steps for adding an existing project to a remote git repo service
# Create a repor on a service such as Bitbucket / Github / Gitlab etc
# The we want to add an existing project we have locally to that repo
#
# The steps involved are:
# 1) Intialise a local repo in the folder with the files to be committed
# 2) Create a gitignore file (ensure we don't commit sensitive files)
# 3) Add the files to be committed (ensure we have a gitignore so we don't commit sensitve files)
# 4) Set the remote origin to our service repo (bitbucket/Github etc)
# 5) Push the files to the remote
# Props: https://stackoverflow.com/a/42871621
#
# If you have a local repo with a master branch and you want to rename that to main
# see also https://gist.github.com/iAugur/2f5d98d7f1dc525030c9a0c30a77bd30
#
#---------------------------------------------------------------------------------------------#
# 1) Intialise a local repo in the folder with the files to be committed
#---------------------------------------------------------------------------------------------#
# The following process has two options depending on your git version
# A) Newer version of git (>2.28) that support seeting the initial branch
# B) older git versions that don't!
#---------------------------------------------------------------------------------------------#
# A) From version 2.28.0 the git init command now takes a --initial-branch param
# Initialise the local git repo
git init --initial-branch=main
git checkout -b main
#---------------------------------------------------------------------------------------------#
# B) Older version of git that do not supprt the initial branch param
git init
git checkout -b main
#---------------------------------------------------------------------------------------------#
# 2) Create a gitignore file (ensure we don't commit sensitive files)
#---------------------------------------------------------------------------------------------#
# It is worth at this point creating a standard .gitignore in the folder root to ensure
# sensitive files and images etc are not added to the repo
# A useful service that generated boilerplate .gitignore files fo common project types
# https://www.toptal.com/developers/gitignore
# Review what that suggests and adjust to your taste
#---------------------------------------------------------------------------------------------#
# 3) Add the files to be committed
#---------------------------------------------------------------------------------------------#
# From here on the steps are the same
# add all of the files
git add --all
git commit -m "Initial Commit"
#---------------------------------------------------------------------------------------------#
# 4) Set the remote origin
#---------------------------------------------------------------------------------------------#
# set the remote to your remote
git remote add origin https://{ the path to your remote repo }
#---------------------------------------------------------------------------------------------#
# 5) Push the files to the remote
#---------------------------------------------------------------------------------------------#
# push the files
git push -u origin main
#---------------------------------------------------------------------------------------------#
Notes:
#---------------------------------------------------------------------------------------------#
# If there are changes on the remote repo that cause an Unrelated Histories error when pushing
fatal: refusing to merge unrelated histories
# Then we may need to do a pull first with the --allow-unrelated-histories param
git pull origin main --allow-unrelated-histories
# This is often caused by a README or .gitignore having been created on the remote when created.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment