Git: Git Immersion Notes April 2nd, 2015

Commands & Notes

  • git add <FILENAME> to add a single file or git add . to add all files to staging
  • git commit -m “MESSAGE” to commit in one step without entering editor mode
  • Git history commands:
    git log
    git log --pretty=oneline
    git log --pretty=oneline --max-count=2
    git log --pretty=oneline --since='5 minutes ago'
    git log --pretty=oneline --until='5 minutes ago'
    git log --pretty=oneline —author=webninjataylor
    git log --pretty=oneline —all
    git log --all --pretty=format:'%h %cd %s (%an)' --since='7 days ago’
    git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
    
  • master is the name of the default branch
  • By checking out a branch by name, you go to the latest version of that branch
  • git tag <TAG-NAME> = Create a new tag
  • git checkout <TAG-NAME> = Check out a branch by tag
  • git checkout <BRANCH-NAME> = Check out a branch by branch name
  • git tag = Show all tags
  • Push tags to GitHub…
    git push --tags
  • View history in both directions from current spot…
    git hist master --all
  • git checkout <FILE> to revert changes
  • git reset HEAD <FILE> to pull file out of staging
  • git revert HEAD to revert commit just made
  • git revert <HASH> to revert to a specific commit
  • Using the hard parameter indicates that the working directory should be updated to be consistent with the new branch head…
    git reset --hard <HASH-OR-TAG>
  • git tag -d TAGNAME to remove unneeded tag names to aide garbage collection
  • Amend previous commit before pushing with
    git commit --amend -m “COMMENT”
  • git mv <FROM> <TO> to move a file
  • cat .git/HEAD shows the branch HEAD is currently referencing which is master by default
  • git cat-file -t <HASH> and git cat-file -p <hash> to view the contents of Git objects; which is all blobs, trees, and commits
  • git checkout -b <BRANCH-NAME> to create a new branch
  • View log of commits on ALL branches, not just the current branch (assumes use of “hist” alias in .gitconfig)
    git hist --all
  • git merge master when on a branch to merge master into it
  • git merge <BRANCH-NAME> when on master to merge the branch into it
  • Fix merge conflicts manually <<<<<<< HEAD … ======= … >>>>>>> master
  • Rebasing vs. merging (git rebase master)
    • The commit tree for the branch is rewritten so that the master branch is a part of the commit history
    • Don’t use for public repos

Config

You can add aliases to the .gitconfig file

[alias]
  co = checkout
  ci = commit
  st = status
  br = branch
  hist = log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
  type = cat-file -t
  dump = cat-file -p

Resources