Git Cheatsheet

What is Git?

Git is a widely-used version control system. In the context of Information Technology, Git allows multiple developers to work on the same project without stepping on each other’s toes. It can keep track of changes to your files (known as commits), so you can always recall specific versions of your project at any point in the future.

Developers can use it for collaborative programming and for those in IT to manage configs collaboratively or to preserve your sanity when working with configurations across multiple devices.

If you’re looking for something specific, Ctrl + F and search for it. If you’re just here to learn, grab a coffee, sit back, and let’s learn together.

I’d normally add a joke in this opening, but I am afraid you might not git it.

Git Commands

Setup

Configure your Git username (to be used for every commit)

git config --global user.name "Your Name"

Configure your Git email (to be used for every commit)

git config --global user.email "your-email@domain.com"

Create & Clone Repositories

Initialize a new repository

git init "coffee-app"

Clone an existing repository

git clone https://github.com/username/coffee-app.git

Local Changes

Check the status of your repository

git status

Add all current changes to the next commit

git add .

Commit all local changes in tracked files

git commit -m "Added new coffee types"

Change the last commit Don’t amend published commits!

git commit --amend

Branches

List all existing branches

git branch

Switch to a specific branch and update the working directory

git checkout "espresso-branch"

Create a new branch based on your current HEAD

git branch "new-latte-feature"

Create a new tracking branch based on a remote branch

git checkout --track <remote>/<branch>

Delete a local branch

git branch -d "unwanted-coffee-feature"

Update & Publish

List all currently configured remotes

git remote -v

Show information about a remote

git remote show "origin"

Add new remote repository, named “origin”

git remote add "origin" https://github.com/username/coffee-app.git

Download all changes from “origin”, but don’t integrate into HEAD

git fetch "origin"

Download changes and directly merge/integrate into HEAD

git pull "origin" master

Publish local changes on a remote

git push "origin" master

Merge & Rebase

Merge “branch” into your current HEAD

git merge "coffee-feature-branch"

Rebase your current HEAD onto “branch” Don’t rebase published commit!

git rebase "master"

Undo

Discard all local changes in your working directory

git reset --hard HEAD

Discard local changes in a specific file

git checkout HEAD coffee-list.txt

Revert a commit (by producing a new commit with contrary changes)

git revert "commitId"

Reset your HEAD pointer to a previous commit and discard all changes since then

git reset --hard "commitId"

This should cover most of the basic and intermediate usage of Git. As always, be mindful of your current branch and the state of your local changes before using commands that modify your history, like commit, merge, rebase, and reset.

Soon, I’ll publish a guide about signing your commits in Git, which we should all be doing!!

I hope you enjoy and benefit from this resoruce.