Skip to content

Instantly share code, notes, and snippets.

@awkale
Last active December 3, 2018 15:01
Show Gist options
  • Save awkale/922318f72934b500ce468d0ae36fc3fa to your computer and use it in GitHub Desktop.
Save awkale/922318f72934b500ce468d0ae36fc3fa to your computer and use it in GitHub Desktop.
#git #cheatsheet
#
# GETTING STARTED
#
git init
# initializes current folder as a git repository
git clone url
# copies remote git folder to local directory
#
# CONFIGURATIONS
#
git config --global color.ui true
git config --global push.default current
git config --global core.editor vim
git config --global user.name "John Doe"
git config --global user.email [email protected]
git config --global diff.tool meld
# prevent git branch list to open in editor
git config --global pager.branch false
#
# BRANCHING
#
git branch
# See the list of all local branches
git checkout branchname
# Switch to existing local branch
git checkout -b new-branch-name
# Checkout current branch into a new branch, named new-branch-name
git merge branchname
# Merge branch-name into the current branch
git merge --no-ff branchname
# Merge branch without fast forwarding. This is what pull requests do.
# It helps to preserve history of the changes as relavant to that branch
# It's an advanced feature, but try it out with GUI to see the difference
# between the regular merge and merge --no-ff
git branch -d branchname
# Soft branch delete, will complain if the branch is not merged
git branch -D branchname
# Hard branch delete, will not complain about nothing. Like rm -rf in bash
git checkout -- .
# discard changes
git branch -a
# To see the list of remote branches
# -a stands for all
git fetch -p
# fetches latest and removes and deleted branches on remote
#
# CURRENT BRANCH
#
git log
# See all commits
git log --pretty=format:"%h %s" --graph
# Pretty commit view, you can customize it as much as you want.
# Just google it :)
git log --author='Alex' --after={1.week.ago} --pretty=oneline --abbrev-commit
# See what you worked on in the past week
git log --no-merges master..
# See only changes made on this branch (assuming it was branched form master branch)
git status
# See status of your current git branch.
# Often will have advice on command that you need to run
git status -s
# Short view of status. Helpful for seeing things at a glance
git add filename
# Add modified file to be commited(aka stage the file)
git add .
# Add all modified files to be commited(aka stage all files)
git add '*.txt'
# Add only text files, etc.
git rm filename
# Tell git not to track file anymore
git commit
# Record changes to git. Default editor will open for a commit message.
# (Visible via git log)
# Once files are commited, they are history.
git commit -m 'Some commit message'
# A short hand for commiting files and writing a commit message via one command
git commit --amend# Past commit will be ammended.
# Changing the history :) If you want to change your previous commit,
# you can, if you haven't pushed it yet to a remote repo
# Simply make new changes, add them via git add, and run the following command.
#
# ADVANCED
#
git reset
# Unstage pending changes, the changes will still remain on file system
git reset --hard HEAD
# Unstage pending changes, and reset files to pre-commit state
git reset --hard origin/master
# pull new from origin and overwrite everything; any local commits that haven't been pushed will be lost.
git reset tag
git reset <commit-hash>
# Go back to some time in history, on the current branch
git stash
# Save current changes, without having to commit them to repo
git stash pop
# And later return those changes
git checkout filename
# Return file to it's previous version, if it hasn’t been staged yet.
# Otherwise use git reset filename or git reset --hard filename
#
# COMPARING CHANGES
#
git diff
# See current changes, that have not been staged yet.
# Good thing to check before running git add
git diff HEAD
# See current changes, that have not been commited yet (including staged changes)
git diff branch-name
# Compare current branch to some other branch
git difftool -d
# Same as diff, but opens changes via difftool that you have configured
# -d tells it to open it in a directory mode, instead of having to open
# each file one at a time.
git difftool -d master..
# See only changes made in the current branch (compared to master branch)
# Helpful when working on a stand alone branch for a while
git diff --no-commit-id --name-only --no-merges origin/master...
# See only the file names that has changed in current branch
git diff --stat #Your diff condition
# Similar to above, but see statistics on what files have changed and how
gitk [fileName]
# Displays changes in a repository or a selected set of commits. This includes visualizing the commit graph, showing information related to each commit, and the files in the trees of each revision.
#
# REMOTE BRANCH
#
git remote
# See list of remote repos available. If you did git clone,
# you'll have at least one named "origin"
git remote -v
# Detailed view of remote repos, with their git urls
git remote add origin <https://some-git-remote-url>
# Add a new remote. I.e. origin if it is not set
git push
# Push current branch to remote branch (usually with the same name)
# called upstream branch
git push -u origin master
# If a remote branch is not set up as an upstream, you can make it so
# The -u tells Git to remember the parameters
git push origin branchname
# Otherwise you can manually specify remote and branch to use every time
git pull
# Just like pushing, you can get the latest updates from remote.
# By defaul Git will try to pull from "origin" and upstream branch
git pull origin branchname
# Or you can tell git to pull a specific branch
git fetch && git merge origin/remote-branch-name
# Git pull, is actually a short hand for two command.
# Telling git to first fetch changes from a remote branch
# And then to merge them into current branch
git fetch -p
# If you want to update history of remote branches, you can fetch and purge
git branch -a
# To see the list of remote branches
# -a stands for all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment