Skip to content

Commit c2c33f0

Browse files
committed
docs: add a Git cheatsheet for stdlib
--- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent 886e85a commit c2c33f0

File tree

1 file changed

+302
-0
lines changed

1 file changed

+302
-0
lines changed
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Git Cheatsheet
22+
23+
> Git Cheatsheet for stdlib
24+
25+
## Introduction
26+
27+
Welcome to `stdlib`! [Git][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.
28+
29+
## Configuration
30+
31+
Before you start using [Git][git], it's important to introduce yourself. This step ensures that your commits are linked to your identity, making it easier to track contributions.
32+
33+
<!-- run-disable -->
34+
35+
```bash
36+
git config --global user.name "Your Name"
37+
git config --global user.email "[email protected]"
38+
```
39+
40+
> You only need to do this once.
41+
42+
To confirm that your identity is set correctly, run:
43+
44+
<!-- run-disable -->
45+
46+
```bash
47+
git config --global user.name
48+
git config --global user.email
49+
```
50+
51+
This should display the name and email you configured. If there's a mistake, simply re-run the configuration commands with the correct details.
52+
53+
## Cloning
54+
55+
To contribute to `stdlib`, you first need to [fork][github-fork] the repository on GitHub. This creates a copy of the repository under your GitHub account where you have write access.
56+
57+
Once you've forked the repository, clone your fork onto your local machine:
58+
59+
<!-- run-disable -->
60+
61+
```bash
62+
git clone https://github.com/YOUR_GITHUB_USERNAME/stdlib.git
63+
cd stdlib
64+
```
65+
66+
Since the official `stdlib` repository keeps updating, link it as the [upstream][git-remotes] remote to fetch the latest changes:
67+
68+
<!-- run-disable -->
69+
70+
```bash
71+
git remote add upstream https://github.com/stdlib-js/stdlib.git
72+
git fetch upstream
73+
```
74+
75+
> In [Git][git], a [remote][github-remote] is a reference to a repository. Your fork is called `origin`, while the official `stdlib` repository is referred to as `upstream`. Adding an upstream remote allows you to consistently fetch the latest updates from the original repository and incorporate them into your work.
76+
77+
## Branching
78+
79+
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:
80+
81+
<!-- run-disable -->
82+
83+
```bash
84+
git checkout -b feature/my-new-feature
85+
```
86+
87+
## Changes
88+
89+
Now comes the fun part of actually writing code! After making your changes, it's always good to check what’s modified:
90+
91+
<!-- run-disable -->
92+
93+
```bash
94+
git status
95+
```
96+
97+
Once you're happy with your changes, add them to the staging area for a final confirmation:
98+
99+
<!-- run-disable -->
100+
101+
```bash
102+
git add <file1> <file2> # Add specific files
103+
git add . # Add all changes
104+
```
105+
106+
Then, commit with a meaningful message:
107+
108+
<!-- run-disable -->
109+
110+
```bash
111+
git commit -m "feat: add support for new function"
112+
```
113+
114+
Try to keep your commit messages clear and concise! Adhering to stdlib's [Git Style Guide][github-style-guide] ensures a well-structured and understandable commit history.
115+
116+
## Pushing
117+
118+
Once your branch is ready, you need to push your **local** changes to your forked repository on GitHub using:
119+
120+
<!-- run-disable -->
121+
122+
```bash
123+
git push
124+
```
125+
126+
If this is the first time you’re pushing the branch, [Git][git] may prompt you to set an upstream branch. You can do this manually by running:
127+
128+
<!-- run-disable -->
129+
130+
```bash
131+
git push --set-upstream origin feature/my-new-feature
132+
```
133+
134+
> **Note:** This is a **one-time setup** for this branch. After this, you can simply use `git push` for future updates.
135+
136+
Once pushed, your changes will be available on GitHub, and you can proceed to create a pull request.
137+
138+
## Pull Request
139+
140+
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:
141+
142+
1. Go to your fork on GitHub.
143+
2. Click "Compare & pull request."
144+
3. Add a clear title and description (mention what you changed and why).
145+
146+
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.
147+
148+
## Syncing
149+
150+
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.
151+
152+
To update your local `develop` branch while making sure there are no unwanted changes, run:
153+
154+
<!-- run-disable -->
155+
156+
```bash
157+
git checkout develop
158+
git pull --ff-only upstream develop
159+
```
160+
161+
> **Why use `--ff-only`?**
162+
> This ensures your branch updates **only if no merge commits are needed**. If your `develop` branch has unexpected changes, this command will fail, alerting you that something is wrong.
163+
164+
If the above pull fails because you accidentally made changes to `develop`, you can **reset** it to match the official repository:
165+
166+
<!-- run-disable -->
167+
168+
```bash
169+
git checkout develop
170+
git reset --hard upstream/develop
171+
```
172+
173+
> **Warning:** This will delete any changes you made to `develop`. Make sure you don’t have important work in this branch before running this command.
174+
175+
After updating `develop`, you can push it to your fork to keep everything in sync:
176+
177+
<!-- run-disable -->
178+
179+
```bash
180+
git push origin develop
181+
```
182+
183+
## Integration
184+
185+
While 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.
186+
187+
### Rebase
188+
189+
Rebasing **moves** your commits on top of the latest `develop`, as if you had started working after the newest updates.
190+
191+
#### Steps to Rebase:
192+
193+
<!-- run-disable -->
194+
195+
```bash
196+
git checkout feature/my-branch
197+
git rebase develop
198+
```
199+
200+
If there are conflicts, resolve them and continue:
201+
202+
<!-- run-disable -->
203+
204+
```bash
205+
git add <resolved-file>
206+
git rebase --continue
207+
```
208+
209+
#### Example: How Rebase Works
210+
211+
Before rebasing (`develop` has new commits `C` and `D`):
212+
```
213+
X---Y---Z (feature/my-branch)
214+
/
215+
A---B---C---D (develop)
216+
```
217+
After rebasing (`X, Y, Z` are reapplied on top of `D`):
218+
```
219+
X'--Y'--Z' (feature/my-branch, rebased)
220+
/
221+
A---B---C---D (develop)
222+
```
223+
224+
### Merge
225+
226+
Merging **combines** your feature branch with `develop`, keeping both histories intact and adding a new merge commit.
227+
228+
#### Steps to Merge:
229+
230+
<!-- run-disable -->
231+
232+
```bash
233+
git checkout feature/my-branch
234+
git merge develop
235+
```
236+
237+
If there are conflicts, resolve them, then commit the merge. Finally, push the updated branch:
238+
239+
<!-- run-disable -->
240+
241+
```bash
242+
git push origin feature/my-branch
243+
```
244+
245+
> Alternatively, if you have a running PR and want to update that branch directly to the `stdlib` develop branch, comment `/stdlib merge` on the PR, wait for the bot to merge your branch automatically, and then run `git pull`.
246+
247+
#### Example: How Merge Works
248+
249+
Before merging (`develop` has new commits `C` and `D`):
250+
```
251+
X---Y---Z (feature/my-branch)
252+
/
253+
A---B---C---D (develop)
254+
```
255+
After merging (`W` is a new merge commit):
256+
```
257+
X---Y---Z---W (feature/my-branch, merged)
258+
/ /
259+
A---B---C-------D (develop)
260+
```
261+
262+
### Which one should you use?
263+
264+
Use **Rebase** if:
265+
- You want a clean, linear history.
266+
- You are working alone or sure that no one else depends on your commits.
267+
268+
Use **Merge** if:
269+
- You want a safer approach that doesn’t rewrite history.
270+
- You are unsure about rebase or are collaborating on the branch.
271+
272+
> **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.
273+
274+
## Conclusion
275+
276+
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!
277+
278+
To get started with your first contribution, check out the [Contributing Guide][stdlib-contributing] and [Development Guide][stdlib-development]. If you have any further questions, feel free to join our [Gitter][stdlib-gitter] channel to connect with the community and get support.
279+
280+
<section class="links">
281+
282+
[stdlib-contributing]: https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md
283+
284+
[stdlib-development]: https://github.com/stdlib-js/stdlib/blob/develop/docs/contributing/development.md
285+
286+
[stdlib-gitter]: https://app.gitter.im/#/room/#stdlib-js_stdlib:gitter.im
287+
288+
[git]: http://git-scm.com/
289+
290+
[github-remote]: https://help.github.com/articles/configuring-a-remote-for-a-fork/
291+
292+
[git-clone-depth]: https://git-scm.com/docs/git-clone#git-clone---depthltdepthgt
293+
294+
[git-remotes]: https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes
295+
296+
[github-fork]: https://help.github.com/articles/fork-a-repo/
297+
298+
[github-style-guide]: https://github.com/stdlib-js/stdlib/tree/develop/docs/style-guides/git#git-commit-messages
299+
300+
</section>
301+
302+
<!-- /.links -->

0 commit comments

Comments
 (0)