Heavy Duty Git Cheatsheet - Useful git commands for common problems.

José M. Gulias Lugo
2018-10-13T02:01:48Z

Heavy Duty Git Cheatsheet.md

Heavy Duty git Cheatsheet

Solve common git scenarios and problems!

Basic workflow

# add elements
git add -A

# set commit message
git commit -m "Commit message"

# upload to remote repositorio
git push

Log all commits

# log all commits
git log

# log all commits, but do not print commits with more than one parent
git log --no-merges

# log all commits, but show no parents before all of its children are shown
git log --topo-order

# log all commics, but pretty them in one line
git log --pretty=oneline

# remix
git log --no-merges --topo-order --pretty=oneline

# display commits by author
git log --author="AuthorName"

Get a list of previous commits

# you will see a list of every thing you've done in git, across all branches!
# each one has an index head@{index}
git reflog

# magic time machine
git reset HEAD@{index}

Get repository remote origin

# if you want only the remote url
git config --get remote.origin.url

# or, if you require full output
git remote show origin

Reload .gitignore

# first commit any outstanding code changes
# also commit your new .gitignore

# remove al cached files from the staging area
git rm -r --cached .

# add all required files
git add -A

# commit
git commit -m ".gitignore is now working"

Update local branches with remote

# get all remote branches
git remote update origin

# … and delete the local ones which
# aren't in the repo
git remote update origin --prune

Go back to a previous commit

# go back one commit
git reset --hard HEAD~1

# go back five commit
git reset --hard HEAD~5

# go back to an specific commit with a hash
git reset --hard 3431bbbb

Remove a local commit

# this doesn't work if you've already pushed to origin
git reset HEAD~ --hard

Remove a local and remote commit

# undo the commit
git reset --soft HEAD~1

# unstage the added changes
git reset HEAD --

# force-push the branch which will return it to the previous commit
# this effectively returns the branch to the previous commit
git push origin {{branch_name}} -f
git push origin master -f

# now make a new branch and commit like normal
git checkout -b {{new_branch_name}}
git add -A
git commit -m "commit message"

Add something after a commit

# make your change
# follow prompts to change or keep the commit message
# now your last commit contains that change!
git add - A
git commit --amend

Change the message on the last commit

# follow prompts to change the commit message
git commit --amend

Cleanup and optimize the local repository

# runs a number of housekeeping tasks within the current repository,
# such as compressing file revisions
git gc

# this option will cause git gc to more aggressively optimize the
# repository at the expense of taking much more time.
git gc --aggressive

# suppress all progress reports.
git gc --quiet

Remove old branches

# cleanup old remote git branches
git remote prune origin

# or
git fetch origin --prune

# and feel free to add `--dry-run` to the end
# of your `git` statement to see the result
# of running it without actually running it.

Tagging

# lightweight tag
git tag v0.1.19

# pushing tags
git push origin --tags

# deleting tags
git tag -d v0.1.19
git push origin :refs/tags/v0.1.19

Quick Stash

# save a stash
git stash

# To apply a stash and remove
git stash pop

Named stash

# save a stash
git stash save "stash name"

# list stashes
git stash list
> stash@{0}: stash name

# to apply a stash and remove it using stash@{n}
git stash pop stash@{0}

Clean repository

To clean up the garbage git provides a command:

git gc

This will not impact the remote repository and will only optimize the local copy so it is safe to run on any git repo you might have. (In fact this operation is already run for you automatically after some git commands that leave behind too many loose objects)

If you want a deeper cleaning (which you should every few hunderd changesets or so), run:

git gc --aggressive

This will take longer but will provide better results.

To learn more:

git help gc

---
Some of the code above came from:

Oh shit, git!

first Aid git