Grizzlybit

GIT Cheat Sheet

September 26, 2016

We can all agree that git has become everyone’s favorite versioning system lately. Having experience with SVN and Git I can safely say that Git is not actually better but it is different. The key advantage is that it is decentralized. With Git, you can do practically anything offline, because everybody has their own repository. Making branches and merging between branches has never been easier. Git is complex, powerful and fast (since it works for Linux kernel of course).

Every company and business setup has its own workflow and with SVN in my company we had a tough time adopting the version system with our business case. Git is very flexible and with its TIMTOWTDI approach will help is meet our goals. Well, everything has its pros and cons even though the pros will obviously outrun the cons, note that Git is much harder to learn, lacks a good GUI and revisions do not have version numbers. Below I have complied a few necessary git commands in the form of a cheat sheet:

Configuration:

$ git config --global user.name "[name]"

Sets the name you want to commit with.

$ git config --global user.email "[email address]"

Sets the email Address you want the author to have.

The most useful config you need to start with is

$ git config --global color.ui auto

Sets to colorize the command line.

Creating a New Repository:

$ git init

Initializes a new repository

$ git clone ssh://[email protected]/repo.git$ git clone [email protected]:/path/to/repository$ git clone /path/to/repository

Clones an existing repository.

Adding Files to be Tracked:

$ git add

Adds the specified file.

$ git add .

Adds all files.

Tracking Changes:

$ git status

List all new or modified files to be committed

$ git diff

Shows file difference not yet staged.

$ git log

Shows all commits

$ git log --oneline

Show all commits summaried each in one line.

$ git log -p

Shows changes for a specific file

$ git diff --staged

Shows file differences between staging and the last file version

$ git blame

Tracking changes on a file about who changed what and when (annotated).

Committing changes:

$ git commit -m 'commit message'

Commit changes to head

$ git commit -a

Commit any file you have added with git add or modified an added file.

$ git remote add origin

If you haven’t connected your local repository to a remote server, add the server to be able to push to it

$ git remote -v

List all currently configured remote repositories:

$ git push origin master

Sends the changes to the master branch on the remote repository

$ git push

Publish local changes on a remote

Branching and Related Commands:

$ git branch

List all existing branches

$ git branch

Creates a new branch based on your current head

$ git checkout

Checkout a specified branch

$ git branch -d

Deletes a specified branch

$ git merge

Merge into the current head

$ git rebase

Rebase current head onto the branch

$ git tag

Mark the current commit with a tag name.

Fetching Commits:

$ git fetch

Download all changes from , but don’t integrate into head

$ git pull

Download changes from and merge into head

File System:

$ git rm

Deletes the specified file form the working directory and stages the deletion.

$ git rm --cached

Deletes the file from the version control system but preserves the file locally in the working directory.

$ git mv < original-filename>
$ git lis-files --other --ignored --exclude-standard

List all ignored files in working project directory

Stashes:

$ git stash

Temporarily stores all modified tracked files

$ git stash pop

Restores the most recently stashed files

$ git stash list

Lists all stashed changesets

$ git stash drop

Discards the most recently stashed changeset

Reverting and Re-doing:

$ git reset

Unstages the specified file, but preserve its contents

$ git reset

Undoes all commits after [commit] , preserving changes locally

$ git reset --hard [commit]

Discards all history and changes back to the specified commit

$ git revert

Revert a commit by producing a new commit with contrary changes

Happy committing!


Zubair Ahmed

Zubair Ahmed 👋
An ambitious tweaker who seeks adventures. He likes slapping the keyboard until something good happens.