Common git commands for everyday use

Lets keep the necessary git commands close at our fingertip for everyday work. This is a list of common git commands. Also tried to add few links which I think essential to know for every git beginner.

This is neither a cheatsheet  nor a detailed documentation of git scm. Check useful links at the bottom  for details references.

Git-Logo

The Basic workflow

    1. Start a repository
      • git init = Start a new repository in current directory
      • git clone <repository-url> = Clone a repository
    2. Do some change
    3. Add/index the Changes
      • git add . = Add all files of current directory
      • git add <path/file.name> = Add specific file
      • git add Documentation/\*.txt = Add files matching pattern
      • git commit -am “<message>” = Commit all your local changes
    4. Check the status
      • git status = Files changed in working directory
      • git log = History of changes
      • git log —-all —-graph —-oneline = Pretty one line log with encestry graph
      • git log branch1..branch2 = Check what are not merged in 2 branches
  1. Get updated
    • git fetch = Fetches latest changes from origin
    • git pull = Fetches latest changes from origin and merge
  2. Publish
    • git push = Push changes to origin
    • git push <origin> <branch> = Push changes to origin

Working With Branches

  1. Creating Branch
    • git branch <branch-name>
  2. Listing
    • git branch  = list your available branches
    • git branch -a  = list your available branches including remote branches
    • git branch -v  = see the last commit on each branch
  1. Switching to branch
    • git checkout <branc-hname>
    • git checkout -b <branch-name> = create and immediately switch to a branch
  2. Getting remote branch to local
    • git fetch; git branch <new-branch-name> origin/<new-branch-name>
  3. Pushing local branch to remote
    • git push -u origin <branch-name> (assuming you are on the new branch now)
  4. Merging branches
    • git merge <branch-name>  = <branch-name> will be merged into current branch
  5. Deleting branch
    • git branch -d <branch-name>  = delete a local branch
    • git push <remote-name> :<branch-name>  = delete a remote branch

Working with Remotes

  1. List of remotes
    • git remote -v
  2. Adding remote
    • git remote <name> <url>
    • git remote assembla https://assembla/repo/path #Example adding assembla repo as a new remote
  3. Update from/publish to a specific remote
    • git fetch <remote-name> = Fetching from a specific remote
    • git push <remote-name> <branch-name> = Fetching from a specific remote and branch

Fixing mistakes and conflicts

  1. Check canges/differences
    • git diff = Changes to tracked files
    • git diff $id1 $id2  = What changed between 2 commits
  2. Discard all modifications
    • git checkout — <filename>
  3. Revert last (or specific) commit
    • git revert HEAD
    • git revert <commit-ref>
  4. Fix last commit
    • git commit –amend [-m “updated message”]
  5. Unstage staged modifications of a file
    • git reset HEAD <file>
  6. Resolving with our/theirs
    • git checkout –ours <path/file> 
    • git checkout –theirs <path/file>

Some Git concepts and tricks

  1. How to ignore files/folders?
  2. What does mean updated/indexed/committed?
  3. Who changed what?
  4. What are tracking branches?
  5. Stash in few wordsBelieve me, you’re gonna need it!
  6. Be quicker with aliases 
  7. Wanna mark a commit/version? Tag it!
  8. How to refer a specific commit version?

Useful Links

If you recommend any other command to be listed here, please let me know.

5 Comments

  1. Anis vai,
    git log –oneline –graph
    is not working for me. I am using git 1.7.12 (Apple Git-37) in OS X 10.8

    Does the git log graph tool need some extension?

Leave a Comment

Your email address will not be published.