Difference between revisions of "Git howto"

From Freeplane - free mind mapping and knowledge management software
m
m
Line 1: Line 1:
 
= Working with freeplane's git repository =
 
= Working with freeplane's git repository =
  
Please read [http://git-scm.com/book/en/Git-Basics the chapter on git basics]
+
You can (and probably want to) use eclipse for most operations. I try to
in order to get started with git. You should understand how to
+
explain this in each section, but it is a good idea to read the
stage and commit changes.  You can (and probably want to) use eclipse for
+
[http://wiki.eclipse.org/EGit/User_Guide EGit documentation].
most operations. I try to explain this in each section, but it is a good
 
idea to read the [http://wiki.eclipse.org/EGit/User_Guide EGit
 
documentation].
 
  
 
*NOTE*: These instructions cover the temporary freeplane repository on github, it will
 
*NOTE*: These instructions cover the temporary freeplane repository on github, it will
Line 39: Line 36:
  
 
This can be done more easily
 
This can be done more easily
[http://wiki.eclipse.org/EGit/User_Guide#Starting_from_existing_Git_Repositories
+
[http://wiki.eclipse.org/EGit/User_Guide#Starting_from_existing_Git_Repositories using Eclipse].
using Eclipse]. This page also describes how to import a repository into eclipse.
+
This page also describes how to import a repository into eclipse.
 +
 
 +
== Basic workflow with one branch ==
 +
Please read [http://git-scm.com/book/en/Git-Basics the chapter on git basics]
 +
in order to get started with git. You should understand how to stage and commit changes.
 +
 
 +
In the following there is a ''simplified'' list of steps for working with
 +
git. Please read the following sections!
 +
 
 +
# clone repo (see above)
 +
# switch to the desired branch (see next section for more about branches!)
 +
$ git checkout <branch> # Eclipse: ''Team->Switch to''
 +
# make sure you have the latest version of the branch (pull = fetch + merge)
 +
$ git pull
 +
# (resolve and commit any merge conflicts)
 +
# modify files...
 +
# add the modifications to the ''index'':
 +
$ git add foo.java # (''Team->Add to index'' in eclipse)
 +
# create a ''commit'' from the changes in the index:
 +
$ git commit [-m message]
 +
(if you omit -m then you will be prompted for a commit message)
 +
create more commits, this is all locally!
 +
# push changesets (commits) to sourceforge:
 +
$ git push
 +
(you might have to pull before the push if someone has pushed to the branch
 +
after you pulled!)
  
 
== Working with branches ==
 
== Working with branches ==
Line 51: Line 73:
 
branches):
 
branches):
  
  git config push.default tracking
+
$ git config push.default tracking
 
    
 
    
 
(you can equivalently set this to ''upstream'' in recent git versions). The
 
(you can equivalently set this to ''upstream'' in recent git versions). The
Line 57: Line 79:
  
 
=== Switch to another branch ===
 
=== Switch to another branch ===
  git checkout <branchname>
+
  $ git checkout <branchname>
  
 
(Team->Switch To->... in Eclipse)
 
(Team->Switch To->... in Eclipse)
Line 67: Line 89:
  
 
  # commit everything that should go into the new branch
 
  # commit everything that should go into the new branch
  git status
+
  $ git status
  git add <file>
+
  $ git add <file>
 
  [...]
 
  [...]
  git commit -m "my changes"
+
  $ git commit -m "my changes"
  
 
  # create new branch locally
 
  # create new branch locally
  git checkout -b <newbranch>
+
  $ git checkout -b <newbranch>
 
   
 
   
 
  # check commit log to see that above commit is in there
 
  # check commit log to see that above commit is in there
  git log
+
  $ git log
 
   
 
   
 
  # new branch must be visible and selected:
 
  # new branch must be visible and selected:
  git branch -a   
+
  $ git branch -a   
 
   
 
   
 
  # create branch remotely, use -u to automatically configure upstream location
 
  # create branch remotely, use -u to automatically configure upstream location
  git push -u origin <newbranch>
+
  $ git push -u origin <newbranch>
 
  # this should output something like this:
 
  # this should output something like this:
 
   Branch <newbranch> set up to track remote branch <newbranch> from origin.  
 
   Branch <newbranch> set up to track remote branch <newbranch> from origin.  
Line 88: Line 110:
  
 
  # remote branch of <newbranch> must be visible:
 
  # remote branch of <newbranch> must be visible:
  git branch -a
+
  $ git branch -a
  
 
So the short story is:
 
So the short story is:
  git checkout -b <newbranch>
+
  $ git checkout -b <newbranch>
  git push -u origin <newbranch>
+
  $ git push -u origin <newbranch>
  
 
=== How to merge branches ===
 
=== How to merge branches ===
  
  git checkout <destination_branch>
+
  $ git checkout <destination_branch>
  git merge <source_branch>
+
  $ git merge <source_branch>
  
 
If you want to abort a merge (revert the working directory to the state
 
If you want to abort a merge (revert the working directory to the state
 
before the merge command), do this:
 
before the merge command), do this:
 
   
 
   
  git reset --hard
+
  $ git reset --hard
  
 
(WARNING: this will remove all uncommitted changes!!)
 
(WARNING: this will remove all uncommitted changes!!)
Line 114: Line 136:
  
 
==== Merge master->dev-branch ====
 
==== Merge master->dev-branch ====
  git checkout <devbranch>
+
  $ git checkout <devbranch>
  git merge master
+
  $ git merge master
  
 
==== Merge dev-branch->master ====
 
==== Merge dev-branch->master ====
  git checkout master
+
  $ git checkout master
  git merge <devbranch>
+
  $ git merge <devbranch>
  
 
=== How to remove a branch ===
 
=== How to remove a branch ===
Line 126: Line 148:
 
  # (use -D instead if you want to delete a branch
 
  # (use -D instead if you want to delete a branch
 
  #  that is not fully merged into HEAD!) <-- TODO:??
 
  #  that is not fully merged into HEAD!) <-- TODO:??
  git checkout -d <branchname>
+
  $ git checkout -d <branchname>
  
 
  # delete remote branch:
 
  # delete remote branch:
  git push origin --delete <branchname>
+
  $ git push origin --delete <branchname>
  
 
Now the local and the remote tracking branch should be gone:
 
Now the local and the remote tracking branch should be gone:
Line 141: Line 163:
 
do a:
 
do a:
  
  git remote prune origin
+
  $ git remote prune origin
  
 
in that other checkout. TODO: but it still exists there as a local branch!
 
in that other checkout. TODO: but it still exists there as a local branch!
Line 169: Line 191:
 
=== Work on your feature branch ===
 
=== Work on your feature branch ===
 
  # switch to feature branch
 
  # switch to feature branch
  git checkout <feature>
+
  $ git checkout <feature>
  
 
  # fetch and merge changes (in case another dev works on this)
 
  # fetch and merge changes (in case another dev works on this)
  git pull
+
  $ git pull
  
 
  # you might have to resolve conflicts: (might want to use 'git mergetool'
 
  # you might have to resolve conflicts: (might want to use 'git mergetool'
  # or [http://wiki.eclipse.org/EGit/User_Guide#Resolving_a_merge_conflict Eclipse])
+
  # or [http://wiki.eclipse.org/EGit/User_Guide#Resolving_a_merge_conflict Eclipse/EGit])
 
  for each conflict x:
 
  for each conflict x:
 
   1. resolve conflict in x, remove markers
 
   1. resolve conflict in x, remove markers
 
   2. git add x # mark as resolved
 
   2. git add x # mark as resolved
 
  # commit the conflict resolution(s)
 
  # commit the conflict resolution(s)
  git commit
+
  $ git commit
  
  # [http://git-scm.com/book/en/Git-Basics the chapter on git basics]
+
  $ git add ...
git add ...
+
  $ git commit
  git commit
 
 
  [...]
 
  [...]
  
Line 190: Line 211:
 
  # make sure that push.default=tracking|upstream (see above),
 
  # make sure that push.default=tracking|upstream (see above),
 
  # otherwise other branches will be pushed as well!
 
  # otherwise other branches will be pushed as well!
  git push [--dry-run]
+
  $ git push [--dry-run]
  
 
=== Merging ===
 
=== Merging ===

Revision as of 18:37, 1 August 2012

Working with freeplane's git repository

You can (and probably want to) use eclipse for most operations. I try to explain this in each section, but it is a good idea to read the EGit documentation.

  • NOTE*: These instructions cover the temporary freeplane repository on github, it will

need to be adapted to the sourceforge hosting service.

Checkout

Checking out is termed cloning in git speak:

$ cd ~
$ mkdir git
$ cd git
$ git clone https://github.com/fnatter/freeplane-git.git freeplane

If you want/have read-only access, use this as the last command:

$ git clone git://github.com/fnatter/freeplane-git.git freeplane

However, this will only configure the master branch (named trunk in other VCS's such as svn) locally:

$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/docear_trunk
  remotes/origin/master

In order to check out a branch (which is simply a reference to a commit) that currently only exists remotely (the remotes/origin/* references are called remote tracking branches), simply switch to that branch:

$ git checkout docear_trunk

This can be done more easily using Eclipse. This page also describes how to import a repository into eclipse.

Basic workflow with one branch

Please read the chapter on git basics in order to get started with git. You should understand how to stage and commit changes.

In the following there is a simplified list of steps for working with git. Please read the following sections!

  1. clone repo (see above)
  2. switch to the desired branch (see next section for more about branches!)
$ git checkout <branch> # Eclipse: Team->Switch to
  1. make sure you have the latest version of the branch (pull = fetch + merge)
$ git pull
  1. (resolve and commit any merge conflicts)
  2. modify files...
  3. add the modifications to the index:
$ git add foo.java # (Team->Add to index in eclipse)
  1. create a commit from the changes in the index:
$ git commit [-m message]

(if you omit -m then you will be prompted for a commit message) create more commits, this is all locally!

  1. push changesets (commits) to sourceforge:
$ git push

(you might have to pull before the push if someone has pushed to the branch after you pulled!)

Working with branches

Branches are very central to git. Do not hesitate to create feature, team and (of course) maintenance branches.

You probably want set push.default which makes sure that only the current branch is pushed to its upstream branch (and NOT all configured branches):

$ git config push.default tracking
 

(you can equivalently set this to upstream in recent git versions). The same thing happens when you select Team->Push To Upstream from eclipse.

Switch to another branch

$ git checkout <branchname>

(Team->Switch To->... in Eclipse)

How to create a new branch

New (feature) branches should be named <user>/<feature> or (if several devs work on a feature): <feature>.

# commit everything that should go into the new branch
$ git status
$ git add <file>
[...]
$ git commit -m "my changes"
# create new branch locally
$ git checkout -b <newbranch>

# check commit log to see that above commit is in there
$ git log

# new branch must be visible and selected:
$ git branch -a  

# create branch remotely, use -u to automatically configure upstream location
$ git push -u origin <newbranch>
# this should output something like this:
  Branch <newbranch> set up to track remote branch <newbranch> from origin. 
# (important if you want to use git pull/push without specifying a remote/refspec)
# remote branch of <newbranch> must be visible:
$ git branch -a

So the short story is:

$ git checkout -b <newbranch>
$ git push -u origin <newbranch>

How to merge branches

$ git checkout <destination_branch>
$ git merge <source_branch>

If you want to abort a merge (revert the working directory to the state before the merge command), do this:

$ git reset --hard

(WARNING: this will remove all uncommitted changes!!)

In eclipse, this can be achieved by:

  • Team->Switch To and choose <destination_branch>
  • Team->Merge... and choose <source_branch>

TODO: how to make eclipse update the list of branches etc. (e.g. if a branch has been deleted from eclipse)??

Merge master->dev-branch

$ git checkout <devbranch>
$ git merge master

Merge dev-branch->master

$ git checkout master
$ git merge <devbranch>

How to remove a branch

# remove branch locally
# (use -D instead if you want to delete a branch
#  that is not fully merged into HEAD!) <-- TODO:??
$ git checkout -d <branchname>
# delete remote branch:
$ git push origin --delete <branchname>

Now the local and the remote tracking branch should be gone:

$ git branch -a
  docear_trunk
* master
  remotes/origin/docear_trunk
  remotes/origin/master

NOTE: The stale branch will still exist in other checkouts, until you do a:

$ git remote prune origin

in that other checkout. TODO: but it still exists there as a local branch!

Check whether your branches are set up correctly

Make sure your branches are set up correctly for push/pull:

$ git remote show origin
* remote origin
  Fetch URL: https://github.com/fnatter/freeplane-git.git
  Push  URL: https://github.com/fnatter/freeplane-git.git
  HEAD branch: master
  Remote branches:
    docear_trunk tracked
    master       tracked
  Local branches configured for 'git pull':
    docear_trunk merges with remote docear_trunk
    master       merges with remote master
  Local refs configured for 'git push':
    docear_trunk pushes to docear_trunk (up to date)
    master       pushes to master       (up to date)

General workflow

Create a dev branch (see section on creating branches above).

Work on your feature branch

# switch to feature branch
$ git checkout <feature>
# fetch and merge changes (in case another dev works on this)
$ git pull
# you might have to resolve conflicts: (might want to use 'git mergetool'
# or Eclipse/EGit)
for each conflict x:
  1. resolve conflict in x, remove markers
  2. git add x # mark as resolved
# commit the conflict resolution(s)
$ git commit
$ git add ...
$ git commit
[...]
# This will push commits to the remote branch that is tracked
# make sure that push.default=tracking|upstream (see above),
# otherwise other branches will be pushed as well!
$ git push [--dry-run]

Merging

Merge master -> <devbranch> (see section on merging above) every once-in-a-while.

After a final code review (on your dev branch), merge <devbranch> -> master (see section on merging above).