Skip to content

Instantly share code, notes, and snippets.

@KhalilZaidoun
Last active October 31, 2018 16:24
Show Gist options
  • Save KhalilZaidoun/2ea67d9495321e58b73cac7091a8d638 to your computer and use it in GitHub Desktop.
Save KhalilZaidoun/2ea67d9495321e58b73cac7091a8d638 to your computer and use it in GitHub Desktop.
Git Cheat Sheet

Create a Repository

Create an empty Git repository

git init [my_project]

Clone an external Git repository

git clone link.to.repository.git

Observe your Repository

List new or modified files not yet committed

git status

Show the changes to files not yet staged

git diff

Show the changes to staged files

git diff --cached

Show all staged and unstaged file changes

git diff HEAD

Show the changes between two commit ids

git diff commit1 commit2

List the change dates and authors for a file

git blame [file]

Show the file changes for a commit id and/or file

git show [commit]:[file]

Show full change history

git log

Show change history for file/directory including diffs

git log -p [file/directory]

List all the conflicted files

git diff --name-only --diff-filter=U

Show all users and the number of commits on the repository

git shortlog --summary --numbered

Working with Branches

List all local branches

git branch

List all branches, local and remote

git branch -av

Switch to a branch, my_branch, and update working directory

git checkout my_branch

Create a new branch called new_branch

git branch new_branch

Delete the branch called my_branch

git branch -d my_branch

Merge branch_a into branch_b

git checkout branch_b
git merge branch_a

Checkout current branch into a new branch, named new_branch_name

git checkout -b new_branch_name

Create branch new_branch based on branch other_branch and switch to it

git checkout -b new_branch other_branch

Tag the current commit

git tag my_tag

To fetch a branch

git fetch origin

Checkout the remote branch

git checkout -b test origin/test

Or

git branch test origin/test

Make a change

Stages the file, ready for commit

git add [file]

Stage all changed files, ready for commit

git add .

Commit all staged files to versioned history

git commit -m “commit message”

Commit all your tracked files to versioned history

git commit -am “commit message”

Adding more changes to the last commit

git commit --amend

Unstages file, keeping the file changes

git reset [file]

Delete last local commit, unstage files, keeping the changes

git reset --soft HEAD^

Revert everything to the last commit

git reset --hard

Discard changes in working directory

git checkout -- <file>

Checkout all project by revision number

git checkout revision_number

Checkout subdirectory by revision number

git checkout revision_number -- <subdirectory>

Apply the changes introduced by some existing commits

git cherry-pick <commit-hash>

Remove files from revision control

git rm --cached  <file-path>

To prevent git from detecting changes in these files

git update-index --assume-unchanged [path]

Synchronize

Get the latest changes from origin (no merge)

git fetch

Fetch the latest changes from origin and merge

git pull

Fetch the latest changes from origin and rebase

git pull --rebase

Push local changes to the origin

git push

Push to the current branch and set the remote as upstream

git push --set-upstream origin my_branch

Git Config

Define the author name to be used for all commits by the current user.

git config --global user.name <name>

Define the author email to be used for all commits by the current user.

git config --global user.email <email>

Create shortcut for a Git command. E.g. alias.glog "log --graph --oneline" will set "git glog" equivalent to "git log --graph --oneline"

git config --global alias.<alias-name> <git-command>

Set text editor used by commands for all users on the machine. arg should be the command that launches the desired editor (e.g., vi).

git config --system core.editor <editor>

Open the global configuration file in a text editor for manual editing.

git config --global --edit

Store credentials indefinitely on disk for use by future Git programs

git config credential.helper store

Managing Remotes

List existing remotes

git remote -v

Change remote's URL

git remote set-url origin https://new-remote-url.git

Adding a remote

git remote add origin https://new-remote-url.git

Renaming a remote

git remote rename origin destination
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment