Git Basics¶
A quick reference to configure Git, clone repositories, and manage branches efficiently.
1. Initial configurations¶
Ensure that your Git version is 1.8 or newer:
Global configurations¶
Before using Git for the first time, set your global username and email (these will be attached to all your commits):
Check your configuration at any time:
Local configurations¶
If you contribute to multiple repositories or use different accounts, you can override the global configuration for a specific repository:
View your local configuration:
2. Cloning a repository¶
There are two main protocols for cloning GitHub repositories: HTTPS and SSH.
- HTTPS: simpler initial setup, but requires credential storage (token or keychain).
- SSH: recommended, uses key-based authentication and simplifies multi-account management.
You can define SSH aliases in ~/.ssh/config for different account or
organizations.
3. Working with branches¶
List branches¶
List all local and remote branches.
Create a branch¶
Create and switch to a new branch:
Switch branches¶
Move to another branch:
Change a branch name¶
Change the active branch name:
Delete a branch¶
Delete a branch that has already been merged:
Force delition (use with caution):
Delete a branch from remote:
4. Basic Workflow (Add -> Commit -> Push -> Pull)¶
Check the repository status¶
See which files have been added, modified, or deleted:
Stage changes¶
Add specific files or all modifications to the staging area:
Commit your changes¶
Create a new commit with a descriptive message:
Follow the Conventinal Commits format for consistency.
Amend the last commit (for example, typo fix a typo):
View commit history¶
List recent commits:
Show detailed information about a specific commit:
Push changes to the remote repository¶
Send your commits to GitHub:
Pull and update your local branch¶
Update your branch with the latest remote commits:
Use
--ff-onlyto ensure your history stays linear and clean. If Git wans about conflicts, resolve them, then commit the merge.
5. Git Tags¶
Tags are used to mark specific points in your project’s history, typically to identify releases (e.g., v1.0, v2.1.3).
They act like bookmarks to easily reference important commits.
Semantic Versioning Guide¶
| Part | When to increment | Example after change |
|---|---|---|
MAJOR |
Breaking changes: old code won't work | 1.0.0 → 2.0.0 |
MINOR |
New features, but backward-compatible | 1.0.0 → 1.1.0 |
PATCH |
Bug fixes, small changes only | 1.0.0 → 1.0.1 |
List tags¶
Display all tags in the repository:
Create a lighweight tag¶
Lightweight tags simply label a commit:
Lightweight tags are quick bookmarks but annotated tags are preferred for releases
Create an annotated tag¶
Annotated tags include a message, the author's name, and a timestap. They are recommended for official or shared releases.
View details about a tag¶
Show detailed information about a specific tag (annotation, author, date, and commit):
Push tags to the remote repository¶
Tags are not pushed automatically.
Push a specific tag:
Push all local tags:
Delete tags¶
Delete a tag locally:
Delete a tag on the remote repository:
6. Igniring files with .gitignore¶
The .gitignore file tells Git which files or directories to exclude from version control.
This helps keep your repository clean and prevents unnecessary or sensitive files from being committed.
Create a .gitignore file¶
At the root of your repository, create a file named .gitignore:
Then open it and list the files or patterns to ignore, for example:
Reference Material¶
Git
GitHub