Everyday GIT commands

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:

GNU Bash icon
git init
git add .
git commit -m "My first commit"
git remote add origin <remote repository url>
git remote -v
git 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

GNU Bash icon
git commit --amend

Commit your work

GNU Bash icon
git add .
git commit -m "my commit message"
git push

Squash commits

Check how many commits have been made:

GNU Bash icon
git rev-list HEAD --count

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

GNU Bash icon
git 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:

GNU Bash icon
git 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:

GNU Bash icon
git pull
git checkout -b myNewBranch

Clone repo and push to new repo

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

GNU Bash icon
git clone <repo-url> // clone the repo
git remote rename origin upstream // rename the repo upstream
git remote add origin <new-repo-url> // add the new repos url to your remote
git push origin master // push the changes to your remote repo
git pull upstream master && git push origin master