Git Github Cheat Sheet



Although there are enough resources on the web about Git, I will keep this one for my own reference. Minimal Git version required 1.7.2.

TOC

Legend

Git cheat sheet for network nuts students. Contribute to kehbixgit/git-cheat-sheet development by creating an account on GitHub. Git branch -a: List all branches (local and remote) git branch branch name Create a new branch: git branch -d branch name Delete a branch: git push origin -delete branch name Delete a remote branch: git checkout -b branch name Create a new branch and switch to it: git checkout -b branch name origin/branch name Clone a remote branch.

Use this handy git cheat sheet guide to enhance your workflow. This Git cheat sheet saves you time when you just can't remember what a command is or don't want to use git help in the command line. It is hard to memorize all the important Git commands by heart, so print this out or save it to your desktop to resort to when you get stuck.

  • index: staging area (Imagine you are loading sand into the truck with bucket. Well, the bucket is like index and truck like a repository :)
  • <sha1>: sha1 hash of commit
  • <file>: path to the file (path/to/file.ext)
  • <branch>: branch name
  • <repository>: remote repository name

Info

Search the history for a change matching a pattern

Useful options:

Find commits where files were deleted

Checkout deleted file in the working tree

Checkout a file from another branch into the working tree

Only show the content of a file from a specific revision

Show diff between branches detecting renames

Show file's history

Show changes on a branch that is not merged upstream

Show log with changed files

Get latest tag in the current branch

Find out if a change is part of a release

Find out which branch contains a change

Adding

Add changes to the index chunk by chunk

  • y: stage this chunk
  • n: do not stage this chunk
  • s: split this chunk into smaller chunks
  • e: edit this chunk

Branching

Create local branch

If not provided, Git uses HEAD as the new branch start point.

or

Delete local branch

Delete already merged branch

Force branch deletion

Patching

Copy commit range from one branch to another

Pick from start <sha1> commit till end <sha1> commit.

Creating and applying patches

By default Git will create a patch for every commit. Use --stdout > <patch>.patch for combined patch.

Create patches for the last N commits (each commit in it's own patch).

Create patches containing all commits from the current branch against another <branch> branch (each commit in it's own patch).

Creating combined patch.

Check what changes are in the patch

Test the patch before applying

Apply patch

Undoing

git reset contains great explanation and examples.

Split commit

--soft option will keep files in the index.

Undo a merge or pull

Undo a merge or pull inside a dirty work tree

Revert a bad commit

Git Github Cheat Sheet

Checkout a deleted file into the work tree

Remotes

Crete a new local branch by pulling a remote branch

Track a remote branch with an existing local

Delete remote branch

Prune remote-tracking branches that are deleted from a remote repo

Change remote URL

Subtree

  • --squash do not preserve history (squash history)

Add subtree as non-remote repository

Add subtree

Pull subtree

Add subtree as remote repository

Add remote

Add subtree

Pull subtree changes

Push subtree changes

Submodules

Update submodules

Update submodule's URL

Edit the .gitmodules file, then run:

Remove submodule

  • remove the submodule's entry in the .gitmodules file
  • remove the submodule's entry in the .git/config
  • run git rm –cached path/to/module - without a trailing slash!
  • remove the submodule from the filesystem, run rm -rf path/to/module/
  • commit changes

Additional resources

Setup

clone the repository specified by ; this is similar to 'checkout' insome other version control systems such as Subversion and CVS

Add colors to your ~/.gitconfig file:

Highlight whitespace in diffs

Add aliases to your ~/.gitconfig file:

Configuration

edit the .git/config [or ~/.gitconfig] file in your $EDITOR

sets your name and email for commit messages

tells git-branch and git-checkout to setup new branches so that git-pull(1)will appropriately merge from that remote branch. Recommended. Without this,you will have to add --track to your branch command or manually merge remotetracking branches with 'fetch' and then 'merge'.

This setting tells git to convert the newlines to the system’s standardwhen checking out files, and to LF newlines when committing in

Git

You can add '--global' after 'git config' to any of these commands to make itapply to all git repos (writes to ~/.gitconfig).

Info

Use this to recover from major fuck ups! It's basically a log of thelast few actions and you might have luck and find old commits thathave been lost by doing a complex merge.

show a diff of the changes made since your last committo diff one file: 'git diff -- 'to show a diff between staging area and HEAD: git diff --cached

show files added to the staging area, files with changes, and untracked files

show recent commits, most recent on top. Useful options:--color with color--graph with an ASCII-art commit graph on the left--decorate with branch and tag names on appropriate commits--stat with stats (files changed, insertions, and deletions)-p with full diffs--author=foo only by a certain author--after='MMM DD YYYY' ex. ('Jun 20 2008') only commits after a certain date--before='MMM DD YYYY' only commits that occur before a certain date--merge only the commits involved in the current merge conflicts

show commits between the specified range. Useful for seeing changes fromremotes:git log HEAD..origin/master # after git remote update

show the changeset (diff) of a commit specified by , which can be anySHA1 commit ID, branch name, or tag (shows the last commit (HEAD) by default)

show only the names of the files that changed, no diff information.

show who authored each line in

show who authored each line in as of (allows blame to go back intime)

really nice GUI interface to git blame

show only the commits which affected listing the most recent firstE.g. view all changes made to a file on a branch:
git whatchanged | grep commit |
colrm 1 7 | xargs -I % git show % this could be combined with git remote show to find all changes onall branches to a particular file.

show the diff between a file on the current branch and potentially anotherbranch

use this form when doing git diff on cherry-pick'ed (but not committed)changessomehow changes are not shown when using just git diff.

list all files in the index and under version control.

show the current version on the remote repo. This can be used to check whethera local is required by comparing the local head revision.

Adding / Deleting

add , , etc... to the project

add all files under directory

to the project, including subdirectories

add all files under the current directory to the projectWARNING: including untracked files.

remove , , etc... from the project

remove all deleted files from the project

commits absence of , , etc... from the project

Ignoring

Option 1:

Edit $GIT_DIR/info/exclude. See Environment Variables below for explanation on$GIT_DIR.

Option 2:

Add a file .gitignore to the root of your project. This file will be checked in.

Either way you need to add patterns to exclude to these files.

Staging

add changes in , ... to the staging area (to be included inthe next commit

interactively walk through the current changes (hunks) in the workingtree, and decide which changes to add to the staging area.

interactively add files/changes to the staging area. For a simplermode (no menu), try git add --patch (above)

Unstaging

remove the specified files from the next commit

Committing

commit , , etc..., optionally using commit message ,otherwise opening your editor to let you type a commit message

commit all files changed since your last commit(does not include new (untracked) files)

commit verbosely, i.e. includes the diff of the contents being committed inthe commit message screen

edit the commit message of the most recent commit

redo previous commit, including changes made to , , etc...

Branching

list all local branches

list all remote branches

list all local and remote branches

create a new branch named , referencing the same point in history asthe current branch

create a new branch named , referencing , which may bespecified any way you like, including using a branch name or a tag name

create a new remote branch named , referencing on theremote.
Example: git push origin origin:refs/heads/branch-1
Example: git push origin origin/branch-1:refs/heads/branch-2

create a tracking branch. Will push/pull changes to/from another repository.Example: git branch --track experimental origin/experimental

delete the branch ; if the branch you are deleting points to acommit which is not reachable from the current branch, this commandwill fail with a warning.

delete a remote-tracking branch.
Example: git branch -r -d wycats/master

even if the branch points to a commit not reachable from the current branch,you may know that that commit is still reachable from some other branch ortag. In that case it is safe to use this command to force git to delete thebranch.

make the current branch , updating the working directory to reflectthe version referenced by

create a new branch referencing , and check it out.

removes a branch from a remote repository.
Example: git push origin :old_branch_to_be_deleted

Using Github For Documentation

Checkout a file from another branch and add it to this branch. Filewill still need to be added to the git branch, but it's present.
Eg. git co remote_at_origin__tick702_antifraud_blocking
..../...nt_elements_for_iframe_blocked_page.rb

Eg. git show remote_tick702 -- path/to/fubar.txt
show the contents of a file that was created on another branch and thatdoes not exist on the current branch.

Show the contents of a file at the specific revision. Note: path has to beabsolute within the repo.

Merging

merge branch into the current branch; this command is idempotentand can be run as many times as needed to keep the current branchup-to-date with changes in

merge branch into the current branch, but do not autocommit theresult; allows you to make further tweaks

merge branch into the current branch, but drops any changes in, using the current tree as the new tree

Cherry-Picking

selectively merge a single commit from another local branch
Example: git cherry-pick 7300a6130d9447e18a931e898b64eefedea19544

Squashing

WARNING: 'git rebase' changes history. Be careful. Google it.

(then change all but the first 'pick' to 'squash')squash the last 10 commits into one big commit

Conflicts

work through conflicted files by opening them in your mergetool (opendiff,kdiff3, etc.) and choosing left/right chunks. The merged result is staged forcommit.

For binary files or if mergetool won't do, resolve the conflict(s) manuallyand then do:

git add [ ...]

Once all conflicts are resolved and staged, commit the pending merge with:

git commit

Sharing

update the remote-tracking branches for (defaults to 'origin').Does not initiate a merge into the current branch (see 'git pull' below).

fetch changes from the server, and merge them into the current branch.Note: .git/config must have a [branch 'some_name'] section for the currentbranch, to know which remote-tracking branch to merge into the currentbranch. Git 1.5.3 and above adds this automatically.

update the server with your commits across all branches that are COMMONbetween your local copy and the server. Local branches that were neverpushed to the server in the first place are not shared.

update the server with your commits made to since your last push.This is always required for new branches that you wish to share. Afterthe first explicit push, 'git push' by itself is sufficient.

E.g. git push origin twitter-experiment:refs/heads/twitter-experimentWhich, in fact, is the same as git push origin but a littlemore obvious what is happening.

Reverting

reverse commit specified by and commit the result. This does not dothe same thing as similarly named commands in other VCS's such as 'svnrevert' or 'bzr revert', see below

re-checkout , overwriting any local changes

re-checkout all files, overwriting any local changes. This is most similarto 'svn revert' if you're used to Subversion commands

Fix mistakes / Undo

abandon everything since your last commit; this command can be DANGEROUS.If merging has resulted in conflicts and you'd like to just forget aboutthe merge, this command will do that.

undo your most recent successful merge and any changes that occurredafter. Useful for forgetting about the merge you just did. If there areconflicts (the merge was not successful), use 'git reset --hard' (above)instead.

forgot something in your last commit? That's easy to fix. Undo your lastcommit, but keep the changes in the staging area for editing.

redo previous commit, including changes you've staged in the meantime.Also used to edit commit message of previous commit.

Plumbing

test = $(git merge-base )
determine if merging sha1-B into sha1-A is achievable as a fast forward;non-zero exit status is false.

Stashing

save your local modifications to a new stash (so you can for example'git svn rebase' or 'git pull')

Github Tutorial For Beginners Pdf

restore the changes recorded in the stash on top of the current working treestate

restore the changes from the most recent stash, and remove it from the stackof stashed changes

list all current stashes

show the contents of a stash - accepts all diff args

delete the stash

delete all current stashes

Remotes

adds a remote repository to your git config. Can be then fetched locally.Example:

Git Command Cheat Sheet

git remote add coreteam git://github.com/wycats/merb-plugins.git
git fetch coreteam

delete a branch in a remote repository

create a branch on a remote repository
Example: git push origin origin:refs/heads/new_feature_name

replace a branch with <new_remote> think twice before do this
Example: git push origin +master:my_branch

prune deleted remote-tracking branches from 'git branch -r' listing

add a remote and track its master

show information about the remote server.

Eg git checkout -b myfeature origin/myfeature
Track a remote branch as a local branch.

For branches that are remotely tracked (via git push) butthat complain about non-fast forward commits when doing agit push. The pull synchronizes local and remote, and ifall goes well, the result is pushable.

Submodules

add the given repository at the given path. The addition will be part of thenext commit.

Update the registered submodules (clone missing submodules, and checkoutthe commit specified by the super-repo). --init is needed the first time.

Executes the given command within each checked out submodule.

Remove submodules

  1. Delete the relevant line from the .gitmodules file.
  2. Delete the relevant section from .git/config.
  3. Run git rm --cached path_to_submodule (no trailing slash).
  4. Commit and delete the now untracked submodule files.

Patches

Generate the last commit as a patch that can be applied on anotherclone (or branch) using 'git am'. Format patch can also generate apatch for all commits using 'git format-patch HEAD^ HEAD'
All page files will be enumerated with a prefix, e.g. 0001 is thefirst patch.

Applies the patch file generated by format-patch.

Generates a patch file that can be applied using patch:

Useful for sharing changes without generating a git commit.

Git Instaweb

Environment Variables

Your full name to be recorded in any newly created commits. Overridesuser.name in .git/config

Your email address to be recorded in any newly created commits. Overridesuser.email in .git/config

Location of the repository to use (for out of working directory repositories)

Location of the Working Directory - use with GIT_DIR to specifiy the workingdirectory rootor to work without being in the working directory at all.





Comments are closed.