Git/Advanced

      This tutorial covers some of the more advanced, multi-user features of git. For the single-user features, please go to the Single developer basics.

      Checking out remote repositories

      One way to check out a remote git repository is

      $ git clone ssh://username@server.com:port/remote/path/to/repo
      

      Now you have a local copy of that repository. You can use all the commands that were introduced in the Single developer basics. Once you are done, you might want to check in your changes to the central repository again.

      First you want to do a "git pull" in case the repository has changed in the meantime and you might have to merge your branch with the repository. After merging, you can use "git push" to send your changes to the repository:

      $ git pull
      $ git push
      

      Checking out local repositories

      git clone also works for local repositories:

      $ git clone /local/path/to/repo
      

      Checking out remote branches

      You might also want to check out remote branches, work on them and check in your local branches. First you might want to know which branches are available:

      $ git branch -r
      $ git remote show origin
      

      Get a remote branch (pull into a local branch):

      $ git pull origin localBranchName:remoteBranchName
      

      Update a remote branch (push a local branch into a remote branch):

      $ git push origin localBranchName:remoteBranchName
      

      This assumes that you have a remote repository called "origin". You can check this with git remote.

      If you want to create a local branch from a remote branch, use:

      $ git checkout -b mylocalbranch origin/maint
      

      Deleting remote branches works like this

      $ git push origin :remoteBranchNameToDelete
      

      The following command synchronizes branches $ git fetch origin

      Tags

      In order to add a tag:

      $ git tag -a mytag
      

      or also:

      $ git tag -f mytag HEAD
      

      Tags can be pushed to remote with

      $ git push --tags
      

      Tags vs Branches

      Both tags and branches point to a commit, they are thus aliases for a specific hash and will save you time by not requiring to type in a hash.

      The difference between tags and branches are that a branch always points to the top of a development line and will change when a new commit is pushed whereas a tag will not change. Thus tags are more useful to "tag" a specific version and the tag will then always stay on that version and usually not be changed.

      Create and Apply a Patch

      In order to create a plain text patch (series) for the changes between origin and master, use

      $ git format-patch origin/master
      

      In order to apply a submitted plain text patch (series), use

      $ git apply --stat  P1.txt  #see the stats, how much will the path change?
      $ git apply --check P1.txt  #check for problems
      $ git am < P1.txt           #apply the patches in the correct order
      
      Last modified on 5 December 2012, at 22:44