Skip to content

Instantly share code, notes, and snippets.

@willnationsdev
Last active May 6, 2020 15:39
Show Gist options
  • Save willnationsdev/b95da577b76074c6fd0814bf28e6ddbf to your computer and use it in GitHub Desktop.
Save willnationsdev/b95da577b76074c6fd0814bf28e6ddbf to your computer and use it in GitHub Desktop.
Git Cheatsheet
# get list of files and their status (unversioned, edited, added in working directory, etc.)
$ git status
# Get a detailed list of prior commits. Get information on where other branches are, relative to each other and the current HEAD.
$ git log
# Get a summarized list of commit hashes/messages starting from the HEAD all the way back to <branch>'s place in history.
$ git log --pretty=oneline <branch>
# When used from the root directory of a repo, adds all edited files to working directory.
# Make sure to only run this *after* you've got an updated .gitignore file. Otherwise, you will need to remove unwanted items from the working directory to prevent committing them.
$ git add .
# To quickly commit something with a summary.
$ git commit -m "some message with which to commit."
# To create a more detailed commit with a summary on the first line and then, separated by 2 newlines, a bulleted list of detailed changes. For example...
$ git commit
# Some summary commit message
#
# - Detailed item in commit message.
# - Another detailed change.
# - Note: users must now do X instead of Y.
# etc.
# -----------
#
# Resets back to commit N, but preserves your changes along the way. To just undo the last commit, do HEAD~1.
$ git reset --soft HEAD~N
# Fetches the a branch from a remote.
# Use `git fetch upstream master` to get the latest changes from upstream.
# Generates an `upstream/master` branch and a `FETCH_HEAD` branch.
$ git fetch <remote> <branch>
# Reset your currently checked out branch completely to another state, without saving your work.
# When your local master is checked out, use `git reset --hard upstream/master` after fetching upstream master to completely reset yourself to a valid state.
$ git reset --hard <branch>
# Combine working directory changes with previous commit. Can also use to just update commit message. Use `--no-edit` to skip message updating process.
$ git commit --amend
# Merge <branch>'s commits into your currently checked out branch.
# When on master, use with `upstream/master` after fetching to fast-forward update your master branch. Similar to `git reset --hard upstream/master` for this purpose.
$ git merge <branch>
# When working on a feature branch, sometimes the master branch has updated with changes. After updating your master, you may then need a feature branch to catch up with those changes.
# To acommodate this, you must re-apply the feature branch's commits onto the updated master. This is called "rebasing".
# With a feature branch checked out, do `git rebase master` to update to have the latest master's changes. Resolve any conflicts. Push to origin. Now your pull requests to upstream won't have any conflicts.
$ git rebase <branch>
# An "interactive" rebase. This gives you full control over how your commits are re-applied on top of the other branch.
# One common use is to combine multiple commits into one. When you use this command, you'll be given a list of subcommands to associate with each commit.
# Use a 'reword' command on the first commit and 'fixup' on subsequent commits. This discards the subsequent commits' messages, merges their changes, and then lets you revise the message for all of them together in the final commit.
# Use `git log` to figure out how many commits back you need to travel. Then use that number for to rebase back that many commits.
$ git rebase -i HEAD~N
# Push your changes. Usually <remote> is 'origin'. <branch> is 'master' if just updating your GitHub copy of master or it will be a feature branch if you are backing up your changes on GitHub / preparing for a pull request submission.
$ git push <remote> <branch>
# If you have done a commit ammend or a git rebase since your last push, then your commits will likely have different commit hashes than the ones on GitHub. This will then require you to do a force push.
$ git push -f
$ git push --force
# If you want to be able to pull changes from someone else's GitHub fork, you must first add them as a remote.
# This would then enable you to do things like `git fetch <name> master` to fetch that person's master.
$ git remote add <name> <url>
$ git remote add john https://github.com/johnsmith/tasker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment