A cheatsheet for using Git to develop stdlib.
Welcome to stdlib! Git can feel overwhelming at times, but don't worry! This cheatsheet will walk you through the essential Git commands you'll need in a structured and easy-to-follow manner. Think of it as your go-to guide for a smooth and hassle-free workflow.
Before you start using Git, it's important to introduce yourself. This step ensures that your commits are linked to your identity, making it easier to track contributions.
$ git config --global user.name "Your Name"
$ git config --global user.email "your.email@example.com"You only need to do this once.
To confirm that your identity is set correctly, run:
$ git config --global user.name
$ git config --global user.emailThis should display the name and email you configured. If there's a mistake, simply re-run the configuration commands with the correct details.
To contribute to stdlib, you first need to fork the repository on GitHub. This creates a copy of the repository under your GitHub account where you have write access.
Once you've forked the repository, clone your fork onto your local machine:
$ git clone https://github.com/YOUR_GITHUB_USERNAME/stdlib.git
$ cd stdlibSince the official stdlib repository keeps updating, link it as the upstream remote to fetch the latest changes:
$ git remote add upstream https://github.com/stdlib-js/stdlib.git
$ git fetch upstreamIn Git, a remote is a reference to a repository. Your fork is called
origin, while the officialstdlibrepository is referred to asupstream. Adding an upstream remote allows you to consistently fetch the latest updates from the original repository and incorporate them into your work.
In stdlib, the develop branch is the primary branch for development. Instead of making changes directly in the develop branch of your forked repository, it's best to create a separate branch for each feature or fix. This keeps your develop branch clean and makes it easier to pull updates from the official repository. To create a new branch for your work, use:
$ git checkout -b feature/my-new-featureNow comes the fun part of actually writing code! After making your changes, it's always good to check what's modified:
$ git statusOnce you're happy with your changes, add them to the staging area for a final confirmation:
$ git add <file1> <file2> # Add specific files
$ git add . # Add all changesThen, commit with a meaningful message:
$ git commit -m "feat: add support for new function"For multi-line commit messages (like when you want to include a longer description), you can use:
$ git commitThis opens an editor where you can write something like:
feat: add support for new function
This adds the initial version of <function_name>, with support for <brief explanation>.
After writing your message:
- Vim: press
Esc, type:wq, then pressEnter - Nano:
Ctrl+O, thenEnter, thenCtrl+X - VS Code: save and close the editor
Multi-line commits are especially useful for giving extra context on why the change was made, or summarizing multiple related changes.
To make this even easier, you can also use:
$ make commitThis gives you an interactive prompt to help you craft a properly formatted commit message. Super handy!
Try to keep your commit messages clear and concise! Adhering to stdlib's Git Style Guide ensures a well-structured and understandable commit history.
Once your branch is ready, you need to push your local changes to your forked repository on GitHub using:
$ git pushIf this is the first time you're pushing the branch, Git may prompt you to set an upstream branch. You can do this manually by running:
$ git push --set-upstream origin feature/my-new-featureNote: This is a one-time setup for this branch. After this, you can simply use
git pushfor future updates.
Once pushed, your changes will be available on GitHub, and you can proceed to create a pull request.
A pull request (PR) is a way to suggest changes to a project. It lets maintainers review your work, give feedback, and approve the changes before adding them to the main repository. This is how you officially submit your contributions to stdlib after pushing your changes:
- Go to your fork on GitHub.
- Click "Compare & pull request."
- Add a clear title and description (mention what you changed and why).
Once you create your pull request, a review request will be automatically sent to the maintainers via stdlib-bot. Your code will then be reviewed, and you may need to make some tweaks before it gets merged.
The develop branch of your forked repository should always stay identical to the official stdlib repository. If you accidentally add commits to your local develop branch, you need to remove them before updating it.
To update your local develop branch while making sure there are no unwanted changes, run:
$ git checkout develop
$ git pull --ff-only upstream developWhy use
--ff-only? This ensures your branch updates only if no merge commits are needed. If yourdevelopbranch has unexpected changes, this command will fail, alerting you that something is wrong.
If the above pull fails because you accidentally made changes to develop, you can reset it to match the official repository:
$ git checkout develop
$ git reset --hard upstream/developWarning: This will delete any changes you made to
develop. Make sure you don't have important work in this branch before running this command.
After updating develop, you can push it to your fork to keep everything in sync:
$ git push origin developWhile working on a feature branch, new changes might be added to the develop branch after syncing it as described above. To ensure your feature branch stays updated with these latest changes, you need to integrate them before pushing your work. There are two main ways to do this: rebasing and merging.
Rebasing moves your commits on top of the latest develop, as if you had started working after the newest updates.
$ git checkout feature/my-branch
$ git rebase developIf there are conflicts, resolve them and continue:
$ git add <resolved-file>
$ git rebase --continueBefore rebasing (develop has new commits C and D):
X---Y---Z (feature/my-branch)
/
A---B---C---D (develop)
After rebasing (X, Y, Z are reapplied on top of D):
X'--Y'--Z' (feature/my-branch, rebased)
/
A---B---C---D (develop)
Merging combines your feature branch with develop, keeping both histories intact and adding a new merge commit.
$ git checkout feature/my-branch
$ git merge developIf there are conflicts, resolve them, then commit the merge. Finally, push the updated branch:
$ git push origin feature/my-branchAlternatively, if you have a running PR and want to update that branch directly to the
stdlibdevelop branch, comment/stdlib mergeon the PR, wait for the bot to merge your branch automatically, and then rungit pull.
Before merging (develop has new commits C and D):
X---Y---Z (feature/my-branch)
/
A---B---C---D (develop)
After merging (W is a new merge commit):
X---Y---Z---W (feature/my-branch, merged)
/ /
A---B---C-------D (develop)
Use Rebase if:
- You want a clean, linear history.
- You are working alone or sure that no one else depends on your commits.
Use Merge if:
- You want a safer approach that doesn't rewrite history.
- You are unsure about rebase or are collaborating on the branch.
When in doubt, use merge. It is safer and avoids potential conflicts caused by rewriting history. If you use the GitHub UI to update your branches, it also performs a merge.
Merge conflicts occur when Git can't automatically resolve differences between two commits (e.g., your branch and develop). This usually happens when you rebase or merge branches with conflicting changes. This is how a conflict looks in a file:
function isEven(x) {
<<<<<<< HEAD
return ( x % 2 ) === 0;
=======
return ( x & 1 ) === 0;
>>>>>>> feature/bitwise-check
}
Note: The
HEADsection represents your current branch (feature/bitwise-check), while theincomingsection represents the branch you're merging or rebasing (e.g.,develop).
To resolve a merge conflict:
- Open the file in your editor.
- Choose which changes to keep.
- Remove the conflict markers (
<<<<<<<,=======,>>>>>>>). - Save the file.
- Add the file to the staging area.
- Continue the rebase or merge process.
We will consider the conflict in the isEven function above. Let's say you want to keep your changes and use the bitwise AND operator (&) instead of the modulo operator (%). Here's how you can resolve the conflict:
-
Modify the file to look like this:
function isEven(x) { return ( x & 1 ) === 0; } -
Add the file to the staging area:
$ git add <file>
-
Continue the rebase or merge:
$ git rebase --continue # If rebasing $ git merge --continue # If merging
Tip: Many editors like VS Code highlight conflicts and even give you buttons to accept incoming or current changes. This can make resolving conflicts much easier.
Merge conflicts can be annoying, but they're a natural part of working with others. Take your time, and don't hesitate to ask for help if you're stuck.
Now that you have all the essential commands, let's put them together in a typical workflow:
Goal: Add a new function isEven to stdlib.
Assuming you've already cloned the repository and set up your identity:
-
Sync
develop:$ git checkout develop $ git pull --ff-only upstream develop $ git push origin develop # Optional: Update your fork -
Create a new branch:
$ git checkout -b feature/is-even
-
Make changes: This could involve adding the new function, writing tests, benchmarks, examples, updating documentation, etc.
-
Commit your changes:
$ git status # Optional: Check what's modified $ git add . $ git commit -m "feat: add isEven function"
If you prefer a multi-line commit message, use:
$ git commit
Then write your message like:
feat: add isEven function This adds the complete implementation of the isEven function, along with benchmarks, examples, tests, and documentation. The implementation is based on the modulo operator.Save and close the editor. As mentioned earlier, you can also use
make commitfor an interactive prompt. -
Push your branch:
$ git push --set-upstream origin feature/is-even
Note: As mentioned earlier, this is a one-time setup for this branch. After this, you can simply use
git pushfor future updates. -
Create a pull request: Go to your fork on GitHub, click "Compare & pull request", add a title and description, and create the PR.
-
Review and update: If you or the reviewers make changes to your PR through the GitHub UI, you need to update your local branch with those changes:
$ git pull origin feature/is-even # git pull also worksAdditionally, if new changes are added to the upstream
developbranch, you can integrate them into your feature branch using rebase or merge:$ git checkout develop $ git pull upstream develop # Update local develop first $ git push origin develop $ git checkout feature/is-even $ git merge developResolve any conflicts with the steps mentioned earlier, then continue the merge:
$ git merge --continue
Finally, push your changes:
$ git push origin feature/is-even
Note: When developing stdlib, we recommend using
mergeinstead ofrebaseonce a PR is open. Rebasing rewrites your branch history, which usually requires a force-push to update the remote branch. This can disrupt other contributors who are reviewing or collaborating on your PR. Since stdlib uses squash and merge for PRs, we don't require a clean, linear commit history. Merge commits are acceptable as long as your diff only contains relevant changes. -
Repeat: After resolving conflicts and updating your branch, you can continue making changes, committing, and pushing until your PR is ready to be merged.
Congratulations! You now have all the essential Git commands to navigate your workflow smoothly while contributing to stdlib. Whether you're fixing a bug, adding a feature, or just getting started, this cheatsheet will help you stay organized and avoid common pitfalls. Keep practicing, stay curious, and enjoy the process. Happy coding!
To get started with your first contribution, check out the Contributing Guide and Development Guide. If you have any further questions, feel free to join our Gitter channel to connect with the community and get support.