Using Git
Welcome to the Git User Guide for Jupyterhub.nrw! Whether you’re a student or researcher, Git is a powerful tool that can help you manage your projects, collaborate with others, and keep track of all the changes you make to your code, notebooks, or research documents.
What is Git?
Git is a version control system that tracks changes in files and allows multiple people to work on the same project simultaneously without interfering with each other’s work. It helps you keep a history of all changes made to a project, roll back to previous versions, and manage collaborations efficiently.
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: If you’re working on a group project or research with others, Git lets everyone contribute without overwriting each other’s work.
- Reproducibility: Git helps you maintain a complete history of your research code, ensuring your work is reproducible.
- Backup: GitHub or GitLab repositories act as a backup for your files in case your local copy is lost or corrupted.
1. Getting Started with Git
Before you start using Git, you need to set it up on your computer.
Installing Git
Follow these steps to install Git on your machine:
Windows: Download the Git installer from Git for Windows and run the setup. macOS: Install Git using Homebrew with the command brew install git, or download the installer from Git for macOS. Linux: Use the package manager to install Git. 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"
This information will be used to track your commits.
2. Basic Git Commands
Cloning a Repository
Let’s cover the most important Git commands you’ll need to manage your projects.
Cloning means downloading a copy of a remote Git repository (like a project on GitHub) to your computer. You’ll need this when you want to start working on an existing project or shared research repository:
git clone https://github.com/your-repository.git
Check the Status of Your Project
To see what changes you’ve made but haven’t committed yet, run:
git status
Tracking Changes
Once you’ve made changes to your files, you can stage and commit them.
Staging Changes
Before committing, you need to add files to the “staging area”:
git add <filename>
To add all changes:
git add .
Committing Changes
Once changes are staged, you can “commit” them. A commit is like a save point with a message describing the changes:
git commit -m "Description of changes made"
Pushing Changes to Remote Repository
After committing changes locally, push them to the remote repository (e.g., GitHub) so others can see your work:
git push origin main
This command uploads your commits to the main branch of the remote repository.
Pulling Changes from Remote Repository
To update your local repository with changes made by others, run:
git pull origin main
This command retrieves the latest changes from the remote repository.
4. Branching and Merging
Git allows you to work on separate branches to develop new features without affecting the main codebase.
Creating a Branch
To create a new branch:
git checkout -b new-feature
This creates a new branch and switches to it.
Switching Branches
To switch to an existing branch:
git checkout main
Merging Branches
Once you’ve made changes on your branch, you can merge it back into the main branch:
git checkout main
git merge new-feature
If there are any conflicts, Git will notify you to resolve them before completing the merge.
5. 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.
Go to the repository where you want to contribute. Click on the “Pull Request” button. Select your branch and submit the PR with a description of what you’ve changed.
6. Git in JupyterHub
Git is usually installed in the JupyterHub configurations and you can access it through the command line.
You can test the installation of Git through typing git version
in terminal.
7. 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:
Git will mark the conflicted files with conflict markers. Open the file to see the conflict. Edit the file to resolve the conflict manually.
Stage the file and commit the changes:
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
This will undo the commit but keep your changes staged for re-commit.
8. Additional Resources
- 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.