Source code management

The main source code management tool for the FreeCAD project is Git, which can be easily installed in most operating systems from a package manager or directly from Git's website. You are advised to become familiar with Git before working with the FreeCAD source code directly. Visit the Git documentation page for the reference manual, as well as the Pro Git book to learn to use the system in a general way. The present article focuses on the use of Git together with FreeCAD.

While Git is primarily a terminal application, there are many graphical clients for it which facilitate working with branches, applying patches, and submitting pull requests to a master branch. Examples include git-cola, gitg, and GitKraken. Please see Developing FreeCAD with GitKraken for a cursory introduction to this tool.

Source code access
Everybody can access and get a copy of the FreeCAD source code, but only the FreeCAD project managers have write access to it. You can get a copy of the code, study it and modify it as you wish. If you want your changes to be included in the official source code, you need to perform a pull request against the master repository so that your modifications can be reviewed by one of the managers.

If your source code changes are significant, you are advised to explain them in the pull request section of the FreeCAD forum.

Official GitHub repository
The FreeCAD source code is hosted in Github, https://github.com/FreeCAD/FreeCAD

In order to contribute code, you need to have a GitHub account.

Setting your git username
Developers should commit code to their personal repository using their GitHub username. If that is not already set globally, you can set it locally for the current Git repository like this:

Where represents your full name or nickname, used to identify the author of a particular commit, and  indicates your GitHub username.

You can now use some combination of and  to create one or more commits in your local repository.

Remote repositories
Please read What is the difference between origin and upstream on GitHub? (Stackoverflow) to help you understand the difference between and  in the context of Git. This section explains how to set the correct repositories for development. Essentially:
 * is your personal fork of the official FreeCAD repository, that is,
 * is the official FreeCAD repository, that is, https://github.com/FreeCAD/FreeCAD

This distinction is important, as you should write code in your own copy of the repository first, before pushing those changes to the official repository.

Based on the above, there are two ways to setup your Git development environment:
 * 1st Method: fork on GitHub and clone your fork locally
 * 2nd Method: clone FreeCAD directly to your local machine, and adjust the remote servers

We recommend the 1st method because it's one step faster.

1st Method: Fork on GitHub and clone your fork locally
First you will fork the FreeCAD repository in GitHub, then clone this personal fork to your computer, and finally set the repository.
 * Log in to your GitHub account.
 * Go to the official FreeCAD repository: https://github.com/FreeCAD/FreeCAD
 * In the top right of the page press the "Fork" button. This will create a personal copy of the FreeCAD repository under your GitHub username:
 * On your machine, clone your newly created FreeCAD fork. It will be created inside a directory.


 * Once the download is complete, enter the new source directory and set the repository.


 * Confirm your remote repositories with ; the output should be similar to this


 * Now development can begin.

2nd Method: Clone FreeCAD directly to your local machine
First you will fork the FreeCAD repository in GitHub, however, you will clone the original FreeCAD repository to your local machine, and then alter your remotes via the terminal.
 * Log in to your GitHub account.
 * Go to the official FreeCAD repository: https://github.com/FreeCAD/FreeCAD
 * In the top right of the page press the "Fork" button. This will create a personal copy of the FreeCAD repository under your GitHub username:
 * Clone the original FreeCAD repository. It will be created inside a directory.


 * Once the download is complete, enter the new source directory and set the repository.


 * Then set up the repository.


 * Confirm your remote repositories with ; the output should be similar to this


 * Now development can begin.

If for some reason the remote repositories exist but point to the wrong address, you can remedy the situation by renaming the remote repository's name. For example, should point to your personal fork; if it is pointing to the original FreeCAD repository, change the name of this remote to, and manually add the  repository.

Branching
Instead of working on the master version of the code, best practices with Git recommend creating a new branch whenever you want to work on a new feature. Using a new branch is done in two steps, first your create the branch, and then you switch to it:

Alternatively, use a single instruction:

To see the branches in your project and the current branch, use the operation alone, or add  or  for more information:

After you've made changes and committed those changes use the operation with the following options to visualize the branches

Committing
Once you are inside a new branch, edit the source files that you want with a text editor. To see which files were modified use the and  operations; when you are satisfied with the modifications, save the changes with the  operation:

Unlike SVN, you need to specifically tell which files to commit; use the option to save changes in all files that were altered. Your text editor, like or, will open to allow you to write a commit message.

Alternatively add the message in the commit itself

Writing good commit messages
You should try to work in small steps, that is, commit often, after a small addition in your code. If you cannot summarize your changes in one sentence, then it has probably been too long since you made a commit.

For big changes, it is important that you have helpful and useful descriptions of your work. FreeCAD has adopted a format mentioned in the Pro Git book.

If you are doing a lot of related work in a branch, you should make many small commits (see a forum post). When you want to merge those changes into the master branch, you should issue

to see the individual commit messages. Then you can write a high quality message when performing a merge.

When you merge to master use the option and commit with your quality commit message. This will allow you to be very liberal with your commits and help to provide a good level of detail in commit messages without so many distinct descriptions.

Pushing your work to your GitHub repository
The local branches in your computer aren't automatically synchronized with the remote servers that you have specified as or  (see Remote repositories); you have to explicitly push the branches to the remote servers, for which you must have write access. Once you do this, the branches become public, and available for review by other developers.

For FreeCAD, you should push your local branch to the remote repository, that is,. You need to enter your username and password every time you push, unless you have set up Credential caching. Please read Pushing commits to a remote repository for more information.

Rebasing from upstream
While you work on your own branch, the official FreeCAD code keeps "moving forward" with commits from other developers, and thus starts diverging from the code that you have in your personal fork. A- myNewBranch | -o---Z FreeCAD master

Therefore, when you are ready to merge your branch to the main FreeCAD repository, you must "rebase" your own copy of the repository, so that it is as close as possible to the official repository. See Git Branching - Rebasing for more information.

This will download the code from the branch of the  repository (the official FreeCAD source), and will merge it with your current branch, that is, your changes will be placed on top of the latest official code. If nobody modified the same files that you did, then the merge will succeed without problems. If some files were changed at the same time by different people, there may be a conflict that needs to be resolved. A' myNewBranch | -o---Z FreeCAD master

To summarize, you need to be in the appropriate branch, rebase the upstream code, and then proceed with the push.

The operation is equivalent to a  followed by a. When the option is used, instead of doing a simple, it runs the  operation.

Merging the branch (pull request)
Once you have committed your changes locally, rebased your branch from the upstream repository, and pushed your branch online, you can initiate a "pull request". A pull request tells the administrators of the official FreeCAD repository that you want to merge the new code in your branch with the official code.

As soon as your push is successful, in your repository, GitHub will display a message telling you that a branch was recently pushed, and it will give you the option of comparing and creating the pull request. By pressing you will find an interface telling you which repository is the "base", target of the merge, and which is the "head", your added branch. A quick check will be done by the system telling you if there are no conflicts with the files that you modified; if you worked on files that nobody has touched, your branch will be able to merge cleanly. In addition, it will show you a text editor so you can write a message documenting your changes; it will also display the number of commits in your branch, the number of files that were modified, and a view showing you the differences between the "base" (official) and the "head" repositories (your own branch) so that everybody can immediately see your intended modifications.

Click to proceed. A message will appear indicating that some checks need to be done on the code. This is a system that compiles FreeCAD automatically and runs the unit tests. If the tests pass, the pull request will have a better chance of being merged into the main code, otherwise a report will be made indicating the errors encountered. See FreeCAD pull requests. Required

The pull request interface can be used whenever you want to submit code from your own repositories to another repository in GitHub. You can even use it to merge code in the opposite direction, from other people's branches to your own, or between your own branches. In the latter case, since you own the branches, the merges can be approved immediately by yourself.

Check out GitHub requests locally
Checkout GitHub pull requests locally

Resolving merge conflicts

 * How merge conflicts are presented
 * Resolving a merge conflict using the command line
 * External merge and diff tools to use when you encounter a Git conflict.

Creating patches from git
Although Git allows you to merge different branches of code quickly, there are times when it may be desirable to create a patch instead of merging or performing a pull request. The following workflow explains how to do this.


 * You should be developing your new code in a secondary branch of your repository, and not in the master branch. So the first step is to make sure you are in the correct branch.


 * Now use against the master branch, and use the  option to redirect the result to standard output; then redirect the standard output to a file, which for convenience is created above the source code directory.


 * Another method is

The number of circumflex carets or the number  indicate the number of commits that should be considered, that is,  or  will create three patches for three commits.

This will create a patch or series of patches with the following naming convention

where is a number from  to, and the commit message forms the majority of the file name, for example,

Applying patches via git
Git can merge patches or diffs. To know more about this process read Applying patches with Git.

If you already have the patch file in your system, just apply it.

You can use to download a patch from a website, and then apply it through.

Add or  at the end of the URL of a GitHub commit, pull request, or compare view so that the website shows you the plain text view of that page.
 * Regular commit page: https://github.com/FreeCAD/FreeCAD/commit/c476589652a0f67b544735740e20ff702e8d0621
 * Diff page: https://github.com/FreeCAD/FreeCAD/commit/c476589652a0f67b544735740e20ff702e8d0621.diff
 * Patch page: https://github.com/FreeCAD/FreeCAD/commit/c476589652a0f67b544735740e20ff702e8d0621.patch

You can point to a particular commit patch in the repository, and pipe it directly to  to apply the patch. curl https://github.com/FreeCAD/FreeCAD/commit/c476589652a0f67b544735740e20ff702e8d0621.patch | git apply -

Reversing a patch in git
When you apply a patch you modify some files. However, these modifications aren't permanent until you commit the changes. Therefore, if you want to revert a patch use the following instructions.

This will revert the changes applied, if you still have access to the original patch file.

Alternatively, this will remove non-committed changes to the branch.

Stashing git commits
Say that you're working on a branch and you find yourself making some modifications to the source that are out of the scope of your current branch; in other words, those changes would be better in another branch instead of the current one. The command can be used to temporarily store those uncommitted local changes.

If in the future you want to use those commits, you can apply the stash.

Or if you decide that you don't like the commits anymore, you may delete the stash.

You can list multiple stashes with

To learn more, read Useful tricks you might not know about Git stash.

FreeCAD revision number
In contrast to subversion, which uses a consecutive number for its revisions, Git produces SHA-1 hash values with every commit. A hash value is a long alphanumeric string that looks like this

Latest revision number
To find the latest revision number of a particular branch use the operation with the  option. Give the name of the branch, remote repository, tag, or a special pointer like, to indicate the last commit in that particular object.

Or browse the repository on GitHub, and read the amount of commits reported in the particular branch.

Revision number of a specific commit hash
Since the hash is an alphanumeric string it is not very useful to decide if a certain commit is older or newer than another hash. To find the revision number of a particular hash, again use the operation; the input can be the full hash, or a partial hash that is unique, usually the first 7 digits are enough.

Revision hash of a specific commit number
If we have the commit number, say, 15000, and we want to find the corresponding hash, we need to calculate the number of commits since this point until the last commit. First, get the latest commit number.

Then subtract the commit that we want.

Then use the operation to show all commits and hashes. The option jumps the difference in commits that we calculated so that we go directly to the hash that we are looking for.

Confirm it's the right commit number.


 * Show the commits starting from a particular commit in GitHub: in the address bar of the browser, change the word to  to show a list.
 * Finding the revision number of the commit
 * Finding the revision number of the commit
 * Finding the corresponding hash value to a particular commit number

Revision number in FreeCAD's interface
The version number that appears in is defined in, which is created at compile time when the  tool is run. Read Extract version number from git source for more information.

See the compile on Unix page for the full information on compiling FreeCAD.

Alternative repositories
The beauty of git is that everybody can clone a project, and start modifying the code. Several frequent collaborators of the FreeCAD project have their own git repository, where they build up their work before it is ready to be included in the official source code, or simply where they experiment new ideas. In certain cases, you might want to clone your FreeCAD code from one of these, instead of the official repos, to benefit from the changes their users did.

Be warned, though, that this is at your own risk, and only the official repository above is fully guaranteed to work and contain clean code.

It is also possible to attach several remote repositories to a same local FreeCAD git code, using the "git remote" command. This is useful to keep in sync with the master code branch, but keep an eye on the work of different developers.