Difference between revisions of "An Introduction to Git"

From GnuCash
Jump to: navigation, search
m (Branches: Remove svn distractions)
m (Commit: Update link)
 
(23 intermediate revisions by 4 users not shown)
Line 1: Line 1:
== Introduction ==
 
 
 
This page is for explaining the use of git in the development process to programmers, documenters or translators who are not familiar with git.
 
This page is for explaining the use of git in the development process to programmers, documenters or translators who are not familiar with git.
Those already familiar with git may find the [[Git]] page more suitable.
+
;Alternatives: For really trivial patches like <q>fixing a typo</q> you can also follow [[Simple Pull Request]] without installing Git.
 +
:[[Git]] contains a few more technical details and tricks..
  
 
Developers who have write (commit) access to the official (canonical) GnuCash git repositories are often termed '''Committers'''.
 
Developers who have write (commit) access to the official (canonical) GnuCash git repositories are often termed '''Committers'''.
Line 8: Line 7:
  
 
These instructions are only an unofficial suggested methodology. Git is a very powerful tool and there are often many different ways to do something. If you know of a better way, please feel free to update this page.
 
These instructions are only an unofficial suggested methodology. Git is a very powerful tool and there are often many different ways to do something. If you know of a better way, please feel free to update this page.
 +
 +
[[Category:Development]][[Category:Git]]
  
 
= What is Git? =
 
= What is Git? =
 
   
 
   
[http://git-scm.com/ Git] is a ''[https://en.wikipedia.org/wiki/Distributed_version_control distributed]'' '''[https://en.wikipedia.org/wiki/Version_control version control system]''' (VCS) originally developed by Linus Torvalds for managing Linux source code without requiring a central server. It is also the primary VCS used by the [http://www.gnome.org Gnome] and [http://www.freedesktop.org Free Desktop] projects. You can get the latest version for your system and read a rich variety of online documentation at [http://git-scm.com/ Git's Home]. In particular, ''Pro Git'' by Scott Chacon is available in several languages for free online reading at [http://git-scm.com/book Git Book], where you can also download the English version as a PDF, ebook, or mobi.
+
[{{URL:wp}}Git Git] is a ''[{{URL:wp}}Distributed_version_control distributed]'' '''[{{URL:wp}}Version_control version control system]''' (VCS) originally developed by Linus Torvalds for managing Linux source code without requiring a central server. You can get the latest version for your system and read a rich variety of online documentation at [{{URL:git-scm}} Git's Home]. In particular, ''Pro Git'' by Scott Chacon is available in several languages for free online reading at [{{URL:git-scm}}book Git Book].
  
If you know nothing about version control, that doesn't matter. Just read [http://git-scm.com/book/en/v2/Getting-Started-About-Version-Control Getting Started - About Version Control]. There in the left column you can select your language. 
+
If you know nothing about version control, read [{{URL:git}}book/en/v2/Getting-Started-About-Version-Control Getting Started - About Version Control].  
  
Another good place to start is [https://help.github.com/articles/good-resources-for-learning-git-and-github/ Good Resources for Learning Git and GitHub] - not only if you want to use GitHub for [[#Pull Requests]].
+
Another good place to start is [https://help.github.com/articles/good-resources-for-learning-git-and-github/ Good Resources for Learning Git and GitHub] - especially if you want to use GitHub for [[#Pull Requests]].
  
  git help
+
;On the command line: <Syntaxhighlight lang="sh">git help</Syntaxhighlight> will give you the list of commands and
will give you a list of usual commands and
+
:<Syntaxhighlight lang="sh">git help $COMMAND</Syntaxhighlight> will show you the manual page of that command.
git help <command>
 
will show you the manual page of that command.
 
  
There are several GUIs available for git, e.g.:
+
==Tools==
 +
While pure <tt>git</tt> has only a ''command line interace'' ('''CLI''') there are several ''graphical user interface''s ('''GUI''')s available for git, for example:
 
* git gui
 
* git gui
 
* gitk (shows the history)
 
* gitk (shows the history)
* [http://www.sourcetreeapp.com/ SourceTree] for Windows or Mac
+
*; Windows or Mac only:
and most IDEs have plugins for it.
+
** [https://desktop.github.com/ GitHub Desktop]
= What has that to do with Gnucash? =
+
** [http://www.sourcetreeapp.com/ SourceTree], and
 +
* most [[:Category:IDE|IDE]]s have plugins for it.
 +
 
 +
= What does that have to do with Gnucash? =
 
We manage our code and documentation sources in Git.
 
We manage our code and documentation sources in Git.
  
Our '''public repositories''' are ''mirrored on Github'': for [https://github.com/Gnucash/gnucash code], [https://github.com/Gnucash/gnucash-docs documentation] and for the [https://github.com/Gnucash/gnucash-htdocs website]. These are updated from the ''primary repository'' by commit hooks, so barring technical problems changes appear in these repositories within a few seconds of being committed to the primary.
+
Our '''public repositories''' are ''mirrored on Github'': for
 
+
:;the program:[{{URL:GH}}Gnucash/gnucash gnucash],
<tt>code.gnucash.org</tt> is the server that hosts the '''canonical git repository''', wiki, [[Mailing Lists]], automated win32 builds, and the nightly builds  of the [http://code.gnucash.org/docs/ documentation and API docs] (via [[Doxygen]]) for both [[#Branchs|branchs]].
+
:;its documentation:[{{URL:GH}}Gnucash/gnucash-docs gnucash-docs],
 +
:;several OS dependent installer packages: [{{URL:GH}}Gnucash/ gnucash-on-''OS''], and
 +
:;our website:[{{URL:GH}}Gnucash/gnucash-htdocs gnucash-htdocs].
 +
: These are updated from the ''primary repository'' by commit hooks, so barring technical problems changes appear in these repositories within a few seconds of being committed to the primary repository.
  
 
= Using the Github Repository =
 
= Using the Github Repository =
  
 
== Branches ==
 
== Branches ==
There are 2 important branches in most of our repositories:
+
;Program and Documentation: can have 3 important branches in our repositories{{Git branchs rename.ref}}:
:;master: is the default branch in git. ''New features'' and their documentation should be based on this branch.
+
:;stable: is the default branch in git. ''Bugfixes'', ''translations'', and improvements to the ''documentation'' should usually be applied on this branch.
:;maint: ''Bugfixes'', ''translations'', improvements of the ''documentation'' should usually be applied on this branch.
+
:;future: will be created when the first ''New feature'' preferrable from [[Release_Schedule#Goals_for_6.0]] gets implemented.
 +
:;unstable: will only appear in the beta testing phase with {{BetaSeries}} releases.
 +
;Packaging Repos <tt>gnucash-on-…</tt>: have only a <tt>master</tt> branch,
 +
;Website: additional a <tt>beta</tt> branch for test purposes.
  
Non-Committers should always work in a branch specific to the bug or feature being worked on, not the master or maint branch. Name the feature or working branch after the bug or feature - e.g. bug-12345 or invoice-new-button.
+
Non-Committers should always work in their ''own working branch'' specific to the bug or feature being worked on. Name your working branch after the bug or feature - e.g. <tt>bug-12345</tt> or <tt>invoice-new-button</tt>.
  
 
== Patches or Pull Requests? ==
 
== Patches or Pull Requests? ==
 
There are 2 main ways to submit bug fixes, modifications or enhancements for review:
 
There are 2 main ways to submit bug fixes, modifications or enhancements for review:
  
:;GitHub pull requests: These are preferred by the developers due to rich GitHub code review functionality, but are more complicated to create than patches.
+
:;GitHub pull requests: These are preferred by the developers due to rich GitHub code review functionality, but are slightly more complicated to create than patches.
  
 
::Proceed to [[#Pull Requests]].
 
::Proceed to [[#Pull Requests]].
Line 57: Line 66:
  
 
Create the directory you wish to hold your repository or repositories in and make it current.
 
Create the directory you wish to hold your repository or repositories in and make it current.
Assuming you wish it to be called github in your home directory:
+
Assuming you wish it to be called git to store all your git repos in your home directory: <syntaxhighlight lang="sh">
  mkdir ~/github
+
mkdir ~/git
  cd ~/github
+
cd ~/git
 
+
</syntaxhighlight>
Create a copy of the required repository on your PC by cloning:
 
  
  git clone https://github.com/Gnucash/gnucash.git
+
Create a copy of the required repository on your PC by cloning: <syntaxhighlight lang="sh">
:;other URLs: https://github.com/Gnucash/gnucash-docs.git (documentation)
+
git clone https://github.com/Gnucash/gnucash.git
::https://github.com/Gnucash/gnucash-htdocs.git (website)
+
</syntaxhighlight>
::See https://github.com/Gnucash for further repos e.g. with OS/distro specific build scripts.
+
:;other URLs: {{URL:GH}}Gnucash/gnucash-docs.git (documentation)
 +
::{{URL:GH}}Gnucash/gnucash-htdocs.git (website)
 +
::See {{URL:GH}}Gnucash for further repos e.g. with OS/distro specific build scripts.
  
The clone example above will create the local repository
+
The clone example above will create the local repository <tt>~/github/gnucash/</tt>
  ~/github/gnucash
 
 
and also defines a ''remote'' named ''origin'' pointing to the ''cloned from'' repository which you can use in future as a shortcut instead of the full URL.
 
and also defines a ''remote'' named ''origin'' pointing to the ''cloned from'' repository which you can use in future as a shortcut instead of the full URL.
  
Change into the repository directory:
+
Change into the repository directory: <syntaxhighlight lang="sh">
  cd gnucash
+
cd gnucash
 +
</syntaxhighlight>
  
 
Only the default branch master will have been cloned.
 
Only the default branch master will have been cloned.
If you wish to work on a modification to the maint branch, create local branch maint to track your remote branch:
+
If you wish to work on a modification to the <tt>stable</tt> branch, create a local branch <tt>stable</tt> to track your remote branch: <syntaxhighlight lang="sh">
  git branch --track maint origin/maint
+
git branch --track stable origin/stable
 +
</syntaxhighlight>
  
 
== Create a working-branch (Patches) ==
 
== Create a working-branch (Patches) ==
Line 83: Line 94:
 
* Use a particular working branch for ''only one'' bug or feature. This will make it much easier to make changes and generate new patches should that prove necessary.
 
* Use a particular working branch for ''only one'' bug or feature. This will make it much easier to make changes and generate new patches should that prove necessary.
 
* Create a local (on your PC) branch to work in:
 
* Create a local (on your PC) branch to work in:
:: '''Bug fixes''' should branch from ''maint'' unless the bug applies only to the unstable version.
+
:: '''Bug fixes''' should branch from <tt>stable</tt> unless the bug applies only to the <tt>unstable</tt> or <tt>future</tt> version.
:: '''New features''' must branch from ''master''.
+
:: '''New features''' must branch from ''future''.
The following example is for a new feature; ''substitute <tt>maint</tt> for <tt>master</tt> if you're doing a bug-fix''.
+
The following example is for a new feature; ''substitute <tt>stable</tt> for <tt>future</tt> if you're doing a bug-fix''.
  
Checkout the branch (master or maint) on which your working-branch will be based, e.g.:
+
Checkout the branch (stable or future) on which your working-branch will be based, e.g.: <syntaxhighlight lang="sh">
  git checkout master
+
git checkout stable
 +
</syntaxhighlight>
  
Note: checkout prepares your local repository ready for work on a particular branch by resetting all files back to how they were at the time they were last committed.
+
;Note: <tt>checkout</tt> prepares your local repositories working directory ready for work on a particular branch by resetting all files back to how they were at the time they were last committed.
It is much easier if you only checkout a branch after committing all changes you have made to the current branch.
+
:It is much easier if you only checkout a branch after committing all changes you have made to the current branch.
 
You will get a warning if you try to change branches and there are uncommitted changes.
 
You will get a warning if you try to change branches and there are uncommitted changes.
It is not wise to leave a file open in an editor while checking out a branch as you may accidentally save a version from another branch.
+
:It is not wise to leave a file open in an editor while checking out a branch as you may accidentally save a version from another branch. <syntaxhighlight lang="sh">
 
+
git branch working-branch        # Creates branch ''working-branch'' from the current branch
  git branch working-branch        # Creates branch ''working-branch'' from the current branch
+
git checkout working-branch # make working-branch current
  git checkout working-branch   # make working-branch current
+
# OR
OR
+
git checkout -b working-branch  # create ''working-branch'' from the current branch and check it out in one step
  git checkout -b working-branch  # create ''working-branch'' from the current branch and check it out in one step
+
</syntaxhighlight>
  
 
You are now ready to make and test your changes in your local working branch.
 
You are now ready to make and test your changes in your local working branch.
  
 
== Commit ==
 
== Commit ==
See http://wiki.gnucash.org/wiki/Documentation_Update_Instructions#Step_11_Commit_your_Changes
+
See [[Documentation Improvement#Commit Your Changes]]
  
== Sync your local master or maint from origin (Patches) ==
+
== Sync your local stable or future from origin (Patches) ==
 
After you have completed your changes, to minimize the chances of your future patch conflicting with any changes that may have been committed
 
After you have completed your changes, to minimize the chances of your future patch conflicting with any changes that may have been committed
 
to the official GnuCash repositories while you were working:
 
to the official GnuCash repositories while you were working:
  
* Update the local master or maint branch (depending on which your working branch was created from) in your local repository from origin:
+
* Update the local stable or future branch (depending on which your working branch was created from) in your local repository from origin: <syntaxhighlight lang="sh">
  git checkout master
+
git checkout stable
  git pull origin master
+
git pull origin stable
  or
+
# or
  git checkout maint
+
git checkout future
  git pull origin maint
+
git pull origin future
 +
</syntaxhighlight>
  
* Rebase your working branch commits based on the updated master or maint:
+
* Rebase your working branch commits based on the updated stable or future: <syntaxhighlight lang="sh">
  git rebase master working-branch
+
git rebase stable working-branch
 +
</syntaxhighlight>
  
You can update both master and maint if you wish but you only need to update the branch on which your working branch was based.
+
You can update both stable and future if you wish but you only need to update the branch on which your working branch was based.
  
* Test again to make sure everthing works with the rebased code
+
* Test again to make sure everything works with the rebased code
  
 
== Create Patch(es) ==
 
== Create Patch(es) ==
 
A patch file is created for each commit.
 
A patch file is created for each commit.
 
* Use <tt>git rebase -i</tt> as necessary to make a clean series of patches for complex changes.
 
* Use <tt>git rebase -i</tt> as necessary to make a clean series of patches for complex changes.
* Use <tt>git format-patch</tt> to create the actual patches from your commits:
+
* Use <tt>git format-patch</tt> to create the actual patches from your commits: <syntaxhighlight lang="sh">
  cd ~/github/gncucash
+
cd ~/github/gncucash
  git format-patch origin/master..master
+
git format-patch origin/stable..stable
 
+
# or
(or <tt>git diff</tt>) to prepare them.
+
git diff # to prepare them.
 +
</syntaxhighlight>
  
 
* Attach the resulting patch(es) to the bug report. It is not necessary to email anyone as the relevant people are automatically emailed when you attach a patch to a bug.
 
* Attach the resulting patch(es) to the bug report. It is not necessary to email anyone as the relevant people are automatically emailed when you attach a patch to a bug.
Line 136: Line 151:
  
 
== Cleanup ==
 
== Cleanup ==
Once the patch has been either merged or rejected, you can delete the local working branch:
+
Once the patch has been either merged or rejected, you can delete the local working branch: <syntaxhighlight lang="sh">
  git checkout master
+
git checkout stable
  git branch -D working-branch
+
git branch -D working-branch
  E.g.
+
E.g.
  git branch -D bug-99999 # delete local branch bug-99999
+
git branch -D bug-99999 # delete local branch bug-99999
 +
</syntaxhighlight>
  
If you no longer require your local repository, you can just delete the folder on your PC and it's sub-folders.
+
If you no longer require your local repository, you can just delete the folder on your PC and its sub-folders.
  
 
== Prepare for your next patch ==
 
== Prepare for your next patch ==
Follow the instructions in [[#Sync your local master or maint from origin (Patches)]], then [[#Create a working-branch (Patches)]].
+
Follow the instructions in [[#Sync your local stable or future from origin (Patches)]], then [[#Create a working-branch (Patches)]].
  
 
= Pull Requests =
 
= Pull Requests =
If you prefer, you can use a GitHub Pull Request instead of attaching a patch to a bug.
+
If you prefer, you can use a GitHub Pull Request (PR) instead of attaching a patch to a bug. Once set up, submitting changes using PRs is very straightforward and easy.
You'll need to create a GitHub account if you haven't got one already and set it up for ssh access.
 
 
 
'''Suggested remote repository names for Non-Committers Using Pull Requests'''
 
 
 
Use the following names in your local PC repository for remote repositories:
 
 
 
:'''upstream''': The official repository to which only authorized developers can write. E.g.
 
::<tt>github.com/Gnucash/gnucash
 
::or
 
::github.com/Gnucash/gnucash-docs</tt>
 
::These are actually public mirrors for the real GnuCash official repositories.
 
 
 
:'''origin''': This is your personal writable GitHub repository. E.g.
 
::<tt>github.com/[YOUR-GITHUB-USERNAME]/gnucash
 
::or
 
::github.com/[YOUR-GITHUB-USERNAME]/gnucash-docs</tt>
 
::Pull requests are made from here to the ''upstream'' repository.
 
 
 
== Set-Up Your Personal GitHub Repository ==
 
Use Github's '''fork''' feature to set up a your own personal read-write enabled '''clone''' on GitHub:
 
  
In a web browser, go to https://www.github.com, login and search for '''Gnucash'''.
+
See [[Setup for Pull Requests]] for more information on how to create a development environment for PRs.
Click on the link for the required repository, E.g.
 
  Gnucash/gnucash or Gnucash/gnucash-docs
 
Click on the Fork button at top right.
 
This will create
 
  www.github.com/[YOUR-GITHUB-USERNAME]/gnucash-docs
 
 
 
== Set-Up a Local Repository on your PC ==
 
As you will eventually push your modified local repository branch back to your personal GitHub repository, it is useful to
 
create your local PC repository by cloning from your personal GitHub repository, rather than the official
 
GnuCash repository. The remote name '''origin''' in your local repository, is automatically set up to point to where you clone from (I.e. your personal GitHub repository).
 
 
 
Create the directory you wish to hold your repository or repositories in and make it current.
 
Assuming you wish it to be called github in your home directory:
 
  mkdir ~/github
 
  cd ~/github
 
 
 
Clone from your remote personal repository ''origin'':
 
  git clone git@github.com:<YOUR-GITHUB-USERNAME>/gnucash.git
 
 
 
The clone example above will create the local repository
 
  ~/github/gnucash
 
and also defines a ''remote'' named ''origin'' pointing to the ''cloned from'' repository which you can use in future as a shortcut instead of the full URL.
 
 
 
Change into the repository directory:
 
  cd gnucash
 
 
 
Only the default branch master will have been cloned.
 
If you wish to work on a modification which should be applied to the maint branch, create local branch maint to track your remote branch:
 
  git branch --track maint origin/maint
 
  
 
== Create a working-branch (Pull Requests) ==
 
== Create a working-branch (Pull Requests) ==
 
* Create a branch to work in:
 
* Create a branch to work in:
:: '''Bug fixes''' should branch from ''maint'' unless the bug applies only to the unstable version.
+
:: '''Bug fixes''' should branch from <tt>stable</tt> unless the bug applies only to the future version.
:: '''New features''' must branch from ''master''.
+
:: '''New features''' must branch from <tt>future</tt>.
 
* Open a bug in Bugzilla to enable your work to be tracked if one doesn't already exist.
 
* Open a bug in Bugzilla to enable your work to be tracked if one doesn't already exist.
  
The following example is for a new feature and therefore uses master; ''substitute <tt>maint</tt> for <tt>master</tt> if you're doing a bug-fix''.
+
The following example is for a bugfix and therefore uses <tt>stable</tt>; ''substitute <tt>stable</tt> by <tt>future</tt> if you're creating a ''new feature''.
* Use a particular working branch for ''only one'' bug or feature. This will make it much easier to make changes should that prove necessary.
+
* Use a particular working branch for ''only one'' bug or feature. This will make it much easier to make changes should that prove necessary. <syntaxhighlight lang="sh">
 +
git checkout stable              # Checkout the branch (stable or future) on which your new ''working-branch'' will be based
  
  git checkout master              # Checkout the branch (master or maint) on which your new ''working-branch'' will be based
+
git branch working-branch        # Create branch ''working-branch'' from the current branch
 +
git checkout working-branch
 +
# OR
 +
git checkout -b working-branch   # create ''working-branch'' from the current branch and check it out in one step
 +
</syntaxhighlight>
  
  git branch working-branch        # Create branch ''working-branch'' from the current branch
+
;Note: <tt>checkout</tt> prepares your local repositories working directory for work on a particular branch by resetting all files back to how they were at the time that branch was last committed.
  git checkout working-branch
+
:It is much easier if you only checkout a branch after committing all changes you have made to the current branch.
  OR
+
:It is not wise to leave a file open in an editor while checking out a branch as you may accidentally save a version from another branch.
  git checkout -b working-branch  # create ''working-branch'' from the current branch and check it out in one step
 
 
 
Note: ''checkout'' prepares your local repository for work on a particular branch by resetting all files back to how they were at the time that branch was last committed.
 
It is much easier if you only checkout a branch after committing all changes you have made to the current branch.
 
It is not wise to leave a file open in an editor while checking out a branch as you may accidentally save a version from another branch.
 
  
 
You are now ready to make and test your changes in your local working branch.
 
You are now ready to make and test your changes in your local working branch.
  
 
== Commit ==
 
== Commit ==
See http://wiki.gnucash.org/wiki/Documentation_Update_Instructions#Step_11_Commit_your_Changes
+
See [[Documentation_Update_Instructions#Commit_Your_Changes]]
  
== Sync your local master or maint from upstream (Pull Requests) ==
+
== Sync your local stable or future branch from upstream (Pull Requests) ==
 
After you have completed and tested your changes, to minimize the chances of your future pull request conflicting with any changes that may have been committed
 
After you have completed and tested your changes, to minimize the chances of your future pull request conflicting with any changes that may have been committed
 
to the official GnuCash repositories while you were working:
 
to the official GnuCash repositories while you were working:
Line 233: Line 200:
 
   git remote -v
 
   git remote -v
 
* If '''upstream''' is not already defined, create it. For example
 
* If '''upstream''' is not already defined, create it. For example
 +
  <Syntaxhighlight lang="sh">
 +
  # to use ssh
 
   git remote add upstream ssh://git@github.com/Gnucash/gnucash
 
   git remote add upstream ssh://git@github.com/Gnucash/gnucash
* Update the local master or maint branch (depending on which your working branch is based) in your local repository from upstream:
+
  # or if you don't want to set up ssh keys, use https
   git checkout master
+
  git remote add upstream https://github.com/Gnucash/gnucash.git
   git pull upstream master
+
  </Syntaxhighlight>
 +
* Update the local stable or future branch (depending on which your working branch is based) in your local repository from upstream:
 +
   git checkout stable
 +
   git pull upstream stable
 
   or
 
   or
   git checkout maint
+
   git checkout future
   git pull upstream maint
+
   git pull upstream future
* Update master or maint in your personal GitHub repository (origin) from your local repository:
+
* Update stable or future in your personal GitHub repository (origin) from your local repository:
   git push origin master
+
   git push origin stable
* Rebase your working branch commits based on the updated master or maint:
+
* Rebase your working branch commits based on the updated stable or future:
   git rebase master working-branch
+
   git rebase stable working-branch
 
* Test again to make sure everything works with the rebased code.
 
* Test again to make sure everything works with the rebased code.
  
You can update both master and maint if you wish but you only need to update the branch on which your working branch was based.
+
You can update both stable or future if you wish but you only need to update the branch on which your working branch was based.
  
 
== Push Back To Your Personal Repository ==
 
== Push Back To Your Personal Repository ==
 
If you followed these instructions and cloned to your local working-branch from your personal
 
If you followed these instructions and cloned to your local working-branch from your personal
 
github repository, then the remote name '''origin''' already points to your personal remote github
 
github repository, then the remote name '''origin''' already points to your personal remote github
repository. Check your defined remotes by:
+
repository. Check your defined remotes by <syntaxhighlight lang="console">
  git remote -v
+
$ git remote -v
The output of the above should include:
+
 
  origin git@github.com:<YOUR-GITHUB-USERNAME>/gnucash-docs.git (fetch)
+
origin git@github.com:<YOUR-GITHUB-USERNAME>/gnucash-docs.git (fetch)
  origin git@github.com:<YOUR-GITHUB-USERNAME>/gnucash-docs.git (push)
+
origin git@github.com:<YOUR-GITHUB-USERNAME>/gnucash-docs.git (push)
 +
upstream  https://github.com/gnucash/gnucash-docs (fetch)
 +
upstream  https://github.com/gnucash/gnucash-docs (push)
 +
</syntaxhighlight>
 
so you can push your working-branch to your remote personal github repository by:
 
so you can push your working-branch to your remote personal github repository by:
 
   # Note: no need to checkout your working-branch first
 
   # Note: no need to checkout your working-branch first
Line 262: Line 237:
 
   git push origin bug-99999
 
   git push origin bug-99999
  
If you have '''amend'''ed a local commit that has already been previously pushed to your remote repo, override the remote commit by:
+
If you have '''amend'''ed a local commit that has already been previously pushed to your remote repo, override the remote commit by <Syntaxhighlight lang="sh">
  git push '''--force''' origin working-branch
+
git push '''--force''' origin working-branch
If you already made a pull request before the amendment, github might have difficulties to display your recent changes correctly on the PR page.
+
</syntaxhighlight>
 +
 
 +
If you already made a pull request before the amendment, your reviewers have to refresh their browser to display your recent changes correctly on the PR page.
  
 
If your remote named '''origin''' does not point to your personal github repository
 
If your remote named '''origin''' does not point to your personal github repository
Line 270: Line 247:
  
 
If you don't have a remote name that points to your personal github repository,
 
If you don't have a remote name that points to your personal github repository,
then add a remote name that does, E.g. to add remote named '''mygithub''':
+
then <Syntaxhighlight lang="sh">
  git remote add mygithub ssh://git@github.com/<YOUR-GITHUB-USERNAME>/gnucash
+
# add a remote name that does, E.g. to add remote named '''mygithub''':
then
+
git remote add mygithub ssh://git@github.com/<YOUR-GITHUB-USERNAME>/gnucash
<tt>git push mygithub working-branch</tt>
+
# and push
 +
git push mygithub working-branch
 +
</syntaxhighlight>
  
 
== Create a Pull Request ==
 
== Create a Pull Request ==
 
+
# Now log in to your GitHub account, go to your forked gnucash repository, select <tt>working-branch</tt> from the branch pick list, and click ''pull request''. It's above the "last commit" line on the right in the directory view.
* Now log in to your GitHub account, go to your forked gnucash repository, select <tt>working-branch</tt> from the branch pick list, and click ''pull request''. It's above the "last commit" line on the right in the directory view.
+
# In the resulting form, give your pull request a title and describe its motivation. If it's associated with a bug, use the bug number and title for your title and paste the bug URL into the description. Note that GitHub descriptions use Markdown and that there's a preview tab to help you make sure that everything looks the way you want it.
* In the resulting form, give your pull request a title and describe its motivation. If it's associated with a bug, use the bug number and title for your title and paste the bug URL into the description. Note that GitHub descriptions use Markdown and that there's a preview tab to help you make sure that everything looks the way you want it.
+
# Click the ''Send Pull Request'' button to the right of the description block.
* Click the ''Send Pull Request'' button to the right of the description block.
+
# If a developer requires changes to your pull request, amend your commits as necessary and force-push your branch. :Don't make any changes to that working branch that aren't associated with the pull request!
* If a developer requires changes to your pull request, amend your commits as necessary and force-push your branch. Don't make any changes to that working branch that aren't associated with the pull request!
 
  
 
== Cleanup ==
 
== Cleanup ==
 
* Once the pull request has been reviewed and either merged or rejected, you can delete the branch.
 
* Once the pull request has been reviewed and either merged or rejected, you can delete the branch.
To delete the local branch:
+
:;From your ''local'' computer: <Syntaxhighlight lang="sh">
  git branch -D working-branch
+
git branch -D ${WORKING_BRANCH}
  E.g.
+
# Example:
  git branch -D bug-99999 # delete local branch bug-99999
+
git branch -D bug-99999 # delete local branch bug-99999
To delete the working branch from your personal remote github repository:
+
</syntaxhighlight>
  git push <remote-name> :working-branch # note space before :working-branch
+
:;From your personal ''remote'' github mirror: <Syntaxhighlight lang="sh">
  E.g.
+
git push $REMOTE :${WORKING_BRANCH} # note space before :working-branch
  git push origin :bug-99999
+
# Example:
 +
git push origin :bug-99999
 +
</syntaxhighlight>
  
 
If you no longer require your local repository, you can just delete the folder on your PC and it's sub-folders.
 
If you no longer require your local repository, you can just delete the folder on your PC and it's sub-folders.
  
 
== Prepare for your next working branch ==
 
== Prepare for your next working branch ==
Follow the instructions in [[#Sync your local master or maint from upstream (Pull Requests)]], then [[#Create a working-branch (Pull Requests)]].
+
Follow the instructions in [[#Sync your local stable or future branch from upstream (Pull Requests)]], then [[#Create a working-branch (Pull Requests)]].

Latest revision as of 22:06, 6 January 2024

This page is for explaining the use of git in the development process to programmers, documenters or translators who are not familiar with git.

Alternatives
For really trivial patches like fixing a typo you can also follow Simple Pull Request without installing Git.
Git contains a few more technical details and tricks..

Developers who have write (commit) access to the official (canonical) GnuCash git repositories are often termed Committers. This page is for Non-Committers.

These instructions are only an unofficial suggested methodology. Git is a very powerful tool and there are often many different ways to do something. If you know of a better way, please feel free to update this page.

What is Git?

Git is a distributed version control system (VCS) originally developed by Linus Torvalds for managing Linux source code without requiring a central server. You can get the latest version for your system and read a rich variety of online documentation at Git's Home. In particular, Pro Git by Scott Chacon is available in several languages for free online reading at Git Book.

If you know nothing about version control, read Getting Started - About Version Control.

Another good place to start is Good Resources for Learning Git and GitHub - especially if you want to use GitHub for #Pull Requests.

On the command line
git help
will give you the list of commands and
git help $COMMAND
will show you the manual page of that command.

Tools

While pure git has only a command line interace (CLI) there are several graphical user interfaces (GUI)s available for git, for example:

What does that have to do with Gnucash?

We manage our code and documentation sources in Git.

Our public repositories are mirrored on Github: for

the program
gnucash,
its documentation
gnucash-docs,
several OS dependent installer packages
gnucash-on-OS, and
our website
gnucash-htdocs.
These are updated from the primary repository by commit hooks, so barring technical problems changes appear in these repositories within a few seconds of being committed to the primary repository.

Using the Github Repository

Branches

Program and Documentation
can have 3 important branches in our repositories[1]:
stable
is the default branch in git. Bugfixes, translations, and improvements to the documentation should usually be applied on this branch.
future
will be created when the first New feature preferrable from Release_Schedule#Goals_for_6.0 gets implemented.
unstable
will only appear in the beta testing phase with 5.9xx releases.
Packaging Repos gnucash-on-…
have only a master branch,
Website
additional a beta branch for test purposes.

Non-Committers should always work in their own working branch specific to the bug or feature being worked on. Name your working branch after the bug or feature - e.g. bug-12345 or invoice-new-button.

Patches or Pull Requests?

There are 2 main ways to submit bug fixes, modifications or enhancements for review:

GitHub pull requests
These are preferred by the developers due to rich GitHub code review functionality, but are slightly more complicated to create than patches.
Proceed to #Pull Requests.
Patches
A patch is essentially a specially formatted file containing a list of the files that have been changed, and for each file, before and after listings of the changed lines.

Patches

Set-Up

Create the directory you wish to hold your repository or repositories in and make it current.

Assuming you wish it to be called git to store all your git repos in your home directory:
mkdir ~/git
cd ~/git
Create a copy of the required repository on your PC by cloning:
git clone https://github.com/Gnucash/gnucash.git
other URLs
https://github.com/Gnucash/gnucash-docs.git (documentation)
https://github.com/Gnucash/gnucash-htdocs.git (website)
See https://github.com/Gnucash for further repos e.g. with OS/distro specific build scripts.

The clone example above will create the local repository ~/github/gnucash/ and also defines a remote named origin pointing to the cloned from repository which you can use in future as a shortcut instead of the full URL.

Change into the repository directory:
cd gnucash

Only the default branch master will have been cloned.

If you wish to work on a modification to the stable branch, create a local branch stable to track your remote branch:
git branch --track stable origin/stable

Create a working-branch (Patches)

  • Open a bug in Bugzilla to attach your patch to if one doesn't already exist.
  • Use a particular working branch for only one bug or feature. This will make it much easier to make changes and generate new patches should that prove necessary.
  • Create a local (on your PC) branch to work in:
Bug fixes should branch from stable unless the bug applies only to the unstable or future version.
New features must branch from future.

The following example is for a new feature; substitute stable for future if you're doing a bug-fix.

Checkout the branch (stable or future) on which your working-branch will be based, e.g.:
git checkout stable
Note
checkout prepares your local repositories working directory ready for work on a particular branch by resetting all files back to how they were at the time they were last committed.
It is much easier if you only checkout a branch after committing all changes you have made to the current branch.

You will get a warning if you try to change branches and there are uncommitted changes.

It is not wise to leave a file open in an editor while checking out a branch as you may accidentally save a version from another branch.
git branch working-branch        # Creates branch ''working-branch'' from the current branch
git checkout working-branch	 # make working-branch current
# OR
git checkout -b working-branch   # create ''working-branch'' from the current branch and check it out in one step

You are now ready to make and test your changes in your local working branch.

Commit

See Documentation Improvement#Commit Your Changes

Sync your local stable or future from origin (Patches)

After you have completed your changes, to minimize the chances of your future patch conflicting with any changes that may have been committed to the official GnuCash repositories while you were working:

  • Update the local stable or future branch (depending on which your working branch was created from) in your local repository from origin:
    git checkout stable
    git pull origin stable
    # or
    git checkout future
    git pull origin future
    
  • Rebase your working branch commits based on the updated stable or future:
    git rebase stable working-branch
    

You can update both stable and future if you wish but you only need to update the branch on which your working branch was based.

  • Test again to make sure everything works with the rebased code

Create Patch(es)

A patch file is created for each commit.

  • Use git rebase -i as necessary to make a clean series of patches for complex changes.
  • Use git format-patch to create the actual patches from your commits:
    cd ~/github/gncucash
    git format-patch origin/stable..stable
    # or
    git diff  # to prepare them.
    
  • Attach the resulting patch(es) to the bug report. It is not necessary to email anyone as the relevant people are automatically emailed when you attach a patch to a bug.
  • If a committer asks you to make changes, revise your original commit and make a new patch. Don't submit a patch to be applied on top of an old one. git-rebase -i can be very helpful if you have a series of patches.

Cleanup

Once the patch has been either merged or rejected, you can delete the local working branch:
git checkout stable
git branch -D working-branch
#  E.g.
git branch -D bug-99999	# delete local branch bug-99999

If you no longer require your local repository, you can just delete the folder on your PC and its sub-folders.

Prepare for your next patch

Follow the instructions in #Sync your local stable or future from origin (Patches), then #Create a working-branch (Patches).

Pull Requests

If you prefer, you can use a GitHub Pull Request (PR) instead of attaching a patch to a bug. Once set up, submitting changes using PRs is very straightforward and easy.

See Setup for Pull Requests for more information on how to create a development environment for PRs.

Create a working-branch (Pull Requests)

  • Create a branch to work in:
Bug fixes should branch from stable unless the bug applies only to the future version.
New features must branch from future.
  • Open a bug in Bugzilla to enable your work to be tracked if one doesn't already exist.

The following example is for a bugfix and therefore uses stable; substitute stable by future if you're creating a new feature.

  • Use a particular working branch for only one bug or feature. This will make it much easier to make changes should that prove necessary.
    git checkout stable              # Checkout the branch (stable or future) on which your new ''working-branch'' will be based
    
    git branch working-branch        # Create branch ''working-branch'' from the current branch
    git checkout working-branch
    #  OR
    git checkout -b working-branch   # create ''working-branch'' from the current branch and check it out in one step
    
Note
checkout prepares your local repositories working directory for work on a particular branch by resetting all files back to how they were at the time that branch was last committed.
It is much easier if you only checkout a branch after committing all changes you have made to the current branch.
It is not wise to leave a file open in an editor while checking out a branch as you may accidentally save a version from another branch.

You are now ready to make and test your changes in your local working branch.

Commit

See Documentation_Update_Instructions#Commit_Your_Changes

Sync your local stable or future branch from upstream (Pull Requests)

After you have completed and tested your changes, to minimize the chances of your future pull request conflicting with any changes that may have been committed to the official GnuCash repositories while you were working:

  • Check if remote upstream is already defined
 git remote -v
  • If upstream is not already defined, create it. For example
  # to use ssh
  git remote add upstream ssh://git@github.com/Gnucash/gnucash
  # or if you don't want to set up ssh keys, use https
  git remote add upstream https://github.com/Gnucash/gnucash.git
  • Update the local stable or future branch (depending on which your working branch is based) in your local repository from upstream:
 git checkout stable
 git pull upstream stable
 or
 git checkout future
 git pull upstream future
  • Update stable or future in your personal GitHub repository (origin) from your local repository:
 git push origin stable
  • Rebase your working branch commits based on the updated stable or future:
 git rebase stable working-branch
  • Test again to make sure everything works with the rebased code.

You can update both stable or future if you wish but you only need to update the branch on which your working branch was based.

Push Back To Your Personal Repository

If you followed these instructions and cloned to your local working-branch from your personal github repository, then the remote name origin already points to your personal remote github

repository. Check your defined remotes by
$ git remote -v

 origin	git@github.com:<YOUR-GITHUB-USERNAME>/gnucash-docs.git (fetch)
 origin	git@github.com:<YOUR-GITHUB-USERNAME>/gnucash-docs.git (push)
 upstream  https://github.com/gnucash/gnucash-docs (fetch)
 upstream  https://github.com/gnucash/gnucash-docs (push)

so you can push your working-branch to your remote personal github repository by:

 # Note: no need to checkout your working-branch first
 git push origin working-branch
 E.g.
 git push origin bug-99999
If you have amended a local commit that has already been previously pushed to your remote repo, override the remote commit by
git push '''--force''' origin working-branch

If you already made a pull request before the amendment, your reviewers have to refresh their browser to display your recent changes correctly on the PR page.

If your remote named origin does not point to your personal github repository but you have another remote which does, then substitute your remote name for origin in the above.

If you don't have a remote name that points to your personal github repository,

then
# add a remote name that does, E.g. to add remote named '''mygithub''':
git remote add mygithub ssh://git@github.com/<YOUR-GITHUB-USERNAME>/gnucash
# and push
git push mygithub working-branch

Create a Pull Request

  1. Now log in to your GitHub account, go to your forked gnucash repository, select working-branch from the branch pick list, and click pull request. It's above the "last commit" line on the right in the directory view.
  2. In the resulting form, give your pull request a title and describe its motivation. If it's associated with a bug, use the bug number and title for your title and paste the bug URL into the description. Note that GitHub descriptions use Markdown and that there's a preview tab to help you make sure that everything looks the way you want it.
  3. Click the Send Pull Request button to the right of the description block.
  4. If a developer requires changes to your pull request, amend your commits as necessary and force-push your branch. :Don't make any changes to that working branch that aren't associated with the pull request!

Cleanup

  • Once the pull request has been reviewed and either merged or rejected, you can delete the branch.
From your local computer
git branch -D ${WORKING_BRANCH}
# Example:
git branch -D bug-99999	# delete local branch bug-99999
From your personal remote github mirror
git push $REMOTE :${WORKING_BRANCH}  # note space before :working-branch
# Example:
git push origin :bug-99999

If you no longer require your local repository, you can just delete the folder on your PC and it's sub-folders.

Prepare for your next working branch

Follow the instructions in #Sync your local stable or future branch from upstream (Pull Requests), then #Create a working-branch (Pull Requests).
  1. Before version 5.0 stable was named maint and future master. Discussionand Announcement