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:
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
Commit your work
git add .
git commit -m "my commit message"
git push
Squash commits
Check how many commits have been made:
git rev-list HEAD --count
Use that number to tell git how many commits to squash into one:
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:
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:
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.
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
Update local feature branch with remote
If you are working on a feature and need to pull in the latest changes from the remote main
branch.
git checkout main
git fetch -p origin
git merge origin/main
git checkout <feature-branch>
git merge main
git push origin <feature-branch>