Introduction

Setup git

  • Set up your name and email address
git config --global user.name "YOUR NAME"
git config --global user.email "YOUR EMAIL"
  • Or initialize a new repository (optional)
git init

Create a repository and commit changes

  • Add a new branch
git branch FEATURE
  • Show all branches
git branch
  • Switch to a branch (to FEATURE branch)
git switch FEATURE #git checkout does the same
  • Delete a branch
git branch -d FEATURE

Use git for collaboration

  • Clone a repository from places like Github (optional)
git clone REPOSITORY.git
  • Add and commit changes
git add CHANGED_FILE1 # or git add -A for committing all changes
git commit -m "Initial commit"
git push
  • Pull changes from BRANCH in REPOSITORY to current branch
git pull REPOSITORY BRANCH
  • Peek at changes without merge
git fetch REPOSITORY BRANCH
git log -p HEAD..FETCH_HEAD

Sync two repositories

cd PRIVATE-REPOSITORY
git remote add upstream git@github.com:GITHUB_USERNAME/PUBLIC-REPOSITORY.git
git remote add origin git@github.com:GIGTHUB_USERNAME/PRIVATE-REPOSITORY.git
 
git switch upstream master
git fetch upstream master
git switch master
git merge BRANCH-IN-PUBLIC-REPOSITORY