annika-w
8/23/2017 - 10:05 AM

Git

A bunch of my most common Git commands.

Branches

git checkout -b my_branch       # Create and checkout new branch
git push origin my_branch       # Push to remote repo
git checkout another_branch     # Checkout local copy of a branch
git branch                      # Show all branches

git branch -m old_name new_name # Rename branch
git branch -d branch_name       # Delete after changes have been pushed
git branch -D branch_name       # Force delete local branch

# Push own local branch to remote without merging into master or another branch
git push origin local_branch_name:remote_branch_name

Updating local Branch with remote changes to master

git fetch origin  # Fetch changes from remote master, nothing applied to any branch here.
# Checkout local copy of master and merge with fetched changes to master.
git checkout master                 
git merge origin/master
# Checkout local copy of my branch and merge with fetched changes to master.
git checkout branch_name
git merge --no-ff origin/master
# Run into merge conflicts? Resolve and then:
git add file1.c file2.c
git commit -a -m "resolved merge conflicts."
# DONE.

Fast Forward:

  • --ff-only: Defaul, try fast-forward not preserving branching history.
  • --no-ff: Don't just join commits in branch and master.

Basics

Push Changes to Remote

git status
git add some/dir
git add *
git commit -a -m "commit message"
git push origin master
git push -u origin newBranch  # to push own branch to remote without merging.

Checkout single file from another branch

git checkout my-branch
git checkout another-branch path/to/file

Cloning and Pulling

git clone https://github.com/annis/project
git pull origin

Initialize Repo

git init
git remote set-url origin https://github.com/AnnikaWierichs/some_project

Cache Login and PW for X Minutes

git config credential.helper store
git config --global credential.helper 'cache --timeout 7200'    # for 2 hours

Deleting, reverting, resetting.bash

git checkout -- *   # revert uncommitted unstaged changes
git reset --hard    # revert uncommitted staged & unstaged changes
git clean -fd       # remove all untracked files and dirs

Sparse Checkout

mkdir my_repo
cd my_repo
git init
git remote add -f origin <url>
git config core.sparseCheckout true
echo "some/dir/" >> .git/info/sparse-checkout
git pull origin master