Skip to content

Instantly share code, notes, and snippets.

@jack-mo
Last active March 25, 2020 02:37
Show Gist options
  • Save jack-mo/6f9678be7e67f1ed887051ebc8f9f352 to your computer and use it in GitHub Desktop.
Save jack-mo/6f9678be7e67f1ed887051ebc8f9f352 to your computer and use it in GitHub Desktop.

git cheat sheet

Configuring git

List the current configurations:
git config --list

Setting the name you want attached to your commit transactions:
git config --global user.name "[name]"

Setting the email you want attached to your commit transactions:
git config --global user.email "[email address]"
Note: this should be your email associated with the git repository

Cloning

git clone [url]
Note: use SSH url

Branches

Checking out an existing branch:
git checkout [branch-name]
Checking out a new branch:
git checkout -b [branch-name]

Listing all local branches:
git branch

Renaming current branch:
git branch -m [new-branch-name]
Note: this only works for local branches, do not do this if the branch was already been pushed

Deleting a branch:
git branch -d [branch-name]
Deleting a branch that has not been fully merged yet:
git branch -D [branch-name]
Note: sometimes you have to fetch before your local repository knows the branch has been merged

Synchronizing Changes

Downloads all history from the repository:
git fetch

Pulling changes from the remote branch:
git pull origin [branch-name]

Pushing changes to the remote branch:
git push origin [branch-name]

Updating branch with current latest master:
git merge master
Note make sure that your local master is up too date with the remote one

Making Changes

Listing status of repository:
git status

Staging files to be committed:
git add [file]
Stage files interactively:
git add -p
To stage all files:
git add .
Note: you shouldn't be doing this

Unstaging a file:
git reset [file]

Committing:
git commit -m "[desciptive message]"
Note: keep you messages short

Amending a commit:
git commit --amend
Note: avoid amending commits that has already been push

Copying commit from another branch:
git cherry-pick [commit-has]

Undoing Changes

Undoing all changes after a commit, preserving the changes locally:
git reset [commit-hash]

Undoing last commit, preserving the changes locally:
git reset HEAD~
Note: include --hard if you want to discard the changes

Reviewing History

Listing the full version history for the current branch:
git log
Listing the version history up to a certain number of commits for the current branch:
git log -[number-of-commits]

Showing the current diff:
git diff
Showing the current diff for a file:
git diff [file-name]

Stashing

Stashing current changes:
git stash

Restoring stashed changes:
git stash pop

Listing stashed changes:
git stash list

Discarding the stashed changes:
git stash drop

Note: avoid having multiple stashes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment