Skip to content
Vassilis Triglianos edited this page Nov 20, 2017 · 12 revisions

Short guide

When I want to create a new branch:

    #create a new branch named "mine" from the current branch
    $ git checkout -b mine

    # do some stuff and commit changes
    # after you have make changes to some files you can add them all (-A option) for your next commit
    $ git add -A
    
    # commit the changes
    $ git commit -m "Meaningful commit message"

    # the following command also creates a remote branch 'origin/my_branch' for 'my_branch' 
    # and lets git know that you want 'my_branch' to start tracking 'origin/my_branch':
    $ git push -u origin my_branch

    # I can now pull or push, without having to specify the current branch
    # as an argument; I just mess around with the checked-out branch:
    $ git push
    $ git pull

When I want to work on somebody else's branch:

    # fetch 'not_mine' from 'origin' and let git know I want a new branch
    # called 'not_mine' to track 'origin/not_mine'
    $ git fetch origin not_mine:not_mine

    # then we can push and pull just the current branch, without
    # having to pass it as an argument
    $ git checkout not_mine
    $ git push
    $ git pull

Typical workflow

    # first update local master
    $ git checkout master
    $ git pull

    # create a branch for the feature you want to work on
    $ git checkout -b feat-myfeature

    # push it to remote and track the remote branch
    $ git push -u origin feat-myfeature 

    #do stuff here

    #commit
    $ git commit -m "Meaningful commit message"

    # At this point it may be a good idea to open a pull request so
    # that you can get feedback on your work.
    # You can do this from the github website.

    #pull updates from remote and the push your work to remote
    $ git pull
    $ git push

    # When it's time to merge to the master you do the following steps:
    # First pull the latest changes to your local master branch...
    $ git checkout master
    $ git pull

    # ... then merge your branch on top of master
    $ git merge feat-myfeature
    $ git push master

    # OR rebase your branch on top of master
    $ git rebase feat-myfeature
    $ git push master

Some helpful tips

    # to see info about the remote and which branches you're tracking
    $ git remote show origin
      * remote origin
        Fetch URL: [email protected]:jacquesd/ASQ.git
        Push  URL: [email protected]:jacquesd/ASQ.git
        HEAD branch (remote HEAD is ambiguous, may be one of the following):
          master
          feat-myfeature
        Remote branches:
          master   tracked
          feat-myfeature tracked
        Local branches configured for 'git pull':
          master   merges with remote master
          feat-myfeature merges with remote feat-myfeature
        Local refs configured for 'git push':
          master   pushes to master   (up to date)
          feat-myfeature pushes to feat-myfeature (up to date)

    # To delete a local and remote branch
    $ git push origin --delete <branchName>

    #The above is syntactic sugar for
    $ git push origin :<branchName>

    # and yes the ':' is a rather obtuse syntax for deleting a branch!

Tutorials

  1. Git Magic
  2. Pro Git
  3. Screencasts
  4. Stack overflow tutorial (answers to common questions in a structured way): http://stackoverflow.com/questions/315911/git-for-beginners-the-definitive-practical-guide
  5. Heroku Cheat Sheet for Git
  6. Visual Git Cheat Sheet

Rebase

Take a look here to see why git rebase is important: http://stackoverflow.com/questions/5250817/git-rebase-loses-history-then-why-rebase