Git

Version control tool

Configuration

git config
git config --global user.name "derekz"
git config --global user.email derekz@derekzoladz.com

# Remember my creds for 15 minutes
git config credential.helper cache

# Store my creds in a plain-text .git-credentials file
git config --global credential.helper store

Clone

# Clone a Repository and history
git clone https://github.com/dzoladz/dzoladz.git

# Clone & Rename to 'website'
git clone https://github.com/dzoladz/dzoladz.git website

# Clone a branch, <branch> <remote repo>
git clone -b collab/berick/ansible-installer-ubuntu-16.04  git://git.evergreen-ils.org/working/random.git

Basics

Initialize Git Repository

mkdir repo_name
cd repo_name
git init

Remotes

# Add initial remote named 'origin'
git remote add origin https://github.com/dzoladz/dzoladz.git

# Rename remote 'origin' to 'upstream'
git remote rename origin upstream

# List all remotes
git remote -v

# Remote remote named 'upstream'
git remote rm upstream

Files

# List new and modified files, branch, etc...
git status

# Track file.txt
git add file.txt

# Add all untracked files to be staged for commit
git add .

# Show file differences between previous version and working version  
git diff

# Remove file.txt from staging for commit
git reset file.txt

# Remove file.txt from tracking
git rm --cached file.txt

# Undo act of commit
git reset --soft HEAD^

# Rename file.txt
git mv file.txt newfile.txt

# Commit tracked files to repo
git commit -m "COMMIT MESSAGE"

# Push files to remote repository
git push <remote_name> <branch_name>

# Incorporate changes from remote repository
git pull

Branches

# List all branches in the local repository
git branch

# Show all local and remote branches
git branch -a

# Create a branch named 'bugfix'
git branch bugfix

# Switch to branch 'bugfix'
git checkout bugfix

# Delete branch 'bugfix'
git branch -d bugfix

# Merge bugfix into master
git checkout master # local working master branch
git pull origin master # incorporate remote updates
git merge bugfix # merge changes into master
git push origin master # push to remote

Clean up a dirty pull

git stash git pull origin master git stash pop

Search Repo

# Case-insensitive search for files containing `commit`
git grep -i 'commit'

# List files in repo using file globbing
git ls-files *.py

History

  1. Viewing git logs
# List commit history of current branch
git log

# Show commit d2b557da6450c3dc77766b86d6d7b832af6d3d6a
git show d2b557da6450c3dc77766b86d6d7b832af6d3d6a

# Show commits by specific author
git log --author=dzoladz

# Display commit history in a specific format
git log --pretty=oneline
git log --pretty=short
git log --pretty=format:"%h%x09%an%x09%s"
  1. Amending commit authors
# <commit-hash> must be hash before the commit to edit
git rebase -i --rebase-merges <commit-hash> --committer-date-is-author-date

# If you need from the beginning of time, use --root
git rebase -i --rebase-merges --root --committer-date-is-author-date

# Flag commits for edit, and amend the author
git commit --amend --author "derek <derek@oplin.ohio.gov>" --no-edit && git rebase --continue

Just for Fun

# Who has the most commits in this repo
git shortlog -s -n