Author: Richard Haines

Posted: 27 Mar 2020

take me there

This is a collection of notes for everyday git commands.

Add an existing project to git

Create a new repository on github, then from the projects root:

1git init
2git add .
3git commit -m "My first commit"
4git remote add origin <remote repository url>
5git remote -v
6git push origin master
  • First we are initializing the local directory as a git repo
  • Then we are adding the new files in your local repo, this stages them for the first commit
  • Then we commit the files that we just staged
  • Copy the remote repo url from your github repo and add it to the remote repo where your local repo will be published
  • Set the new remote
  • Push the changes in your local repo to github

Amend a commit message

1git commit --amend

Commit your work

1git add .
2git commit -m "my commit message"
3git push

Squash commits

Check how many commits have been made:

1git rev-list HEAD --count

Use that number to tell git how many commits to squash into one:

1git rebase -i HEAD~<number-of-commits>

Create a new branch

To create a new branch checkout the branch you want to base your work on:

1git checkout myOriginalBranch

Then its as simple as doing a pull to make sure you have the latest work and adding the -b tag with your new branches name:

1git pull
2git checkout -b myNewBranch

Clone repo and push to new repo

Create a new repo on github then clone the repo you want to copy.

1git clone <repo-url> // clone the repo
2git remote rename origin upstream // rename the repo upstream
3git remote add origin <new-repo-url> // add the new repos url to your remote
4git push origin master // push the changes to your remote repo
5git pull upstream master && git push origin master
Edit on GitHub.Previous: React context with TypeScriptNext: Text with linear gradient background