Jump to content

Git/Advanced

From Wikibooks, open books for an open world
< Git

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

[edit | edit source]

One way to check out a remote git repository is

$ git clone ssh:https://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 /remote/path/to/repo

or

$ cd repo
$ git pull

then

$ git push

Checking out local repositories

[edit | edit source]

git clone also works for local repositories:

$ git clone /local/path/to/repo

Checking out remote branches

[edit | edit source]

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 remoteBranchName:localBranchName

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

Git allows you to specify some tags in order to focus on some things in the history[1].

In order to add an annotated tag:

$ git tag -a mytag

or also:

$ git tag -a mytag my-branch

To add a lightweight tag:

$ git tag mytag

To force overwriting existing tag:

$ git tag -f mytag HEAD

To display previous tags:

$ git tag 

Tags can be pushed to remote with

$ git push --tags

To position the repo on a tag:

git checkout tags/0.3.4

Tags vs Branches

[edit | edit source]

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.

In practice, tags are used to designate the software versioning, and are named with numbers (ex: v1.0.2).


Example of branches with tags.
Example of branches with tags.

Create and Apply a Patch

[edit | edit source]

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

References

[edit | edit source]
  1. https://git-scm.com/book/en/Git-Basics-Tagging