DEV Community

Cover image for Git cheat sheet
Edgar Cirilo
Edgar Cirilo

Posted on • Updated on

Git cheat sheet

Git has become a basic tool in the software development lifecycle. And now, in an age where it seems the world is moving to a remote approach, mastering this skill will be of great advantage to all software professionals, since it makes you a better team player and improves the lifecycle.

There are many blogs teaching Git. These are the ones that I highly recommend:

Moreover, I want to share the most common commands that I use in my daily workflow. Note that all will be using the command line:

Get the repo

# It works with https or ssh
$ git clone 'https://github.com/git/git.git'
Enter fullscreen mode Exit fullscreen mode

Change Origin

# Sometimes we need to change the repo to other owner or location, 
# in order to avoid it to be cloned again, we can just change the remote origin.
$ git remote add origin 'https://github.com/moby/moby.git'

# We can verify the new remote is set correctly
$ git remote -v
> origin  https://github.com/moby/moby.git (fetch)
> origin  https://github.com/moby/moby.git (push)
Enter fullscreen mode Exit fullscreen mode

Get the latest changes

# Get the latest changes in the remote repository with 'git pull'
# Remember that specifying the remote 'origin' and the branch name is always a good practice
$ git pull origin <branch-name>

# Use 'git fetch' to sync your local repository with the remote repository
# This only includes the metadata, new branches, tags, and tree history
$ git fetch 
Enter fullscreen mode Exit fullscreen mode

Create a new branch

# Move and create at the same time to a new branch
# Tip: Use git-flow branch naming convention: feature/*, hotfix/* and so on.
$ git checkout -b <branch-name>
Enter fullscreen mode Exit fullscreen mode

Add your changes

# Add changes to Stage
$ git add <file-name>

# Add ALL changes to Stage
$ git add .

# Commit changes to the local repository
$ git commit -m "Commit message"

# Add all and commit at the same time (Only to tracked files)
$ git commit -am "Commit message"

# Push commits to the remote repository
$ git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Fix commits

# Wrong commit message? No problem. Edit last commit message
$ git commit --amend -m "New commit message"

# Did you forget some changes and don't want a new commit?
# Add them to the last commit
# Warning: Try not to use it a lot 
$ git add .
$ git commit --amend --no-edit
Enter fullscreen mode Exit fullscreen mode

See commit history

# Print commit tree in a pretty way
$ git log --oneline --decorate --graph --all
Enter fullscreen mode Exit fullscreen mode

Save unfinished work

#Save all tracked files (Uncommitted)
$ git stash

#Save all tracked files with a message (Uncommitted)
$ git stash push -m "Great message"

# Include untracked and ignored files
$ git stash --all 

# List saved work
$ git stash list
> stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation
> stash@{1}: On master: 9cc0589... Add git-stash

# Apply last saved work to the working directory
$ git stash pop

# Apply specific saved work to the working directory
$ git stash pop stash@{1}

# Clean stash
# Warning: Be sure you don't need anything else before running this command
$ git clear
Enter fullscreen mode Exit fullscreen mode

Clean up a local repository

# A lot of branches that do no longer exist in the remote repository?
# Delete them from the local repository...
$ git fetch -p
Enter fullscreen mode Exit fullscreen mode

Handling mistakes (Warning: use locally only)

# Unstage file
$ git reset HEAD -- <file-name>

# Unwanted changes now?, Sync with HEAD
$ git reset --hard HEAD

# Want to start over? undo n-last commits
$ git reset --hard HEAD~n
Enter fullscreen mode Exit fullscreen mode

My git aliases

# I'm using Windows, so, instead of use bash aliases I use the git config option
git config --global alias.co=checkout
git config --global alias.br=branch
git config --global alias.cm=commit
git config --global alias.st=status
git config --global alias.unstage=reset HEAD --
git config --global alias.last=log -1 HEAD
git config --global alias.po=pull origin
git config --global alias.ps=!git push origin $(git current)
git config --global alias.current=branch --show-current
git config --global alias.pu=push origin
git config --global alias.pl=!git pull origin $(git current)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)