Git Integration
This page provides a brief introduction to Git and describes the most important commands for working with Git in JupyterHub.
What is Git?¶
Git is a tool for managing projects, collaborating with others, and tracking changes to code or notebooks. As a version control system, Git logs changes to files and makes it possible to track the development of a project and revert to earlier versions if necessary.
Why Use Git?¶
- Track Changes: Git records every change you make to your files, so you can easily see what you've modified or revert to a previous version.
- Collaborate with Others: For group projects, Git allows multiple people to work on the same project at the same time without overwriting each other's changes.
- Reproducibility: Git stores the history of code, thereby ensuring the reproducibility of work.
- Backup: Repositories on GitHub or GitLab can serve as backups in case local copies are lost or damaged.
1. Getting Started with Git¶
Installing Git¶
Install Git as follows, depending on your operating system:
- Windows: Download the Git installer from Git for Windows and run the setup.
- macOS: Install Git via Homebrew using the command
brew install gitor use the installer for macOS. - Linux: Install Git via the package manager. For Ubuntu:
sudo apt-get install git
Configuring Git¶
Once Git is installed, configure it with your user information:
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"
2. Basic Git Commands¶
Cloning a Repository¶
When you clone a repository, a copy of a remote repository—such as a project on GitHub—is downloaded to your computer. Clone a repository using the following command:
git clone https://github.com/your-repository.git
Check the Status of Your Project¶
Check the status of the repository using the following command:
git status
Tracking Changes¶
Once you've made changes to your files, you can stage and commit them.
Staging Changes¶
Add the desired files to the "staging area" before committing using the following command:
git add <filename>
git add .
Committing Changes¶
Once the changes are in the staging area, create a commit using the following command:
git commit -m "Description of changes made"
Pushing Changes to Remote Repository¶
After committing locally, use the following command to push the changes to the remote repository (e.g., GitHub) so that they are available to others:
git push origin main
Pulling Changes from Remote Repository¶
Update the local repository using the following command:
git pull origin main
3. Branching and Merging¶
Git allows you to work on separate branches to develop new features without affecting the main codebase.
Creating a Branch¶
A new branch can be created using the following command:
git checkout -b new-feature
Switching Branches¶
You can switch to an existing branch using the following command:
git checkout main
Merging Branches¶
Once the changes have been completed in your branch, you can merge them into the main branch using the following command:
git checkout main
git merge new-feature
4. Collaborating with Git¶
Forking a Repository¶
If you want to contribute to a project, you can fork the repository on GitHub. This creates your own copy of the repository where you can make changes without affecting the original project.
Creating a Pull Request¶
Once you've made changes, you can submit a pull request (PR) to propose your changes to the original repository.
- Navigate to the original repository.
- Click the “Pull Request” button.
- Select the appropriate branch and submit the PR with a description of the changes.
5. Git in JupyterHub¶
Git is already preinstalled in the JupyterHub environments and can be used via the command line (terminal).
You can test the installation of Git through typing git version in terminal.
6. Troubleshooting¶
What to Do if You See Merge Conflicts¶
Merge conflicts occur when changes in two branches cannot be automatically merged. Here’s how to resolve them:
- Open the affected file to view the conflicted lines. Git marks the affected lines with conflict markers.
-
Manually edit the file to resolve the conflict.
- Git inserts three markers in each file with a conflict:
<<<<<<< HEAD– Start of the local (current) section=======– Separator>>>>>>> <branch‑name>– Start of the incoming section-
Open the file in an editor (e.g., VS Code, IntelliJ, vim).
-
Decide which version you want to keep, or combine both.
- Remove all conflict markers (
<<<<<<<, =======, >>>>>>>). - Save the file.
-
Add the file to the staging area and commit it using the following command:
git add <filename> git commit -m "Resolved merge conflict"
Undoing the Last Commit¶
If you made a mistake in your last commit and want to undo it, you can use:
git reset --soft HEAD~1
7. Useful Links¶
- Git Documentation
- GitHub Learning Lab: A platform for interactive Git and GitHub learning.
- Pro Git Book: A comprehensive guide to Git.
- Fixing problems in GIt CYOA: A comprehensive guide to recovering from what you did not mean to do when using git.