Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Git for developers: Difference between revisions

From MoodleDocs
mNo edit summary
m (Protected "Git for developers": Developer Docs Migration ([Edit=Allow only administrators] (indefinite)))
 
(108 intermediate revisions by 38 users not shown)
Line 1: Line 1:
This document is for helping you get started on Moodle development with Git. For further details of Git, see [[:Category:Git]].
{{Template:Migrated|newDocId=/docs/guides/git/}}
 
== Set-up ==
 
1. Go to [https://github.com/ Github] and create an account.
 
2. Go to the [https://github.com/moodle/moodle official Moodle Github repository] and click on the Fork button. You now have your own github Moodle repository.
 
3. Install Git on your computer. If you are on Mac, [http://code.google.com/p/git-osx-installer/ git-osx-installer] installs it in few click.
 
4. Now the technical part, you will need to setup your SSH public key, so you can push to your github Moodle repository from your local Moodle repository. On Mac you can go on this [http://help.github.com/mac-key-setup/ Github help page]. If you are on another system, go to your Github administration page, to the section SSH Public Keys, and you should see a link to a help page. Done? Good! That was the most difficult part!
 
== Working on 2.0 ==
 
1. Create a local clone repository of your github repository. In a terminal:
<code bash>
git clone git@github.com:YOUR_GITHUB_USERNAME/moodle.git ./YOUR_LOCAL_MOODLE_FOLDER/
</code>
In Debian Lenny (git version 1.5) you may want to use the following command:
<code bash>
git clone git@github.com:YOUR_GITHUB_USERNAME/moodle.git YOUR_LOCAL_MOODLE_FOLDER
</code>
2. You will create a update script in order to update your github and local repository from git.moodle.org. Add a reference to this Moodle.org Git repository and make your local master branch tracking it:
<code bash>
cd ./YOUR_LOCAL_MOODLE_FOLDER/
git remote add upstream git://git.moodle.org/moodle.git
git fetch upstream
git branch --set-upstream master upstream/master
</code>
 
3. Create the update script (name it "update_github")
 
<code bash>
cd YOUR_LOCAL_MOODLE_FOLDER
git checkout master
git pull
git push origin refs/remotes/upstream/master:master
</code>
 
4. Run the./update_github script. Your local repository ("master" branch) and your github repository ("master" branch) will be updated from git.moodle.org.
 
5. Now you can work on a new issue. You can use a Git UI tool like SmartGit for these steps:
* Run the update script (the script will switch to "master" branch, so take care to stash/commit the changes of your current branch). To check the current branch:
<code bash>
git branch -a
</code>
* Create a new branch and switch to it. You should never work in your local master branch:
<code bash>
cd YOUR_LOCAL_MOODLE_FOLDER
git branch BRANCH_NAME
git checkout BRANCH_NAME
</code>
* Fix your issue with your favorite IDE. Check updated files:
<code bash>
cd YOUR_LOCAL_MOODLE_FOLDER
git status
</code>
* Commit, push and do a pull request (FILE_NAME can be either a single file or a folder, for a folder the "add" command will be performed recursively)
<code bash>
git add FILE_NAME
git commit
git push origin BRANCH_NAME
</code>
 
== Working on 1.9 or earlier branches ==
This is just a tiny little bit more complicated that working on 2.0. Even though you could work in the same folder as for 2.0, you should create another folder for working on 1.9. It will make things less messy and easier to understand.
 
1. In a terminal
<code bash>
git clone git@github.com:YOUR_GITHUB_USERNAME/moodle.git ./YOUR_LOCAL_MOODLE_19_FOLDER/
</code>
 
2. Add the remote upstream. You want to update your local MOODLE_19_STABLE branch and your github (called "origin") MOODLE_19_STABLE branch from the official Moodle upstream.
<code bash>
cd ./YOUR_LOCAL_MOODLE_19_FOLDER/
git remote add upstream git://git.moodle.org/moodle.git
git fetch upstream
</code>
 
3. Now you are going to create a local 1.9 branch. You will keep this branch updated (the branch will track upstream) and you are never going to work in it.
<code bash>
git checkout -b MOODLE_19_STABLE upstream/MOODLE_19_STABLE
</code>
 
4. Create the update script in ./update_github
<code bash>
cd YOUR_LOCAL_MOODLE_19_FOLDER
git checkout MOODLE_19_STABLE
git pull
git push origin refs/remotes/upstream/MOODLE_19_STABLE:MOODLE_19_STABLE
</code>
 
5. Run the script. Your github MOODLE_19_STABLE branch and your local MOODLE_19_STABLE branch are now updated.
 
6. You can now work on an issue. You can use a Git UI tool like SmartGit for these steps:
* Run the update script (don't forget to stash/commit the change of your current local branch, you are going to switch to MOODLE_19_STABLE local branch)
* Create a new branch for your issue and switch to it
* Fix your issue with your favorite IDE
* Commit, push and do a pull request
 
== Peer review some code ==
You need to review someone else work, They must have pushed their work into github:
* github repository url: git://github.com/PEER_USERNAME/moodle.git
* branch name: MDL-XXXXX
 
1. Run the update script (it will switch to your local master branch and update the code)
 
2. Add the remote repository (only first time ever, then skip this step)
<code bash>
git remote add PEER_USERNAME git://github.com/PEER_USERNAME/moodle.git
</code>
 
3. Fetch the repository, so Git knows about the last changes in it
<code bash>
git fetch PEER_USERNAME
</code>
 
4. Switch to a new local 'testing' branch
<code bash>
git checkout -b PEER_USERNAME-MDL-XXXXX
</code>
 
5. Import the remote change into this local branch (but not marked as 'committed' so you can see the changes into your IDE)
<code bash>
git merge --squash PEER_USERNAME/MDL-XXXXX
</code>
 
6. You can now test in your favorite IDE. If it supports Git you should see the difference with your local master.
 
7. Finally you are going to delete this testing branch:
<code bash>
# Warning: if you didn't switch, you should still be on testing branch.
# the first command revert all the changes to the 'master' state.
git reset --hard
git checkout master
git branch -d PEER_USERNAME-MDL-XXXXX
</code>
 
== Troubleshooting ==
 
* If you want to delete a remote branch on github:
<code bash>
git push origin :CONFLICTING_REMOTE_BRANCH_NAME
</code>
* PHPStorm (beta version) named your 'origin' => 'master' when you cloned your github repository. It is better to rename it as 'origin'.
<code bash>
git remote rename master origin
</code>
* You've been working with Git in the early stages when Moodle HQ were experimenting with it. They now changed 'cvshead' to 'master' into upstream (moodle.org Git). You need to rename all your 'cvshead' github and local branches as 'master'. There what you should do (this is not a script to run, execute the command one at the time following the instructions):
<code bash>
#go to your root git folder
 
git fetch origin
git fetch upstream
git remote prune upstream
git remote prune origin
 
/// FIRST TIME YOU RUN THIS SCRIPT
git checkout -b master upstream/master
git push origin master
 
#on github, in your repository administration, change the default to 'master'
 
git push origin :cvshead
git branch -d cvshead
/// END OF FIRST TIME
 
git remote set-head origin -a
git remote set-head upstream -a
/// OTHER TIME - FOR YOUR OTHER GIT MOODLE FOLDERS
git checkout -b master upstream/master
git branch -d cvshead
/// END OF OTHER TIME
#check .git/config that there is no references to 'cvshead', if there are replace with 'master'
#optional:
#update your update script ('cvshead' => 'master')
</code>
* To know about your branches (the current branch is marked with an asterix)
<code bash>
git branch -a
</code>
*For any help
<code bash>
git any_command --help
</code>
 
==See also==
 
* [[Tutorial_on_using_git_in_Moodle_development]]
 
 
[[Category:Git]]

Latest revision as of 12:14, 14 April 2023

Important:

This content of this page has been updated and migrated to the new Moodle Developer Resources. The information contained on the page should no longer be seen up-to-date.

Why not view this page on the new site and help us to migrate more content to the new site!