Sunday, June 23, 2013

Git Cheat Sheet


Most commonly used git commands

git init
    # initializes an empty git in the current directory

git config -l
git config --global user.name "<your name>"
git config --global user.email "<your email>"
git config --global color.status auto
git config --global color.diff auto
git config --global color.branch auto

git clone <git host>:/repo/<projectName>
    # copy a git repository so you can add to it

git add <file1 file2>
    # adds files to the staging area
git add .
    # recursively add all files under current directory
git add *
    # add all files in current directory only

git checkout -- <file>
    # reverts back the modified file to the one in the repository

git status
    # view the status of your files in the working directory and staging area
git status -s
    # short form. Left column for staging area. Right column for working dir

git diff
    # show diff of unstaged changes
git diff --cached
    # show diff of staged changes
git diff HEAD
    # show diff of all staged or unstaged changes

git commit
    # records a snapshot of the staging area
git commit -a
    # stage modified files before commit. Does not add new files, but removes deleted files from staging area
git commit -m 'commit message goes here'
git commit --amend
    # Update the last commit message
git commit --amend --reset-author
    # Use this if you have changed your user name or email with git config and want to fix this identity for previous commit

git reset --hard HEAD
    # This will throw away any changes you may have added to the git index and as well as any outstanding changes you have in your working tree.
git reset HEAD
    # unstage all changes that were staged. Reset staging area to HEAD
git reset HEAD -- <file1 file2>
    # unstage files. Reset the file to its snapshot in HEAD

git revert HEAD
    # This will create a new commit which undoes the change in HEAD (i.e. the last commit). You will need to enter the message for the new commit.
git revert HEAD^
    # Git will attempt to undo the old change while leaving intact any changes made since then.

git rm <file1 file2>
    # remove files from staging area and working dir
git rm --cached <files>
    # remove files from staging area only
git rm -r subdir/
    # path = * to recursively remove subdirectories from staging area and working dir

git pull

git branch -a
git branch <branchName>
    # create new local branch at your last commit. So all commits of current branch will be copied over to the new branch.
git push origin <branchName>
    # push a new local branch to the server. If you do not explicitly push your new local branches, they stay in your repo and are invisible to others
git pull origin <branchName>
    # pull a remote branch
git branch -d <branchName>
    # delete branch. This command ensures that the changes in the branch (to be deleted) are already in the current branch.
git branch -D <branchName>
    # delete branch. This command will NOT check if the changes in the branch (to be deleted) are already in the current branch.
git push origin :<branchName>
    # will delete the branch on the origin remote
git branch --track <master> <origin/master>
    #Add release branch??
git checkout <branchName>
    # Switch the branch you are currently on
git remote
git remote show <origin>
    # Lists the URL for the remote repository as well as the tracking branch information

git log <release>
git log --name-status
git log -p
git log --pretty=full
git log --no-merges
    # Ignore merges
git tag -l
    # list tag names
git tag -a v1.4 -m 'version 1.4'
    # Create tag with annotation
git tag v1.4
    # Create tag without annotation
git push --tags
    # Push tags to remote
git show <tagname>
    # Show tag and tagger details

#To rename a tag:
git tag NEW OLD
    # First create NEW as an alias of OLD
git tag -d OLD
    # Delete OLD
git push origin :OLD
    # Delete OLD in remote branch

git stash
git stash apply
git stash list
git log stash@{2}
git show stash@{32}
git reflog
git reflog expire --expire=30.days refs/stash

Looking for anything else? Please refer to Git Documentation page.


4 comments:

  1. Good one, one will be needing it first after joining somewhere... working with mercurial but bookmarked. :D

    ReplyDelete
    Replies
    1. Now-a-days, IDEs are giving everything in a single package, for example, you can do all the needed git stuffs from IntelliJ IDEA pretty easily just by clicking... Still I like to use git from command line, makes me feel like a pro :P

      Delete
  2. I was gonna make it myself..
    Thanks a ton.. :)

    ReplyDelete