Create a new repository

  1. Create a new empty repo in your account on the GitHub website.
  2. Locally, in your new project directory, open Git Bash and run the following commands:
echo "Hello, world!" >> README.md
git init
git add .
git commit -m "First commit"
git branch -M main
git remote add origin https://github.com/<username>/<repo-name>.git
git push -u origin main

Push an existing project to a remote repo

  1. Create a new empty repo in your account on the GitHub website.
  2. Locally, in your project directory, open Git Bash and run the following commands:
git init -b main
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/<username>/<repo-name>.git
git push -u origin main

If you’re remote repo has preexisting files, run a pull command before you push.

git pull origin main

Other Useful Commands

In case of a “fatal: refusing to merge unrelated histories” Git error, run this command:

git pull origin main --allow-unrelated-histories
git push -u origin main

Branch Name

git branch

Rebase

git pull --rebase origin main
git push -u origin main

Rename current master branch to main

git branch -m main

Move the ‘master’ branch to ‘main’

git branch -m master main

Point HEAD to ‘main’ branch

git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main

Leave a Reply

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