Skip to content

Instantly share code, notes, and snippets.

@jeff-hager-dev
Last active August 29, 2015 14:13
Show Gist options
  • Save jeff-hager-dev/87240d42ec59cd4c231e to your computer and use it in GitHub Desktop.
Save jeff-hager-dev/87240d42ec59cd4c231e to your computer and use it in GitHub Desktop.
A collections of git tricks and tips I have collected in my adventures.

Git Tips and Tricks

Branches and Merging

  • List branches that are already merged. (Found this here: Stackoverflow post)

    git branch --merged master #lists branches merged into master
    git branch --merged lists #branches merged into HEAD (i.e. tip of current branch)
    git branch --no-merged #lists branches that have not been merged
  • Merge trunk into branch

    git commit -am "Current Changes"
    git fetch
    git pull origin master
  • See all branches including remote branches

    git branch -a
  • Checkout previous branch. Found this gem in this stackoverflow post

    git checkout -

Files

  • Remove all local untracked files

    git clean -f -d
  • Revert a file to remote version

    git reset --hard origin/<branch>
  • Removes all files that are tracked but are also part of the ignore file.

    git rm -r --cached .
    git add .
    git commit -am "Removing unwanted files"
  • Change the last commit message

    git commit --amend
  • Undo the last commit

    git reset --hard HEAD~1
  • Remove a file from a git add that hasn't been committed yet

    git reset <filename>

MISC

  • Git Pull vs Git Fetch: Pull does a fetch then merge where a Fetch pulls changes but keepsing them in refs/remotes//

  • Rename a local branch

    git branch -m <oldname> <newname>
    git branch -m <newname> #If you want to rename the current branch
  • Get edited line information. Found this GEM in a response to this stackoverflow question.

    git log --author="<author_name>" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'  > output_file.txt
  • Get commit stats

    git shortlog -s -n -e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment