Git Introduction


Basic Commands

git init Initializing a local repository

git clone Initializing a repository from a remote one

git fetch Fetching new commit information from a remote

git rebase Rebasing the current branch's mod onto another branch

git checkout Checking out a branch into the working tree (filesystem)

git branch Managing branches

git add Adding files to the staging (to-be-committed) area

git status Viewing the status of the working tree

git commit Committing the staged changes

git merge Merging branches

git push Pushing new local commits to a remote

Other useful things

git remote Managing remotes

git tag Managing tags

git reset Reverting and unstaging changes

git diff Viewing the changes in the working tree, a file or a directory

git mv Renaming/moving files and directories

git rm Removing files and directories

Key things to understand

Everything is a commit

master = origin/master = 9beaba0fe26d7a965baf015409a1fb9430f1cd91


They're just containing a commit cloud

Commits are all linked together via their parent commit hash

Tags and branches are just pointers (aliases) to one of those commits

Don't mix those up

origin/master is a pointer to a commit, one argument.

origin master is just the name of a remote and a branch, two arguments.

Read that output

Git spits out lots of what seems to be gibberish at first, but it actually makes sense, and is often offering answers to questions you might have, if you read it carefully

Typical workflow

  1. git checkout -b new_branch
  2. code
  3. git status
  4. git add -p
  5. git commit -m "Message"
  6. goto 3, repeat until no changes you want to commit are left
  7. goto 2, repeat until no more work is needed in this branch
  8. git checkout master (or another integration branch)
  9. git merge new_branch
  10. git push origin master
  11. if the push fails:
    1. git fetch origin
    2. git rebase origin/master
    3. git push origin master
  12. git branch -d new_branch

Don't pull

Unless you really know what you're doing, just don't use git pull.


git rebase keeps the history clean

.. but it rewrites the commits => pushed/shared branches shouldn't be rebased

Image - credits to Mark Lodato

Resolving conflicts while rebasing

fix the files (search for those characteristic <<<<<< & >>>>>>)

git add *path*

git rebase --continue


Dealing with submodules

Submodules are pointers to commits, not branches, you must update them manually

Before working in a submodule, you should do a git checkout master to be sure you're not in a detached head

Every time someone updates the pointer, you must run git submodule update again

Git config niceties

Set with git config --global -e

    name = John Smith
    email =
    sup = !git submodule update --recursive --init
    fo = !git fetch origin
    fu = !git fetch upstream
    fro = !git fetch origin && git rebase origin/master
    fru = !git fetch upstream && git rebase upstream/master
    branch = auto
    diff = auto
    interactive = auto
    status = auto
    editor = vim