Source code management: Difference between revisions

From LabRPS Documentation
Jump to navigation Jump to search
(Created page with "{{TOCright}} == Introduction == The main source code management tool for the LabRPS project is [http://en.wikipedia.org/wiki/Git_%28software%29 Git], which can be easily installed in most operating systems from a package manager or directly from [https://git-scm.com/ Git's website]. You are advised to become familiar with Git before working with the LabRPS source code directly. Visit the [https://git-scm.com/doc Git documentation] page for the reference manual, as wel...")
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 5: Line 5:
The main source code management tool for the LabRPS project is [http://en.wikipedia.org/wiki/Git_%28software%29 Git], which can be easily installed in most operating systems from a package manager or directly from [https://git-scm.com/ Git's website]. You are advised to become familiar with Git before working with the LabRPS source code directly. Visit the [https://git-scm.com/doc Git documentation] page for the reference manual, as well as the [https://git-scm.com/book/en/v2 Pro Git book] to learn to use the system in a general way. The present document focuses on the use of Git for LabRPS development. Compiling LabRPS is described in [[Compiling|Compiling]].
The main source code management tool for the LabRPS project is [http://en.wikipedia.org/wiki/Git_%28software%29 Git], which can be easily installed in most operating systems from a package manager or directly from [https://git-scm.com/ Git's website]. You are advised to become familiar with Git before working with the LabRPS source code directly. Visit the [https://git-scm.com/doc Git documentation] page for the reference manual, as well as the [https://git-scm.com/book/en/v2 Pro Git book] to learn to use the system in a general way. The present document focuses on the use of Git for LabRPS development. Compiling LabRPS is described in [[Compiling|Compiling]].


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 [https://git-scm.com/docs/gitk gitk] (the first graphical interface developed),  [https://wiki.gnome.org/Apps/Gitg/ gitg] (Gnome), [https://github.com/tibirna/qgit qgit] (Qt), [https://jonas.github.io/tig/ tig] (Ncurses), [http://github.com/git-cola/git-cola git-cola], and [https://www.gitkraken.com/ GitKraken] (proprietary). Please see [[Developing LabRPS with GitKraken|Developing LabRPS with GitKraken]] for a cursory introduction to this tool.
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 [https://git-scm.com/docs/gitk gitk] (the first graphical interface developed),  [https://wiki.gnome.org/Apps/Gitg/ gitg] (Gnome), [https://github.com/tibirna/qgit qgit] (Qt), and [https://jonas.github.io/tig/ tig] (Ncurses), [http://github.com/git-cola/git-cola git-cola].


Note: if any of this is starting to make you dizzy, there is a very good non-technical series on how to use git and Github called '[https://youtu.be/BCQHnlnPusY Git and Github for Poets]'
Note: if any of this is starting to make you dizzy, there is a very good non-technical series on how to use git and Github called '[https://youtu.be/BCQHnlnPusY Git and Github for Poets]'
Line 32: Line 32:
git config user.email GITHUB_USERNAME@users.noreply.github.com
git config user.email GITHUB_USERNAME@users.noreply.github.com
}}
}}


Where {{incode|"YOUR_NAME"}} represents your full name or nickname, used to identify the author of a particular commit, and {{incode|GITHUB_USERNAME}} indicates the name of your account on GitHub.
Where {{incode|"YOUR_NAME"}} represents your full name or nickname, used to identify the author of a particular commit, and {{incode|GITHUB_USERNAME}} indicates the name of your account on GitHub.
Line 50: Line 49:


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


<div class="mw-collapsible mw-collapsed toccolours">
<div class="mw-collapsible mw-collapsed toccolours">


==== 1st Method: Fork on GitHub and clone your fork locally ====  
==== 1st Method: Fork on GitHub and clone your fork locally ====  


<div class="mw-collapsible-content">
<div class="mw-collapsible-content">


First you will fork the LabRPS repository in GitHub, then clone this personal fork to your computer, and finally set the {{incode|upstream}} repository.
First you will fork the LabRPS repository in GitHub, then clone this personal fork to your computer, and finally set the {{incode|upstream}} repository.
Line 66: Line 61:
* In the top right of the page press the "Fork" button. This will create a personal copy of the LabRPS repository under your GitHub username: {{URLn|https://github.com/GITHUB_USERNAME/LabRPS}}
* In the top right of the page press the "Fork" button. This will create a personal copy of the LabRPS repository under your GitHub username: {{URLn|https://github.com/GITHUB_USERNAME/LabRPS}}
* On your machine, clone your newly created LabRPS fork. It will be created inside a directory {{incode|labrps-source}}.
* On your machine, clone your newly created LabRPS fork. It will be created inside a directory {{incode|labrps-source}}.


{{Code|lang=text|code=
{{Code|lang=text|code=
git clone https://github.com/GITHUB_USERNAME/LabRPS.git labrps-source
git clone https://github.com/GITHUB_USERNAME/LabRPS.git labrps-source
}}
}}


* Once the download is complete, enter the new source directory and set the {{incode|upstream}} repository.
* Once the download is complete, enter the new source directory and set the {{incode|upstream}} repository.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 80: Line 72:
git remote add upstream https://github.com/LabRPS/LabRPS.git
git remote add upstream https://github.com/LabRPS/LabRPS.git
}}
}}


* Confirm your remote repositories with {{incode|git remote -v}}; the output should be similar to this
* Confirm your remote repositories with {{incode|git remote -v}}; the output should be similar to this


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 91: Line 81:
upstream https://github.com/LabRPS/LabRPS.git (push)
upstream https://github.com/LabRPS/LabRPS.git (push)
}}
}}


* Now development can begin.
* Now development can begin.


</div>
</div>
Line 100: Line 88:


<div class="mw-collapsible mw-collapsed toccolours">
<div class="mw-collapsible mw-collapsed toccolours">


==== 2nd Method: Clone LabRPS directly to your local machine  ====  
==== 2nd Method: Clone LabRPS directly to your local machine  ====  


<div class="mw-collapsible-content">
<div class="mw-collapsible-content">


First you will fork the LabRPS repository in GitHub, however, you will clone the original LabRPS repository to your local machine, and then alter your remotes via the terminal.
First you will fork the LabRPS repository in GitHub, however, you will clone the original LabRPS repository to your local machine, and then alter your remotes via the terminal.
Line 113: Line 98:
* In the top right of the page press the "Fork" button. This will create a personal copy of the LabRPS repository under your GitHub username: {{URLn|https://github.com/GITHUB_USERNAME/LabRPS}}
* In the top right of the page press the "Fork" button. This will create a personal copy of the LabRPS repository under your GitHub username: {{URLn|https://github.com/GITHUB_USERNAME/LabRPS}}
* Clone the original LabRPS repository. It will be created inside a directory {{incode|labrps-source}}.
* Clone the original LabRPS repository. It will be created inside a directory {{incode|labrps-source}}.


{{Code|lang=text|code=
{{Code|lang=text|code=
git clone https://github.com/LabRPS/LabRPS.git labrps-source
git clone https://github.com/LabRPS/LabRPS.git labrps-source
}}
}}


* Once the download is complete, enter the new source directory and set the {{incode|origin}} repository.
* Once the download is complete, enter the new source directory and set the {{incode|origin}} repository.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 127: Line 109:
git remote add origin https://github.com/GITHUB_USERNAME/LabRPS.git
git remote add origin https://github.com/GITHUB_USERNAME/LabRPS.git
}}
}}


* Then set up the {{incode|upstream}} repository.
* Then set up the {{incode|upstream}} repository.


{{Code|lang=text|code=
{{Code|lang=text|code=
git remote add upstream https://github.com/LabRPS/LabRPS.git
git remote add upstream https://github.com/LabRPS/LabRPS.git
}}
}}


* Confirm your remote repositories with {{incode|git remote -v}}; the output should be similar to this
* Confirm your remote repositories with {{incode|git remote -v}}; the output should be similar to this


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 146: Line 124:
upstream https://github.com/LabRPS/LabRPS.git (push)
upstream https://github.com/LabRPS/LabRPS.git (push)
}}
}}


* Now development can begin.
* Now development can begin.


</div>
</div>
</div>
</div>


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, {{incode|origin}} should point to your personal fork; if it is pointing to the original LabRPS repository, change the name of this remote to {{incode|upstream}}, and manually add the {{incode|origin}} repository.
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, {{incode|origin}} should point to your personal fork; if it is pointing to the original LabRPS repository, change the name of this remote to {{incode|upstream}}, and manually add the {{incode|origin}} repository.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 163: Line 137:
git remote -v
git remote -v
}}
}}


You can also show more information with the {{incode|show}} keyword.
You can also show more information with the {{incode|show}} keyword.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 172: Line 144:
git remote show upstream
git remote show upstream
}}
}}


== Git development process ==  
== Git development process ==  
Line 186: Line 157:


Using a new branch is done in two steps, first your create the branch, and then you switch to it:
Using a new branch is done in two steps, first your create the branch, and then you switch to it:


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 192: Line 162:
git checkout myNewBranch
git checkout myNewBranch
}}
}}


Alternatively, perform both steps with a single instruction:
Alternatively, perform both steps with a single instruction:


{{Code|lang=text|code=
{{Code|lang=text|code=
git checkout -b myNewBranch
git checkout -b myNewBranch
}}
}}


Now you can change branches with {{incode|checkout}} whenever you need to work on them. To see the branches in your project and the current branch, use the {{incode|branch}} operation alone, or add {{incode|-v}} or {{incode|-vv}} for more information:
Now you can change branches with {{incode|checkout}} whenever you need to work on them. To see the branches in your project and the current branch, use the {{incode|branch}} operation alone, or add {{incode|-v}} or {{incode|-vv}} for more information:


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 209: Line 175:
git branch -vv
git branch -vv
}}
}}


After you've made changes and committed those changes use the {{incode|log}} operation with the following options to visualize the branches
After you've made changes and committed those changes use the {{incode|log}} operation with the following options to visualize the branches


{{Code|lang=text|code=
{{Code|lang=text|code=
git log --oneline --decorate --graph --all
git log --oneline --decorate --graph --all
}}
}}


=== Committing ===  
=== 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 {{incode|status}} and {{incode|diff}} operations; when you are satisfied with the modifications, save the changes with the {{incode|commit}} operation:
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 {{incode|status}} and {{incode|diff}} operations; when you are satisfied with the modifications, save the changes with the {{incode|commit}} operation:


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 229: Line 191:
git commit -a
git commit -a
}}
}}


Unlike SVN, you need to specifically tell which files to commit; use the {{incode|-a}} option to save changes in all files that were altered. Your text editor, for example, {{incode|nano}} or {{incode|vim}}, will open to allow you to write a commit message.
Unlike SVN, you need to specifically tell which files to commit; use the {{incode|-a}} option to save changes in all files that were altered. Your text editor, for example, {{incode|nano}} or {{incode|vim}}, will open to allow you to write a commit message.


Alternatively add the message in the commit itself:
Alternatively add the message in the commit itself:


{{Code|lang=text|code=
{{Code|lang=text|code=
git commit -a -m "Fix the bug in the clone function."
git commit -a -m "Fix the bug in the clone function."
}}
}}


If you create new files or directories, you must use the {{incode|add}} operation first to add them to the local repository before committing the changes.
If you create new files or directories, you must use the {{incode|add}} operation first to add them to the local repository before committing the changes.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 248: Line 206:
git commit -a
git commit -a
}}
}}


Where {{incode|path}} can be any directory or file.
Where {{incode|path}} can be any directory or file.
Line 277: Line 234:


If you are doing a lot of related work in a branch, you should make many small commits (see a [https://labrps.com/boards?f=10&t=2062&p=14887#p14886 forum post]). When you want to merge those changes into the master branch, you should issue
If you are doing a lot of related work in a branch, you should make many small commits (see a [https://labrps.com/boards?f=10&t=2062&p=14887#p14886 forum post]). When you want to merge those changes into the master branch, you should issue


{{Code|lang=text|code=
{{Code|lang=text|code=
git log master..myNewBranch
git log master..myNewBranch
}}
}}


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


With {{incode|git log --oneline}} you can see many commits in sequence, with the newest commit on top. In this example, starting from "feature A" many commits are made to implement "feature B"; we would like to squash all commits belonging to "feature B" into one.
With {{incode|git log --oneline}} you can see many commits in sequence, with the newest commit on top. In this example, starting from "feature A" many commits are made to implement "feature B"; we would like to squash all commits belonging to "feature B" into one.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 305: Line 259:
6394da Feature A
6394da Feature A
}}
}}


Use the {{incode|rebase}} operation with the {{incode|--interactive}} or {{incode|-i}} option to select various commits and squash them. Use the hash of the commit just before the first one that you want to squash, in this case the one corresponding to "feature A".
Use the {{incode|rebase}} operation with the {{incode|--interactive}} or {{incode|-i}} option to select various commits and squash them. Use the hash of the commit just before the first one that you want to squash, in this case the one corresponding to "feature A".


{{Code|lang=text|code=
{{Code|lang=text|code=
git rebase -i 6394da
git rebase -i 6394da
}}
}}


(TIP: If you know how many commits you want to edit, you can use {{incode|git rebase -i HEAD~n}} to work on the last {{incode|n}} commits)
(TIP: If you know how many commits you want to edit, you can use {{incode|git rebase -i HEAD~n}} to work on the last {{incode|n}} commits)


The command line editor, like {{incode|nano}} or {{incode|vim}}, will open to show you the commits again, now with the older commit on top. Before each commit, the word {{incode|pick}} will be shown. Delete the word {{incode|pick}}, and write the word {{incode|squash}} or just the letter {{incode|s}} instead, with the exception of the first entry; this commit is the oldest one, so all future commits will be squashed into it.
The command line editor, like {{incode|nano}} or {{incode|vim}}, will open to show you the commits again, now with the older commit on top. Before each commit, the word {{incode|pick}} will be shown. Delete the word {{incode|pick}}, and write the word {{incode|squash}} or just the letter {{incode|s}} instead, with the exception of the first entry; this commit is the oldest one, so all future commits will be squashed into it.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 329: Line 279:
s 871adb OK, feature B is fully implemented
s 871adb OK, feature B is fully implemented
}}
}}


Save the file and close the editor.
Save the file and close the editor.
Line 336: Line 285:


You can use {{incode|git log --oneline}} again to observe the new commit history. In this case only a single commit for "feature B" will appear, on top of the unmodified commit for "feature A".
You can use {{incode|git log --oneline}} again to observe the new commit history. In this case only a single commit for "feature B" will appear, on top of the unmodified commit for "feature A".


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 342: Line 290:
6394da Feature A
6394da Feature A
}}
}}


When coding for LabRPS, we ask that you begin each commit message with the module that it affects. For example, a commit message for a change to sketcher might be:
When coding for LabRPS, we ask that you begin each commit message with the module that it affects. For example, a commit message for a change to sketcher might be:
Line 361: Line 308:


For LabRPS, you should push your local branch to the {{incode|origin}} remote repository, that is, {{URLn|https://github.com/GITHUB_USERNAME/LabRPS}}. You need to enter your username and password every time you push, unless you have set up [https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage#_credential_caching Credential caching]. Please read [https://help.github.com/articles/pushing-to-a-remote/ Pushing commits to a remote repository] for more information.
For LabRPS, you should push your local branch to the {{incode|origin}} remote repository, that is, {{URLn|https://github.com/GITHUB_USERNAME/LabRPS}}. You need to enter your username and password every time you push, unless you have set up [https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage#_credential_caching Credential caching]. Please read [https://help.github.com/articles/pushing-to-a-remote/ Pushing commits to a remote repository] for more information.


{{Code|lang=text|code=
{{Code|lang=text|code=
git push origin myNewBranch
git push origin myNewBranch
}}
}}


When you work with a single branch, you may need to interactively rebase, squash, and fix commits many times. In this case, your branch history will not be simple, and you will not be able to push it to the remote repository. You may get a message like the following, saying that it is not possible to do a "fast-forward" push.
When you work with a single branch, you may need to interactively rebase, squash, and fix commits many times. In this case, your branch history will not be simple, and you will not be able to push it to the remote repository. You may get a message like the following, saying that it is not possible to do a "fast-forward" push.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 378: Line 322:
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
}}
}}


In order to finally push your branch to the remote repository you need to "force push" it. This will completely overwrite your remote branch with the actual branch that you have offline.
In order to finally push your branch to the remote repository you need to "force push" it. This will completely overwrite your remote branch with the actual branch that you have offline.


{{Code|lang=text|code=
{{Code|lang=text|code=
git push -f origin myNewBranch
git push -f origin myNewBranch
}}
}}


The regular developer doesn't have write access to the {{incode|upstream}} repository {{URL|https://github.com/LabRPS/LabRPS}}, therefore, you should never push code to this remote server.
The regular developer doesn't have write access to the {{incode|upstream}} repository {{URL|https://github.com/LabRPS/LabRPS}}, therefore, you should never push code to this remote server.
Line 393: Line 334:


While you work on your own branch, the official LabRPS code keeps "moving forward" with commits from other developers, and thus starts diverging from the code that you have in your personal fork.
While you work on your own branch, the official LabRPS code keeps "moving forward" with commits from other developers, and thus starts diverging from the code that you have in your personal fork.


<!--NOT CHANGE THE <pre> </pre> BALISE CAUSE THE SPACE IN MACRO CODE IS NOT RESPECTED-->
<!--NOT CHANGE THE <pre> </pre> BALISE CAUSE THE SPACE IN MACRO CODE IS NOT RESPECTED-->
Line 404: Line 344:


Therefore, when you are ready to merge your branch to the main LabRPS repository, you must "rebase" your own copy of the repository, so that it is as close as possible to the official repository. See [https://git-scm.com/book/en/v2/Git-Branching-Rebasing Git Branching - Rebasing] for more information.
Therefore, when you are ready to merge your branch to the main LabRPS repository, you must "rebase" your own copy of the repository, so that it is as close as possible to the official repository. See [https://git-scm.com/book/en/v2/Git-Branching-Rebasing Git Branching - Rebasing] for more information.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 410: Line 349:
git pull --rebase upstream master
git pull --rebase upstream master
}}
}}


This will download the code from the {{incode|master}} branch of the {{incode|upstream}} repository (the official LabRPS source), and will merge it with your current branch ({{incode|myNewBranch}}), so that your changes will appear 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.
This will download the code from the {{incode|master}} branch of the {{incode|upstream}} repository (the official LabRPS source), and will merge it with your current branch ({{incode|myNewBranch}}), so that your changes will appear 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.


<!--NOT CHANGE THE <pre> </pre> BALISE CAUSE THE SPACE IN MACRO CODE IS NOT RESPECTED-->
<!--NOT CHANGE THE <pre> </pre> BALISE CAUSE THE SPACE IN MACRO CODE IS NOT RESPECTED-->
Line 421: Line 358:
-----o-----------Z LabRPS upstream/master
-----o-----------Z LabRPS upstream/master
</pre>
</pre>


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


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 431: Line 366:
git push origin myNewBranch
git push origin myNewBranch
}}
}}


The {{incode|pull}} operation is equivalent to a {{incode|fetch}} followed by a {{incode|merge}}. When the {{incode|--rebase}} option is used, instead of doing a simple {{incode|merge}}, it runs the {{incode|rebase}} operation.
The {{incode|pull}} operation is equivalent to a {{incode|fetch}} followed by a {{incode|merge}}. When the {{incode|--rebase}} option is used, instead of doing a simple {{incode|merge}}, it runs the {{incode|rebase}} operation.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 449: Line 382:
git rebase master
git rebase master
}}
}}


=== Merging the branch (pull request) ===  
=== Merging the branch (pull request) ===  
Line 465: Line 397:


GitHub will show you a text editor so you can write a message documenting your changes: this editor will be pre-filled with a welcome message (that you can delete), a checklist (that you should go through), and a reminder to document your change on the wiki when it's accepted. To use the checklist, go through each item in turn and change the {{incode|[ ]}} to {{incode|[X]}} to indicate that you've done that step. GitHub 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" and the "head" so that everybody can immediately see your intended modifications. Double-check these for things like stray blank lines you didn't mean to add, or huge formatting changes that your IDE decided to make behind your back.
GitHub will show you a text editor so you can write a message documenting your changes: this editor will be pre-filled with a welcome message (that you can delete), a checklist (that you should go through), and a reminder to document your change on the wiki when it's accepted. To use the checklist, go through each item in turn and change the {{incode|[ ]}} to {{incode|[X]}} to indicate that you've done that step. GitHub 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" and the "head" so that everybody can immediately see your intended modifications. Double-check these for things like stray blank lines you didn't mean to add, or huge formatting changes that your IDE decided to make behind your back.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 472: Line 403:
Able to merge. These branches can be automatically merged.
Able to merge. These branches can be automatically merged.
}}
}}


Click {{Button|Create pull request}} to proceed. A message will appear indicating that some checks need to be done on the code. This is a system that compiles LabRPS 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 [https://travis-ci.org/LabRPS/LabRPS/pull_requests LabRPS pull requests].
Click {{Button|Create pull request}} to proceed. A message will appear indicating that some checks need to be done on the code. This is a system that compiles LabRPS 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 [https://travis-ci.org/LabRPS/LabRPS/pull_requests LabRPS pull requests].


<!--NOT CHANGE THE <pre> </pre> BALISE CAUSE THE PIPE OR SPACE IN MACRO CODE CUT THE MACRO-->
<!--NOT CHANGE THE <pre> </pre> BALISE CAUSE THE PIPE OR SPACE IN MACRO CODE CUT THE MACRO-->
Line 483: Line 412:
* continuous-integration/travis-ci/pr Pending — The Travis CI build is in progress  |Required|
* continuous-integration/travis-ci/pr Pending — The Travis CI build is in progress  |Required|
</pre>
</pre>


If the tests succeed, you will see a message such as the following
If the tests succeed, you will see a message such as the following


All checks have passed
All checks have passed


<!--NOT CHANGE THE <pre> </pre> BALISE CAUSE THE PIPE OR SPACE IN MACRO CODE CUT THE MACRO-->
<!--NOT CHANGE THE <pre> </pre> BALISE CAUSE THE PIPE OR SPACE IN MACRO CODE CUT THE MACRO-->
Line 494: Line 421:
* continuous-integration/travis-ci/pr — The Travis CI build passed  |Required|
* continuous-integration/travis-ci/pr — The Travis CI build passed  |Required|
</pre>
</pre>


This branch has no conflicts with the base branch
This branch has no conflicts with the base branch
Line 500: Line 426:


Now you must wait for the administrators to merge your branch; you will be notified when this happens.
Now you must wait for the administrators to merge your branch; you will be notified when this happens.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 508: Line 433:
If you wish, you can also delete your fork of LabRPS/LabRPS.
If you wish, you can also delete your fork of LabRPS/LabRPS.
}}
}}


If you wish, you may delete the branch that was just merged, or even your entire LabRPS fork, as your own code is already included at the end of the master branch.
If you wish, you may delete the branch that was just merged, or even your entire LabRPS fork, as your own code is already included at the end of the master branch.


{{Code|lang=text|code=
{{Code|lang=text|code=
-----o-----------Z----A' LabRPS upstream/master
-----o-----------Z----A' LabRPS upstream/master
}}
}}


{{Emphasis|Note:}} you may continue working ({{incode|git commit -a}}) on the same branch while you wait for merge approval; if you {{incode|git push}} again, a second merge commit will be queued in the same pull request, and another automated test will be done. That is, while your merges aren't yet approved by the administrators, you may keep pushing changes to your {{incode|origin}} repository, and this will queue those commits in the same pull request to the {{incode|upstream}} repository. Using a single pull request to queue many individual commits is often desirable for small changes. For big additions to the source code, you should create another branch, develop your features there, and then submit a separate pull request for this branch.
{{Emphasis|Note:}} you may continue working ({{incode|git commit -a}}) on the same branch while you wait for merge approval; if you {{incode|git push}} again, a second merge commit will be queued in the same pull request, and another automated test will be done. That is, while your merges aren't yet approved by the administrators, you may keep pushing changes to your {{incode|origin}} repository, and this will queue those commits in the same pull request to the {{incode|upstream}} repository. Using a single pull request to queue many individual commits is often desirable for small changes. For big additions to the source code, you should create another branch, develop your features there, and then submit a separate pull request for this branch.


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


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 528: Line 449:
base repository: GITHUB_USERNAME/LabRPS    base: myNewBranch  <----  head repository: GITHUB_USERNAME/LabRPS        compare: fix-many-bugs-branch
base repository: GITHUB_USERNAME/LabRPS    base: myNewBranch  <----  head repository: GITHUB_USERNAME/LabRPS        compare: fix-many-bugs-branch
}}
}}


=== Keeping the GitHub repository up to date ===  
=== Keeping the GitHub repository up to date ===  


Once you've forked LabRPS, your personal repository exists independently from the original. When the original repository has new commits, GitHub will inform you that your personal repository is behind in number of commits:
Once you've forked LabRPS, your personal repository exists independently from the original. When the original repository has new commits, GitHub will inform you that your personal repository is behind in number of commits:


{{Code|lang=text|code=
{{Code|lang=text|code=
This branch is 5 commits behind LabRPS:master.
This branch is 5 commits behind LabRPS:master.
}}
}}


In similar way, if you created a development branch with new code, GitHub will inform you that this branch is ahead in number of commits; that is, this branch has changes that haven't been merged into the official LabRPS repository:
In similar way, if you created a development branch with new code, GitHub will inform you that this branch is ahead in number of commits; that is, this branch has changes that haven't been merged into the official LabRPS repository:


{{Code|lang=text|code=
{{Code|lang=text|code=
This branch is 3 commits ahead of LabRPS:master.
This branch is 3 commits ahead of LabRPS:master.
}}
}}


While developing, both cases are possible, as your own branch may lack commits made by other developers, but include new commits by you:
While developing, both cases are possible, as your own branch may lack commits made by other developers, but include new commits by you:


{{Code|lang=text|code=
{{Code|lang=text|code=
This branch is 2 commits ahead, 14 commits behind LabRPS:master.  
This branch is 2 commits ahead, 14 commits behind LabRPS:master.  
}}
}}


When developing code it is recommended that you rebase the branch in which you are currently working, as that will put your branch always ahead of the LabRPS master code.
When developing code it is recommended that you rebase the branch in which you are currently working, as that will put your branch always ahead of the LabRPS master code.


As for your original {{incode|master}} branch, it will never be automatically updated by GitHub; this is something that you must do yourself. Switch to the {{incode|master}} branch, then {{incode|pull}} from {{incode|upstream}} (which performs a {{incode|fetch}} and {{incode|merge}}), and then push this updated {{incode|master}} branch to your remote {{incode|origin}} repository.
As for your original {{incode|master}} branch, it will never be automatically updated by GitHub; this is something that you must do yourself. Switch to the {{incode|master}} branch, then {{incode|pull}} from {{incode|upstream}} (which performs a {{incode|fetch}} and {{incode|merge}}), and then push this updated {{incode|master}} branch to your remote {{incode|origin}} repository.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 566: Line 479:
git push origin master
git push origin master
}}
}}


After this is done, GitHub will let you know that your are synchronized with the {{incode|upstream}} repository.
After this is done, GitHub will let you know that your are synchronized with the {{incode|upstream}} repository.


{{Code|lang=text|code=
{{Code|lang=text|code=
This branch is even with LabRPS:master.  
This branch is even with LabRPS:master.  
}}
}}


Now that your {{incode|master}} is up to date, you may decide to switch to it, and delete the other branch that you used previously to develop a feature.
Now that your {{incode|master}} is up to date, you may decide to switch to it, and delete the other branch that you used previously to develop a feature.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 583: Line 492:
git branch -d myNewBranch
git branch -d myNewBranch
}}
}}


To delete the branch in the {{incode|origin}} remote repository, you can use the {{incode|push}} operation. Normally, you push a local branch; this creates a remote branch with the same name as your local branch.
To delete the branch in the {{incode|origin}} remote repository, you can use the {{incode|push}} operation. Normally, you push a local branch; this creates a remote branch with the same name as your local branch.


{{Code|lang=text|code=
{{Code|lang=text|code=
git push origin myNewBranch
git push origin myNewBranch
}}
}}


However, if you use the notation {{incode|local_name:remote_name}}, the local branch is created in the remote repository under a different name:
However, if you use the notation {{incode|local_name:remote_name}}, the local branch is created in the remote repository under a different name:


{{Code|lang=text|code=
{{Code|lang=text|code=
git push origin myNewBranch:someRemoteBranch
git push origin myNewBranch:someRemoteBranch
}}
}}


Therefore, you can delete the remote branch by pushing an empty local branch:
Therefore, you can delete the remote branch by pushing an empty local branch:


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 608: Line 511:
git push origin :someRemoteBranch
git push origin :someRemoteBranch
}}
}}


Now that you only have an up-to-date {{incode|master}}, you can create a new branch, and repeat the steps of changing files, committing, pushing, submitting a pull request, merging, and updating.
Now that you only have an up-to-date {{incode|master}}, you can create a new branch, and repeat the steps of changing files, committing, pushing, submitting a pull request, merging, and updating.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 617: Line 518:
git checkout -b anotherBranch
git checkout -b anotherBranch
}}
}}


If you don't want to delete your already custom branch, you may force updating it to be equal to the updated {{incode|master}}; then you can do whatever you want with it, including adding more commits and pushing it to the remote {{incode|origin}} repository.
If you don't want to delete your already custom branch, you may force updating it to be equal to the updated {{incode|master}}; then you can do whatever you want with it, including adding more commits and pushing it to the remote {{incode|origin}} repository.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 627: Line 526:
git push -f origin myNewBranch
git push -f origin myNewBranch
}}
}}


Hard resetting a branch like this is usually not needed. In most cases, you want to follow the sequence of creating a new branch, committing changes, pushing those changes, merging the branch, and then deleting the branch.
Hard resetting a branch like this is usually not needed. In most cases, you want to follow the sequence of creating a new branch, committing changes, pushing those changes, merging the branch, and then deleting the branch.
Line 640: Line 538:


Use {{incode|git ls-files}} to search the repository for file that contains a certain string in a filename. The example below will return all instances of the files that contain the 'dxf' in their filenames.
Use {{incode|git ls-files}} to search the repository for file that contains a certain string in a filename. The example below will return all instances of the files that contain the 'dxf' in their filenames.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 646: Line 543:
}}
}}
   
   
==== Search for a string ====  
==== Search for a string ====  


Use {{incode|git grep}} to search the repository for file that contains a certain string with the files themselves. The example below will return all instances of the files that contain the 'dxf' within each and every file.
Use {{incode|git grep}} to search the repository for file that contains a certain string with the files themselves. The example below will return all instances of the files that contain the 'dxf' within each and every file.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 656: Line 551:
}}
}}
   
   
=== Resolving merge conflicts ===  
=== Resolving merge conflicts ===  


Line 662: Line 556:


Once a conflict occurs, a message like this may appear.
Once a conflict occurs, a message like this may appear.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 669: Line 562:
Patch failed at 1234 Some commit message when editing source_code.py
Patch failed at 1234 Some commit message when editing source_code.py
}}
}}


If a specialized diff tool is installed and configured for Git, for example, Gnome's [https://wiki.gnome.org/Apps/Meld Meld], the conflict can be examined and solved by using the {{incode|mergetool}} operation.
If a specialized diff tool is installed and configured for Git, for example, Gnome's [https://wiki.gnome.org/Apps/Meld Meld], the conflict can be examined and solved by using the {{incode|mergetool}} operation.


{{Code|lang=text|code=
{{Code|lang=text|code=
git mergetool
git mergetool
}}
}}


The Meld tool normally displays three columns; the two columns on the sides display the two conflicting files, while the column on the middle displays the new code that will be saved and committed finally. Therefore, this central column should be edited in a way that it integrates the code of both side columns. Once the conflict is solved and the new source code (the central column) is saved, the Meld tool can be closed. Then the {{incode|merge}} or {{incode|rebase}} operation can continue.
The Meld tool normally displays three columns; the two columns on the sides display the two conflicting files, while the column on the middle displays the new code that will be saved and committed finally. Therefore, this central column should be edited in a way that it integrates the code of both side columns. Once the conflict is solved and the new source code (the central column) is saved, the Meld tool can be closed. Then the {{incode|merge}} or {{incode|rebase}} operation can continue.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 686: Line 575:
git rebase --continue
git rebase --continue
}}
}}


For more information on merging and solving conflicts see:
For more information on merging and solving conflicts see:
Line 697: Line 585:


Inspect the history of a single file through various commits with the {{incode|log}} operation:
Inspect the history of a single file through various commits with the {{incode|log}} operation:


{{Code|lang=text|code=
{{Code|lang=text|code=
git log --patch path
git log --patch path
}}
}}


Where {{incode|path}} can be any directory or file. Instead of {{incode|--patch}}, also the shorthands {{incode|-p}} or {{incode|-u}} can be used.
Where {{incode|path}} can be any directory or file. Instead of {{incode|--patch}}, also the shorthands {{incode|-p}} or {{incode|-u}} can be used.
Line 709: Line 595:


Inspect the changes between two branches with the {{incode|log}} and {{incode|diff}} operations with the names of the branches:
Inspect the changes between two branches with the {{incode|log}} and {{incode|diff}} operations with the names of the branches:


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 715: Line 600:
git diff master..myBranch
git diff master..myBranch
}}
}}


The {{incode|log}} operation shows the commits, while {{incode|diff}} shows the actual changes in the files.
The {{incode|log}} operation shows the commits, while {{incode|diff}} shows the actual changes in the files.
Line 724: Line 608:


This can be done quickly using the {{incode|checkout}} operation:
This can be done quickly using the {{incode|checkout}} operation:


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 730: Line 613:
git checkout .
git checkout .
}}
}}


This will restore the {{incode|path}} (a file or a directory) to the state it is at the tip of the branch, discarding changes that haven't been committed. If {{incode|path}} is the single dot {{incode|.}}, it will restore all files in the current directory.
This will restore the {{incode|path}} (a file or a directory) to the state it is at the tip of the branch, discarding changes that haven't been committed. If {{incode|path}} is the single dot {{incode|.}}, it will restore all files in the current directory.


If you have accidentally added files and directories you can use the {{incode|clean}} operation:
If you have accidentally added files and directories you can use the {{incode|clean}} operation:


{{Code|lang=text|code=
{{Code|lang=text|code=
git clean -df
git clean -df
}}
}}


This will forcefully delete all files and directories ({{incode|-df}}) that are not being tracked by the repository, that is, those that have not been included previously with the {{incode|add}} operation.
This will forcefully delete all files and directories ({{incode|-df}}) that are not being tracked by the repository, that is, those that have not been included previously with the {{incode|add}} operation.


To completely reset the repository, losing all uncommitted modifications, use the {{incode|reset}} operation:
To completely reset the repository, losing all uncommitted modifications, use the {{incode|reset}} operation:


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 751: Line 630:
git reset --hard FETCH_HEAD
git reset --hard FETCH_HEAD
}}
}}


Where {{incode|FETCH_HEAD}} is the the tip of the {{incode|upstream}} repository. Another commit can also be used.
Where {{incode|FETCH_HEAD}} is the the tip of the {{incode|upstream}} repository. Another commit can also be used.
Line 760: Line 638:


If you have committed many branches to the {{incode|upstream}} repository, you may wish to remove these branches from your local system as they have already been merged. The branch in the {{incode|origin}} repository online can be deleted immediately after merging. Then you can remove the local references to that branch, using the {{incode|--prune}} or {{incode|prune}} options to the {{incode|fetch}} and {{incode|remote}} operations.
If you have committed many branches to the {{incode|upstream}} repository, you may wish to remove these branches from your local system as they have already been merged. The branch in the {{incode|origin}} repository online can be deleted immediately after merging. Then you can remove the local references to that branch, using the {{incode|--prune}} or {{incode|prune}} options to the {{incode|fetch}} and {{incode|remote}} operations.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 766: Line 643:
git remote prune origin
git remote prune origin
}}
}}


Finally you can delete the branches locally
Finally you can delete the branches locally


{{Code|lang=text|code=
{{Code|lang=text|code=
git branch -D myBranch
git branch -D myBranch
}}
}}


It is also a good practice to do garbage collection after a while, by using the {{incode|gc}} operation. This will cleanup unnecessary files, and compress local file revisions, in order to optimize local disk usage of the repository.
It is also a good practice to do garbage collection after a while, by using the {{incode|gc}} operation. This will cleanup unnecessary files, and compress local file revisions, in order to optimize local disk usage of the repository.


{{Code|lang=text|code=
{{Code|lang=text|code=
git gc
git gc
}}
}}


=== Working with patches ===  
=== Working with patches ===  
Line 791: Line 663:


* 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.
* 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.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 797: Line 668:
git checkout myBranch
git checkout myBranch
}}
}}


* Now use {{incode|git format-patch}} against the master branch, and use the {{incode|--stdout}} 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.
* Now use {{incode|git format-patch}} against the master branch, and use the {{incode|--stdout}} 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.


{{Code|lang=text|code=
{{Code|lang=text|code=
git format-patch master --stdout > ../myCode.patch
git format-patch master --stdout > ../myCode.patch
}}
}}


* Another method is
* Another method is


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 814: Line 681:
git format-patch HEAD~1
git format-patch HEAD~1
}}
}}


The number of circumflex carets {{incode|^}} or the number {{incode|1}} indicate the number of commits that should be considered, that is, {{incode|^^^}} or {{incode|~3}} will create three patches for three commits.
The number of circumflex carets {{incode|^}} or the number {{incode|1}} indicate the number of commits that should be considered, that is, {{incode|^^^}} or {{incode|~3}} will create three patches for three commits.


{{Code|lang=text|code=
{{Code|lang=text|code=
git format-patch HEAD^
git format-patch HEAD^
}}
}}


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


{{Code|lang=text|code=
{{Code|lang=text|code=
XXXX-commit-message.patch
XXXX-commit-message.patch
}}
}}


where {{incode|XXXX}} is a number from {{incode|0000}} to {{incode|9999}}, and the commit message forms the majority of the file name, for example,
where {{incode|XXXX}} is a number from {{incode|0000}} to {{incode|9999}}, and the commit message forms the majority of the file name, for example,


{{Code|lang=text|code=
{{Code|lang=text|code=
0001-fix-ViewProjMatrix-getProjectionMatrix.patch
0001-fix-ViewProjMatrix-getProjectionMatrix.patch
}}
}}


==== Applying patches ====  
==== Applying patches ====  
Line 845: Line 705:


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


{{Code|lang=text|code=
{{Code|lang=text|code=
git apply myCode.patch
git apply myCode.patch
}}
}}


You can use {{incode|curl}} to download a patch from a website, and then apply it through {{incode|git}}.
You can use {{incode|curl}} to download a patch from a website, and then apply it through {{incode|git}}.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 859: Line 716:
git apply myCode.patch
git apply myCode.patch
}}
}}


Add {{incode|.diff}} or {{incode|.patch}} 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.
Add {{incode|.diff}} or {{incode|.patch}} 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.
Line 867: Line 723:


You can point {{incode|curl}} to a particular commit patch in the repository, and pipe it directly to {{incode|git}} to apply the patch.
You can point {{incode|curl}} to a particular commit patch in the repository, and pipe it directly to {{incode|git}} to apply the patch.


<pre>
<pre>
curl https://github.com/LabRPS/LabRPS/commit/c476589652a0f67b544735740e20ff702e8d0621.patch | git apply -
curl https://github.com/LabRPS/LabRPS/commit/c476589652a0f67b544735740e20ff702e8d0621.patch | git apply -
</pre>
</pre>


==== Reversing a patch ====  
==== Reversing a patch ====  
Line 879: Line 733:


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


{{Code|lang=text|code=
{{Code|lang=text|code=
git apply -R myCode.patch
git apply -R myCode.patch
}}
}}


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


{{Code|lang=text|code=
{{Code|lang=text|code=
git checkout -f
git checkout -f
}}
}}


=== Stashing git commits ===  
=== 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 {{incode|git stash}} command can be used to temporarily store those uncommitted local changes.
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 {{incode|git stash}} command can be used to temporarily store those uncommitted local changes.


{{Code|lang=text|code=
{{Code|lang=text|code=
git stash
git stash
}}
}}


If in the future you want to use those commits, you can "pop" the commits out of the stash, and into your working branch.
If in the future you want to use those commits, you can "pop" the commits out of the stash, and into your working branch.


{{Code|lang=text|code=
{{Code|lang=text|code=
git stash pop
git stash pop
}}
}}


Or if you decide that you don't like those saved commits anymore, you may drop the commits from the stash entirely.
Or if you decide that you don't like those saved commits anymore, you may drop the commits from the stash entirely.


{{Code|lang=text|code=
{{Code|lang=text|code=
git stash drop
git stash drop
}}
}}


You can list multiple stash commits with
You can list multiple stash commits with


{{Code|lang=text|code=
{{Code|lang=text|code=
git stash list
git stash list
}}
}}


To learn more, read [https://medium.freecodecamp.org/useful-tricks-you-might-not-know-about-git-stash-e8a9490f0a1a Useful tricks you might not know about Git stash].
To learn more, read [https://medium.freecodecamp.org/useful-tricks-you-might-not-know-about-git-stash-e8a9490f0a1a Useful tricks you might not know about Git stash].
Line 933: Line 775:


[https://gist.github.com/piscisaureus/3342247 Checkout GitHub pull requests locally]
[https://gist.github.com/piscisaureus/3342247 Checkout GitHub pull requests locally]


=== Blaming ===  
=== Blaming ===  
Line 950: Line 791:


Then enter this from the terminal:
Then enter this from the terminal:


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 957: Line 797:
git bisect bad efgh
git bisect bad efgh
}}
}}


Result: {{incode|git}} will check out the mid point between the two commits.  
Result: {{incode|git}} will check out the mid point between the two commits.  


The next step is to build and test the code. If the system works, continue the process by typing:
The next step is to build and test the code. If the system works, continue the process by typing:


{{Code|lang=text|code=
{{Code|lang=text|code=
git bisect good
git bisect good
}}
}}


Repeat the previous step of building the code and testing it.
Repeat the previous step of building the code and testing it.


If the system is broken, type:
If the system is broken, type:


{{Code|lang=text|code=
{{Code|lang=text|code=
git bisect bad
git bisect bad
}}
}}


Repeat the previous steps applying {{incode|good}} or {{incode|bad}} depending on the outcome of your tests.
Repeat the previous steps applying {{incode|good}} or {{incode|bad}} depending on the outcome of your tests.
Line 984: Line 819:


Finally, to exit the bisect process, type:
Finally, to exit the bisect process, type:


{{Code|lang=text|code=
{{Code|lang=text|code=
git bisect reset
git bisect reset
}}
}}


Note: {{incode|git bisect}} takes a long time if good and bad are far apart.
Note: {{incode|git bisect}} takes a long time if good and bad are far apart.
Line 996: Line 829:


In contrast to subversion, which uses a consecutive number for its revisions, Git produces [https://en.wikipedia.org/wiki/SHA-1 SHA-1 hash values] with every commit. A hash value is a long alphanumeric string that looks like this
In contrast to subversion, which uses a consecutive number for its revisions, Git produces [https://en.wikipedia.org/wiki/SHA-1 SHA-1 hash values] with every commit. A hash value is a long alphanumeric string that looks like this


{{Code|lang=text|code=
{{Code|lang=text|code=
9b3ffef570596e184006287434fba54a4b03ccc3
9b3ffef570596e184006287434fba54a4b03ccc3
}}
}}


=== Latest revision number ===  
=== Latest revision number ===  


To find the latest revision number of a particular branch use the {{incode|rev-list}} operation with the {{incode|--count}} option. Give the name of the branch, remote repository, tag, or a special pointer like {{incode|HEAD}}, to indicate the last commit in that particular object.
To find the latest revision number of a particular branch use the {{incode|rev-list}} operation with the {{incode|--count}} option. Give the name of the branch, remote repository, tag, or a special pointer like {{incode|HEAD}}, to indicate the last commit in that particular object.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 1,013: Line 843:
git rev-list --count origin
git rev-list --count origin
}}
}}


Or browse [https://github.com/LabRPS/LabRPS the repository on GitHub], and read the amount of commits reported in the particular branch.
Or browse [https://github.com/LabRPS/LabRPS the repository on GitHub], and read the amount of commits reported in the particular branch.
Line 1,020: Line 849:


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 {{incode|rev-list}} operation; the input can be the full hash, or a partial hash that is unique, usually the first 7 digits are enough.
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 {{incode|rev-list}} operation; the input can be the full hash, or a partial hash that is unique, usually the first 7 digits are enough.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 1,026: Line 854:
git rev-list --count 9948ee4
git rev-list --count 9948ee4
}}
}}


=== Revision hash of a specific commit number ===  
=== 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 ({{incode|HEAD}}). First, get the latest 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 ({{incode|HEAD}}). First, get the latest commit number.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 1,037: Line 863:
17465
17465
}}
}}


Then subtract the commit that we want.
Then subtract the commit that we want.


{{Code|lang=text|code=
{{Code|lang=text|code=
17465 - 15000 = 2465
17465 - 15000 = 2465
}}
}}


Then use the {{incode|log}} operation to show all commits and hashes. The {{incode|--skip}} option jumps the difference in commits that we calculated so that we go directly to the hash that we are looking for.
Then use the {{incode|log}} operation to show all commits and hashes. The {{incode|--skip}} option jumps the difference in commits that we calculated so that we go directly to the hash that we are looking for.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 1,054: Line 876:
commit 44c2f19e380e76b567d114a6360519d66f7a9e24
commit 44c2f19e380e76b567d114a6360519d66f7a9e24
}}
}}


Since the log may show you two close commits, confirm it's the right commit number. If it's off by one, just pick the next commit in the sequence (before or after) and check again.
Since the log may show you two close commits, confirm it's the right commit number. If it's off by one, just pick the next commit in the sequence (before or after) and check again.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 1,063: Line 883:
15000
15000
}}
}}


* [https://labrps.com/boards?f=10&t=26673 Show the commits] immediately before a particular commit in GitHub: in the address bar of the browser, change the word {{incode|commit}} to {{incode|commits}} to show a list.
* [https://labrps.com/boards?f=10&t=26673 Show the commits] immediately before a particular commit in GitHub: in the address bar of the browser, change the word {{incode|commit}} to {{incode|commits}} to show a list.
Line 1,072: Line 891:
=== Revision number in LabRPS's interface ===  
=== Revision number in LabRPS's interface ===  


The version number that appears with the [[Std_About|Std About]] tool is defined in {{incode|src/Build/Version.h}}, which is created at compile time when the {{incode|cmake}} tool is run. Read [https://labrps.com/boards?f=4&t=3025 Extract version number from git source] for more information.
The version number that appears with the [[Std_About|Std About]] tool is defined in {{incode|app/src/globals.cpp}}, Read [https://labrps.com/boards?f=4&t=3025 Extract version number from git source] for more information.


== Adding other repositories (remotes) ==  
== Adding other repositories (remotes) ==  
Line 1,079: Line 898:


Use the {{incode|git remote}} command to add these other repositories so that you can {{incode|fetch}} and {{incode|pull}} their code.
Use the {{incode|git remote}} command to add these other repositories so that you can {{incode|fetch}} and {{incode|pull}} their code.


{{Code|lang=text|code=
{{Code|lang=text|code=
Line 1,087: Line 905:
git checkout -b OTHER_BRANCH OTHER_USER/OTHER_BRANCH
git checkout -b OTHER_BRANCH OTHER_USER/OTHER_BRANCH
}}
}}


For example, lets add Bernd's remote repository:
For example, lets add Bernd's remote repository:


{{Code|lang=text|code=
{{Code|lang=text|code=
git remote add bernd http://github.com/berndhahnebach/LabRPS_bhb
git remote add bernd http://github.com/berndhahnebach/LabRPS_bhb
}}
}}


The {{incode|git fetch}} command downloads the references from that remote repository.
The {{incode|git fetch}} command downloads the references from that remote repository.


{{Code|lang=text|code=
{{Code|lang=text|code=
git fetch bernd
git fetch bernd
}}
}}


List all branches in your own repository, and those from your added remotes. Bernd's branches will display as {{incode|remotes/bernd/<branchname>}}.
List all branches in your own repository, and those from your added remotes. Bernd's branches will display as {{incode|remotes/bernd/<branchname>}}.


{{Code|lang=text|code=
{{Code|lang=text|code=
git branch -a
git branch -a
}}
}}


Now, lets view a summarized list of the last 10 commits of bernd's {{incode|femdev}} branch.
Now, lets view a summarized list of the last 10 commits of bernd's {{incode|femdev}} branch.


{{Code|lang=text|code=
{{Code|lang=text|code=
git log -10 --oneline remotes/bernd/femdev
git log -10 --oneline remotes/bernd/femdev
}}
}}


Now we can checkout the desired branch to inspect.
Now we can checkout the desired branch to inspect.


{{Code|lang=text|code=
{{Code|lang=text|code=
git checkout remotes/bernd/femdev
git checkout remotes/bernd/femdev
}}
}}


Then we can create a local branch that is based on the remote branch. This local branch we can modify, and add our own code to it.
Then we can create a local branch that is based on the remote branch. This local branch we can modify, and add our own code to it.


{{Code|lang=text|code=
{{Code|lang=text|code=
git checkout -b local_branch_name /remotes/bernd/femdev
git checkout -b local_branch_name /remotes/bernd/femdev
}}
}}


You may wish to {{incode|git rebase}} the newly obtained branch onto the {{incode|upstream/master}} branch to make sure it is using the latest code. If there are conflicts, they will have to be solved at this point.
You may wish to {{incode|git rebase}} the newly obtained branch onto the {{incode|upstream/master}} branch to make sure it is using the latest code. If there are conflicts, they will have to be solved at this point.


{{Code|lang=text|code=
{{Code|lang=text|code=
git pull --rebase upstream master
git pull --rebase upstream master
}}
}}


The new branch is ready to be modified and compiled as described in [[Compiling|Compiling]].
The new branch is ready to be modified and compiled as described in [[Compiling|Compiling]].
Line 1,151: Line 954:
== Further reading ==  
== Further reading ==  


* [[Developing LabRPS with GitKraken|Developing LabRPS with GitKraken]], a guide to use a graphical interface with Git.
* [https://wiki.spheredev.org/index.php/Git_for_the_lazy Git for the lazy], a very concise guide to the principal commands of {{incode|git}}.
* [https://wiki.spheredev.org/index.php/Git_for_the_lazy Git for the lazy], a very concise guide to the principal commands of {{incode|git}}.
* The [https://git-scm.com/book Pro Git book], an open source book teaching you about Git; it is available in electronic and print versions.
* The [https://git-scm.com/book Pro Git book], an open source book teaching you about Git; it is available in electronic and print versions.

Latest revision as of 08:05, 24 August 2022

Introduction

The main source code management tool for the LabRPS 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 LabRPS 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 document focuses on the use of Git for LabRPS development. Compiling LabRPS is described in Compiling.

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 gitk (the first graphical interface developed), gitg (Gnome), qgit (Qt), and tig (Ncurses), git-cola.

Note: if any of this is starting to make you dizzy, there is a very good non-technical series on how to use git and Github called 'Git and Github for Poets'

Source code access

Everybody can access and get a copy of the LabRPS source code, but only the LabRPS project managers have write access to it. You can get a copy of the code, study it and modify it as you wish, but 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 the managers. This style of development is known as the Dictator and lieutenants workflow, as the core developers (dictators) and trusted developers (lieutenants) filter the code that is submitted by independent developers and users.

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

File:LabRPS git workflow.svg

Generic workflow to develop code for LabRPS; everybody can get the code from the main repository, but the main developers have the exclusive right to review and merge submissions by other developers.

Official GitHub repository

The LabRPS source code is hosted on Github, https://github.com/LabRPS/LabRPS

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:


git config user.name "YOUR_NAME"
git config user.email GITHUB_USERNAME@users.noreply.github.com

Where "YOUR_NAME" represents your full name or nickname, used to identify the author of a particular commit, and GITHUB_USERNAME indicates the name of your account on GitHub.

Remote repositories

Please read What is the difference between origin and upstream on GitHub? (Stackoverflow) to help you understand the difference between origin and upstream in the context of Git. This section explains how to set the correct repositories for development. Essentially:

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 LabRPS 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 LabRPS repository in GitHub, then clone this personal fork to your computer, and finally set the upstream repository.

  • Log in to your GitHub account.
  • Go to the official LabRPS repository: https://github.com/LabRPS/LabRPS
  • In the top right of the page press the "Fork" button. This will create a personal copy of the LabRPS repository under your GitHub username: https://github.com/GITHUB_USERNAME/LabRPS
  • On your machine, clone your newly created LabRPS fork. It will be created inside a directory labrps-source.
git clone https://github.com/GITHUB_USERNAME/LabRPS.git labrps-source
  • Once the download is complete, enter the new source directory and set the upstream repository.
cd  labrps-source
git remote add upstream https://github.com/LabRPS/LabRPS.git
  • Confirm your remote repositories with git remote -v; the output should be similar to this
origin	https://github.com/GITHUB_USERNAME/LabRPS.git (fetch)
origin	https://github.com/GITHUB_USERNAME/LabRPS.git (push)
upstream	https://github.com/LabRPS/LabRPS.git (fetch)
upstream	https://github.com/LabRPS/LabRPS.git (push)
  • Now development can begin.

2nd Method: Clone LabRPS directly to your local machine

First you will fork the LabRPS repository in GitHub, however, you will clone the original LabRPS repository to your local machine, and then alter your remotes via the terminal.

  • Log in to your GitHub account.
  • Go to the official LabRPS repository: https://github.com/LabRPS/LabRPS
  • In the top right of the page press the "Fork" button. This will create a personal copy of the LabRPS repository under your GitHub username: https://github.com/GITHUB_USERNAME/LabRPS
  • Clone the original LabRPS repository. It will be created inside a directory labrps-source.
git clone https://github.com/LabRPS/LabRPS.git labrps-source
  • Once the download is complete, enter the new source directory and set the origin repository.
cd labrps-source
git remote add origin https://github.com/GITHUB_USERNAME/LabRPS.git
  • Then set up the upstream repository.
git remote add upstream https://github.com/LabRPS/LabRPS.git
  • Confirm your remote repositories with git remote -v; the output should be similar to this
origin	https://github.com/GITHUB_USERNAME/LabRPS.git (fetch)
origin	https://github.com/GITHUB_USERNAME/LabRPS.git (push)
upstream	https://github.com/LabRPS/LabRPS.git (fetch)
upstream	https://github.com/LabRPS/LabRPS.git (push)
  • 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, origin should point to your personal fork; if it is pointing to the original LabRPS repository, change the name of this remote to upstream, and manually add the origin repository.

git remote rename origin upstream
git remote add origin https://github.com/GITHUB_USERNAME/LabRPS.git
git remote -v

You can also show more information with the show keyword.

git remote show origin
git remote show upstream

Git development process

Never develop on your local master branch. Instead, create a local branch for development, and then merge this local branch to the upstream master branch through a pull request. Please read Git Branching, Basic Branching and Merging, and GitHub - Contributing to a project to learn more.

File:LabRPS git branches workflow.svg

Generic workflow to develop code for LabRPS using git; the main repository is forked online and cloned to an offline computer (0); new branches (1) are used to commit local changes and additions to the code (2); the branches are rebased to the latest online code (3), and then are pushed to the remote repository (4); then a pull request is created in order to merge the code into the main repository (5). Then the personal clone is updated with the new master code (a); this updated master is also pushed to the remote repository (b) in order to have the same code both online and offline.

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. Branches are inexpensive, they don't copy the entire source tree, but merely create a point in time on top of which you will write code; thus branches help keep work in progress separate from the main code.

Using a new branch is done in two steps, first your create the branch, and then you switch to it:

git branch myNewBranch
git checkout myNewBranch

Alternatively, perform both steps with a single instruction:

git checkout -b myNewBranch

Now you can change branches with checkout whenever you need to work on them. To see the branches in your project and the current branch, use the branch operation alone, or add -v or -vv for more information:

git branch
git branch -vv

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

git log --oneline --decorate --graph --all

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 status and diff operations; when you are satisfied with the modifications, save the changes with the commit operation:

git status
git diff
git commit -a

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

Alternatively add the message in the commit itself:

git commit -a -m "Fix the bug in the clone function."

If you create new files or directories, you must use the add operation first to add them to the local repository before committing the changes.

git add path
git commit -a

Where path can be any directory or file.

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. LabRPS has adopted a format mentioned in the Pro Git book, which consists of a short message, and then a larger descriptive paragraph.

Short (50 chars or less) summary of changes
 
 More detailed explanatory text, if necessary.  Wrap it to about 72
 characters or so.  In some contexts, the first line is treated as the
 subject of an email and the rest of the text as the body.  The blank
 line separating the summary from the body is critical (unless you omit
 the body entirely); tools like rebase can get confused if you run the
 two together.
 
 Further paragraphs come after blank lines. 
 
  - Bullet points are okay, too
 
  - Typically a hyphen or asterisk is used for the bullet, preceded by a
    single space, with blank lines in between, but conventions vary here

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

git log master..myNewBranch

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 --squash 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.

Squashing commits

Squashing refers to the process of combining various consecutive commits into one. This may be desirable if you made many small commits that you want to present as a single commit, for example, when changing a single variable, correcting spelling mistakes, and adjusting the spacing of the code. You should squash only small commits to a single file; big changes to the code across multiple files should contain the full commit history.

With git log --oneline you can see many commits in sequence, with the newest commit on top. In this example, starting from "feature A" many commits are made to implement "feature B"; we would like to squash all commits belonging to "feature B" into one.

871adb OK, feature B is fully implemented
1c3317 Whoops, it is not ready yet...
87871a I'm almost ready!
643d0e Code cleanup
af2581 Fix this and that
4e9baa Good implementation
d94e78 Prepare the module for feature B
6394da Feature A

Use the rebase operation with the --interactive or -i option to select various commits and squash them. Use the hash of the commit just before the first one that you want to squash, in this case the one corresponding to "feature A".

git rebase -i 6394da

(TIP: If you know how many commits you want to edit, you can use git rebase -i HEAD~n to work on the last n commits)

The command line editor, like nano or vim, will open to show you the commits again, now with the older commit on top. Before each commit, the word pick will be shown. Delete the word pick, and write the word squash or just the letter s instead, with the exception of the first entry; this commit is the oldest one, so all future commits will be squashed into it.

pick d94e78 Prepare the module for feature B
s 4e9baa Good implementation
s af2581 Fix this and that
s 643d0e Code cleanup
s 87871a I'm almost ready!
s 1c3317 Whoops, it is not ready yet...
s 871adb OK, feature B is fully implemented

Save the file and close the editor.

The editor will open up again. Now you can add a longer message that describes all changes as if they were a single commit. Save the file and close the editor once more. This will finish combining those commits into one, with the new commit message that you wrote.

You can use git log --oneline again to observe the new commit history. In this case only a single commit for "feature B" will appear, on top of the unmodified commit for "feature A".

c83d67 OK, feature B is fully implemented now, with proper module setup, and clean code.
6394da Feature A

When coding for LabRPS, we ask that you begin each commit message with the module that it affects. For example, a commit message for a change to sketcher might be:

Sketcher: make straight lines curve a bit

Straight lines are sort of ugly, so this commit adds a little bit of curvature to them, so
they are more visually pleasing. They also sparkle some, and change colors over time.

Fixes bug #1234.

Your PR will be easier to review, and faster to be merged, if you are careful to use rebase to structure and describe your commits before submitting.

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 origin or upstream (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 LabRPS, you should push your local branch to the origin remote repository, that is, https://github.com/GITHUB_USERNAME/LabRPS. 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.

git push origin myNewBranch

When you work with a single branch, you may need to interactively rebase, squash, and fix commits many times. In this case, your branch history will not be simple, and you will not be able to push it to the remote repository. You may get a message like the following, saying that it is not possible to do a "fast-forward" push.

error: failed to push some refs to 'https://github.com/USER/LabRPS.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

In order to finally push your branch to the remote repository you need to "force push" it. This will completely overwrite your remote branch with the actual branch that you have offline.

git push -f origin myNewBranch

The regular developer doesn't have write access to the upstream repository https://github.com/LabRPS/LabRPS, therefore, you should never push code to this remote server.

Rebasing from upstream

While you work on your own branch, the official LabRPS code keeps "moving forward" with commits from other developers, and thus starts diverging from the code that you have in your personal fork.

      .-----A origin/myNewBranch
     / 
-----o-----------Z LabRPS upstream/master


Therefore, when you are ready to merge your branch to the main LabRPS 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.

git checkout myNewBranch
git pull --rebase upstream master

This will download the code from the master branch of the upstream repository (the official LabRPS source), and will merge it with your current branch (myNewBranch), so that your changes will appear 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' origin/myNewBranch
                 /
-----o-----------Z LabRPS upstream/master

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

git checkout myNewBranch
git pull --rebase upstream master
git push origin myNewBranch

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

git pull upstream

git fetch upstream
git merge FETCH_HEAD
git pull --rebase upstream master

git fetch upstream
git rebase master

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 LabRPS repository that you want to merge the new code in your branch with the official code.

To recap, the development process looks like this:

  1. Fork LabRPS and get a local copy of that fork.
  2. Create a branch on your fork and change to that branch.
  3. Code! Commit and much or as little as you like, writing good commit messages to keep track of what you are doing.
  4. When you are satisfied with your work, use git rebase -i HEAD~n (where n is the total number of commits you've made) to collapse your commits into a logical set with good commit messages (each message should begin with the name of the module it affects, e.g. "Sketcher: make straight lines curve a bit").
  5. Use GitHub to submit your code as a "Pull Request (PR)" as described below.

As soon as you push the code to your origin repository https://github.com/GITHUB_USERNAME/LabRPS, GitHub will give you the option of comparing and creating a pull request against the upstream repository. By pressing Compare & pull request you will open an interface that will allow you to pick which repository is the "base", target of the merge, and which is the "head", your additional code. 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.

GitHub will show you a text editor so you can write a message documenting your changes: this editor will be pre-filled with a welcome message (that you can delete), a checklist (that you should go through), and a reminder to document your change on the wiki when it's accepted. To use the checklist, go through each item in turn and change the [ ] to [X] to indicate that you've done that step. GitHub 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" and the "head" so that everybody can immediately see your intended modifications. Double-check these for things like stray blank lines you didn't mean to add, or huge formatting changes that your IDE decided to make behind your back.

base repository: LabRPS/LabRPS    base: master  <----  head repository: GITHUB_USERNAME/LabRPS    compare: myNewBranch

Able to merge. These branches can be automatically merged.

Click Create pull request to proceed. A message will appear indicating that some checks need to be done on the code. This is a system that compiles LabRPS 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 LabRPS pull requests.

Some checks haven’t completed yet

* continuous-integration/travis-ci/pr Pending — The Travis CI build is in progress  |Required|

If the tests succeed, you will see a message such as the following

All checks have passed

* continuous-integration/travis-ci/pr — The Travis CI build passed  |Required|

This branch has no conflicts with the base branch Only those with write access to this repository can merge pull requests.

Now you must wait for the administrators to merge your branch; you will be notified when this happens.

Pull request successfully merged and closed

You’re all set — the GITHUB_USERNAME:myNewBranch branch can be safely deleted.
If you wish, you can also delete your fork of LabRPS/LabRPS.

If you wish, you may delete the branch that was just merged, or even your entire LabRPS fork, as your own code is already included at the end of the master branch.

-----o-----------Z----A' LabRPS upstream/master

Note: you may continue working (git commit -a) on the same branch while you wait for merge approval; if you git push again, a second merge commit will be queued in the same pull request, and another automated test will be done. That is, while your merges aren't yet approved by the administrators, you may keep pushing changes to your origin repository, and this will queue those commits in the same pull request to the upstream repository. Using a single pull request to queue many individual commits is often desirable for small changes. For big additions to the source code, you should create another branch, develop your features there, and then submit a separate pull request for this branch.

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

base repository: SomeProject/Some_Software  base: master       <----  head repository: GITHUB_USERNAME/Some_Software  compare: add_new_functions
base repository: GITHUB_USERNAME/LabRPS    base: myNewBranch  <----  head repository: LabRPS/LabRPS                compare: master
base repository: GITHUB_USERNAME/LabRPS    base: myNewBranch  <----  head repository: GITHUB_USERNAME/LabRPS        compare: fix-many-bugs-branch

Keeping the GitHub repository up to date

Once you've forked LabRPS, your personal repository exists independently from the original. When the original repository has new commits, GitHub will inform you that your personal repository is behind in number of commits:

This branch is 5 commits behind LabRPS:master.

In similar way, if you created a development branch with new code, GitHub will inform you that this branch is ahead in number of commits; that is, this branch has changes that haven't been merged into the official LabRPS repository:

This branch is 3 commits ahead of LabRPS:master.

While developing, both cases are possible, as your own branch may lack commits made by other developers, but include new commits by you:

This branch is 2 commits ahead, 14 commits behind LabRPS:master.

When developing code it is recommended that you rebase the branch in which you are currently working, as that will put your branch always ahead of the LabRPS master code.

As for your original master branch, it will never be automatically updated by GitHub; this is something that you must do yourself. Switch to the master branch, then pull from upstream (which performs a fetch and merge), and then push this updated master branch to your remote origin repository.

git checkout master
git pull upstream master
git push origin master

After this is done, GitHub will let you know that your are synchronized with the upstream repository.

This branch is even with LabRPS:master.

Now that your master is up to date, you may decide to switch to it, and delete the other branch that you used previously to develop a feature.

git checkout master
git branch -d myNewBranch

To delete the branch in the origin remote repository, you can use the push operation. Normally, you push a local branch; this creates a remote branch with the same name as your local branch.

git push origin myNewBranch

However, if you use the notation local_name:remote_name, the local branch is created in the remote repository under a different name:

git push origin myNewBranch:someRemoteBranch

Therefore, you can delete the remote branch by pushing an empty local branch:

git push origin :myNewBranch
git push origin :someRemoteBranch

Now that you only have an up-to-date master, you can create a new branch, and repeat the steps of changing files, committing, pushing, submitting a pull request, merging, and updating.

git checkout master
git checkout -b anotherBranch

If you don't want to delete your already custom branch, you may force updating it to be equal to the updated master; then you can do whatever you want with it, including adding more commits and pushing it to the remote origin repository.

git checkout myNewBranch
git reset --hard master
git push -f origin myNewBranch

Hard resetting a branch like this is usually not needed. In most cases, you want to follow the sequence of creating a new branch, committing changes, pushing those changes, merging the branch, and then deleting the branch.

Advanced Git operations

Searching

Some handy tools to help you find what you're looking for:

Search filenames

Use git ls-files to search the repository for file that contains a certain string in a filename. The example below will return all instances of the files that contain the 'dxf' in their filenames.

git ls-files *dxf*

Search for a string

Use git grep to search the repository for file that contains a certain string with the files themselves. The example below will return all instances of the files that contain the 'dxf' within each and every file.

git grep dxf

Resolving merge conflicts

Merging branches with git merge, or rebasing your branch with git rebase, will occasionally present conflicts, as files may have been modified by another author at the same time. If this happens you should see the changes of both sides, the other author's, and your own, and then make a decision on how to include both sets of changes in the best way possible. This is normally a manual process that cannot be automated; the programmer must understand the code, and decide what code to move, re-write, or drop to solve the conflict.

Once a conflict occurs, a message like this may appear.

CONFLICT (content): Merge conflict in src/Mod/source_code.py
error: Failed to merge in the changes.
Patch failed at 1234 Some commit message when editing source_code.py

If a specialized diff tool is installed and configured for Git, for example, Gnome's Meld, the conflict can be examined and solved by using the mergetool operation.

git mergetool

The Meld tool normally displays three columns; the two columns on the sides display the two conflicting files, while the column on the middle displays the new code that will be saved and committed finally. Therefore, this central column should be edited in a way that it integrates the code of both side columns. Once the conflict is solved and the new source code (the central column) is saved, the Meld tool can be closed. Then the merge or rebase operation can continue.

git merge --continue
git rebase --continue

For more information on merging and solving conflicts see:

Inspect changes

Inspect the history of a single file through various commits with the log operation:

git log --patch path

Where path can be any directory or file. Instead of --patch, also the shorthands -p or -u can be used.

Inspect changes between two branches

Inspect the changes between two branches with the log and diff operations with the names of the branches:

git log master..myBranch
git diff master..myBranch

The log operation shows the commits, while diff shows the actual changes in the files.

Reset files and directories

If you accidentally made modifications to a file or directory, you may want to completely revert these changes, to get the previous state of the source code.

This can be done quickly using the checkout operation:

git checkout path
git checkout .

This will restore the path (a file or a directory) to the state it is at the tip of the branch, discarding changes that haven't been committed. If path is the single dot ., it will restore all files in the current directory.

If you have accidentally added files and directories you can use the clean operation:

git clean -df

This will forcefully delete all files and directories (-df) that are not being tracked by the repository, that is, those that have not been included previously with the add operation.

To completely reset the repository, losing all uncommitted modifications, use the reset operation:

git fetch
git reset --hard FETCH_HEAD

Where FETCH_HEAD is the the tip of the upstream repository. Another commit can also be used.

The revert operation also reverts changes. However, this command does this by adding another commit to the history; in many cases this is not desired.

Pruning old branches

If you have committed many branches to the upstream repository, you may wish to remove these branches from your local system as they have already been merged. The branch in the origin repository online can be deleted immediately after merging. Then you can remove the local references to that branch, using the --prune or prune options to the fetch and remote operations.

git fetch --prune origin
git remote prune origin

Finally you can delete the branches locally

git branch -D myBranch

It is also a good practice to do garbage collection after a while, by using the gc operation. This will cleanup unnecessary files, and compress local file revisions, in order to optimize local disk usage of the repository.

git gc

Working with patches

Although Git allows you to merge different branches of code with git merge (in your computer) or a pull request (remote repository), there are times when it may be desirable to create a traditional "patch", which can be sent as an attachment through email. The following workflow explains how to do this.

Creating patches

  • 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.
git branch -v
git checkout myBranch
  • Now use git format-patch against the master branch, and use the --stdout 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.
git format-patch master --stdout > ../myCode.patch
  • Another method is
git format-patch HEAD^
git format-patch HEAD~1

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

git format-patch HEAD^

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

XXXX-commit-message.patch

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

0001-fix-ViewProjMatrix-getProjectionMatrix.patch

Applying patches

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.

git apply myCode.patch

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

curl -O https://some.website.org/code/myCode.patch
git apply myCode.patch

Add .diff or .patch 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.

You can point curl to a particular commit patch in the repository, and pipe it directly to git to apply the patch.

curl https://github.com/LabRPS/LabRPS/commit/c476589652a0f67b544735740e20ff702e8d0621.patch | git apply -

Reversing a patch

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.

git apply -R myCode.patch

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

git checkout -f

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 git stash command can be used to temporarily store those uncommitted local changes.

git stash

If in the future you want to use those commits, you can "pop" the commits out of the stash, and into your working branch.

git stash pop

Or if you decide that you don't like those saved commits anymore, you may drop the commits from the stash entirely.

git stash drop

You can list multiple stash commits with

git stash list

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

Check out GitHub requests locally

Checkout GitHub pull requests locally

Blaming

Section TBD

Add content from https://labrps.com/boards?f=23&t=55943&p=481483#p481287

Bisect

git bisect is a method to find the specific commit that introduced a bug.

You need to find 2 commits:

  • A good commit (for example abcd) before the system broke.
  • A bad commit (for example efgh) after the system broke.

Then enter this from the terminal:

git bisect start
git bisect good abcd
git bisect bad efgh

Result: git will check out the mid point between the two commits.

The next step is to build and test the code. If the system works, continue the process by typing:

git bisect good

Repeat the previous step of building the code and testing it.

If the system is broken, type:

git bisect bad

Repeat the previous steps applying good or bad depending on the outcome of your tests.

Eventually, git will tell you that wxyz is the first bad commit.

Finally, to exit the bisect process, type:

git bisect reset

Note: git bisect takes a long time if good and bad are far apart.

LabRPS 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

9b3ffef570596e184006287434fba54a4b03ccc3

Latest revision number

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

git rev-list --count master
git rev-list --count HEAD
git rev-list --count origin

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 rev-list operation; the input can be the full hash, or a partial hash that is unique, usually the first 7 digits are enough.

git rev-list --count ab1520b872821414c6ce4a15fb85d471ac2a2b03
git rev-list --count 9948ee4

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 (HEAD). First, get the latest commit number.

git rev-list --count HEAD
17465

Then subtract the commit that we want.

17465 - 15000 = 2465

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

git log --skip=2465
commit 44c2f19e380e76b567d114a6360519d66f7a9e24

Since the log may show you two close commits, confirm it's the right commit number. If it's off by one, just pick the next commit in the sequence (before or after) and check again.

git rev-list --count 44c2f19e38
15000

Revision number in LabRPS's interface

The version number that appears with the Std About tool is defined in app/src/globals.cpp, Read Extract version number from git source for more information.

Adding other repositories (remotes)

Several collaborators of the LabRPS project have their own Git repositories where they build up their work or where they experiment new ideas before they are ready to be included in the official source code. You may want to get their sources in order to test their code yourself when they make a pull request.

Use the git remote command to add these other repositories so that you can fetch and pull their code.

git checkout master
git remote add OTHER_USER OTHER_URL
git fetch OTHER_USER
git checkout -b OTHER_BRANCH OTHER_USER/OTHER_BRANCH

For example, lets add Bernd's remote repository:

git remote add bernd http://github.com/berndhahnebach/LabRPS_bhb

The git fetch command downloads the references from that remote repository.

git fetch bernd

List all branches in your own repository, and those from your added remotes. Bernd's branches will display as remotes/bernd/<branchname>.

git branch -a

Now, lets view a summarized list of the last 10 commits of bernd's femdev branch.

git log -10 --oneline remotes/bernd/femdev

Now we can checkout the desired branch to inspect.

git checkout remotes/bernd/femdev

Then we can create a local branch that is based on the remote branch. This local branch we can modify, and add our own code to it.

git checkout -b local_branch_name /remotes/bernd/femdev

You may wish to git rebase the newly obtained branch onto the upstream/master branch to make sure it is using the latest code. If there are conflicts, they will have to be solved at this point.

git pull --rebase upstream master

The new branch is ready to be modified and compiled as described in Compiling.

Head to the development section of the LabRPS forum to discuss more about development.

Further reading