Skip to content

Instantly share code, notes, and snippets.

@codfish
Last active August 3, 2023 15:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codfish/87ddae4b7fdbb3decf07 to your computer and use it in GitHub Desktop.
Save codfish/87ddae4b7fdbb3decf07 to your computer and use it in GitHub Desktop.
GIT Cheat Sheet & Reference

GIT Cheat Sheet & Reference

Here's some helpful git examples and reference for everyday issues.

# set new remote url
git remote set-url origin <remote_git_url>

# Update the latest commit
git commit --amend

# get change history for a specific file in repo
git log -p -- <filename>

# get a list of commit hashes from your current branch based on a grep search
# `git l` is an alias: log --graph --pretty='format:%C(yellow)%h %Creset- %s %C(cyan)<%an>%C(magenta)%d'
git l | grep "<search term>" | awk '{print $2}'

# output the above list of git hashes in reverse (from oldest to newest)
# i've used this before if i needed to get a list of commits that I needed
# to cherry-pick in the proper order
git log --oneline --no-merges | grep '<search term>' | awk '{print $1}' | tail -r | xargs echo

# Get an alphabetical list of files that a specific string is found within your repo
git grep '<search term>' | awk '{print $1}' | sed 's/:$//' | sort -u

# Get the branch the repo's working tree is currently on
git symbolic-ref --short -q HEAD

Angular Commit Convention -- Source

Angular git semantic commit conventions

<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
  • feat: A new feature
  • fix: A bug fix
  • docs: Documentation only changes
  • style: Changes that dont affect the meaning of the code (white-space, formatting, etc)
  • refactor: A code change that neither fixes a bug nor adds a feature
  • perf: A code change that improves performance
  • test: Adding missing or correcting existing tests
  • chore: Changes to the build process or auxiliary tools and libraries (example scopes: gulp, broccoli, npm)
  • ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)

<subject> text

  • use imperative, present tense: “change” not “changed” nor “changes”
  • don't capitalize first letter
  • no dot (.) at the end

Branch Management

# delete local branch
git branch -D bugfix

# delete remote branch
git push origin --delete <branch>

# delete remote branch alternative
git push origin :<branch>

# tell which local branches are tracking which remote branches
git branch -avv

# update a remote tracking branch, http://goo.gl/Fv65nW
# Given a branch foo and a remote upstream:
git branch -u upstream/foo
# alternatively:
git branch --set-upstream-to=upstream/foo
# or if local branch foo is not the current branch:
git branch -u upstream/foo foo
git branch --set-upstream-to=upstream/foo foo

# rename a branch
git branch -m <new-name> # for current branch
git branch -m <old-name> <new-name> # rename any branch, not just current one

Cleanup & Removal

Reference: http://git-scm.com/book/en/Git-Basics-Undoing-Things

# removes all files from the repo with the status of deleted.
git rm $(git ls-files --deleted)

# git clean removes untracked files and git checkout clears unstaged changes
git clean -df & git checkout -- .
# Source: http://stackoverflow.com/questions/1139762/gitignore-file-not-ignoring
git rm -r --cached .
git add .
git commit -m ".gitignore is now working"
# Good for when trying to do a `git rm, and getting a 'Argument list too long' error.
# xargs will limit the command length to something the shell won't choke on and invoke the command
# passed as many times as required.
# source: http://stackoverflow.com/questions/17290587/remove-deleted-files-from-git-argument-list-too-long
git status | grep deleted | awk '{print $3}' | xargs git rm
# OR
git ls-files --deleted -z | xargs -0 git rm # handles files with spaces

Tagging

Reference: http://git-scm.com/book/en/Git-Basics-Tagging

# pushes any and all local tags you have to the origin
git push origin --tags

# pushes a specific tag
git push origin <tagname>

# add a tag to a any commit
git tag -a <tagname> -m 'version ###' 9fceb02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment