Git Basics for Contributors
Table of Contents
- 1. Branching
- What is a Branch?
- Common Branching Commands
- 2. Committing Changes
- What is a Commit?
- Common Committing Commands
- 3. Merging Branches
- What is Merging?
- Common Merging Commands
- 4. Pushing to Remote
- What Does it Mean to Push?
- Common Pushing Commands
- 5. Step-by-Step Workflow Examples
- What is a Git Workflow?
- Examples of Common Workflows
- 6. Best Practices for Collaboration
- Why Collaboration Practices Matter
- Examples of Good Git Habits
1. Branching
What is a branch?
A branch is like a copy of the project where you can make changes without affecting the main code.
You use branches to work on features or fixes safely and separately.
Common Branching Commands
This section covers Git commands used to create, switch, and manage branches:
- git branch
- git checkout
- git reset
- git reflog
git branch
List all local branches
Lists all local branches. The current branch is marked by an asterisk*.
Delete a merged branch
Deletes a local branch, but only if it has been merged.Force delete a branch
Force deletes a branch, even if it has not been merged. Use with caution, changes may be lost.git checkout
Switch to an existing branch
Switches from the current branch to the branch namedbranch-name.
Create and switch into new branch
Creates a new branch namednew-branch and switches to it immediately.
git reset
Reset working directory to the latest commit (hard reset)
Discards all uncommitted changes in the current branch and resets it to the latest commit.WARNING: This is destructive and cannot be undone.
Reset to a previous commit, but keep changes staged (soft reset)
MovesHEAD back one commit (or to a specific hash), but keeps your changes staged so you can recommit them.Useful when you want to edit a previous commit without losing your previous work.
General syntax
---soft: Keeps all changes staged (in the index)
- --mixed (default): Keeps changes but unstages them
- --hard: Discards all changes.
You can use:
- A commit hash (a1b2c3d), or
- A relative reference like HEAD~1 (1 commit before the current), HEAD~2 (2 commits before current), etc.
git reflog
Show recent Git history
Shows a log of your recent Git history, including branch switches, commits and resets. Useful for recovering deleted branches or lost commits.Example Use:
If you accidentally delete a branch or run a destructive command such as git reset --hard, you can use git reflog to find the commit before things went wrong.
First, view the reflog:
commit-id with the actual hash.This brings your project back to that point, even if the branch no longer exists.
2. Committing Changes
What is a commit?
A commit is like a snapshot of your project at a certain point. It saves the changes you've staged (i.e., marked as ready to commit) and adds them to your branch's history.
Common Committing Commands
This section covers Git commands used to stage changes and record commits:
- git add
- git commit
- git revert
git add
Stage a specific file
Stages a specific file to be included in the next commit. "Staged" means you've marked the file as ready to commit.Stage all changed files
Stages all changed files in the current directory.git commit
Create a commit with a message
Creates a new commit that saves all staged changes to your branch, along with a message describing what was changed.Example Use:
The -m flag lets you write a short, descriptive message. For example:
git commit -m "Fix typo in README"
git commit -m "Remove unused image assets"
git commit -m "Implement dark mode toggle"
Amending a commit
Replaces your most recent commit with a new one. Useful if you forgot to include a file or want to update the commit message.Only use this if the commit has not been pushed to a remote repository.
If you amend a commit that’s already been pushed, you’ll need to force-push, which can overwrite history and affect teammates.
Example Use:
If you forgot to include a file in your last commit:
Or, to correct a commit message:
git revert
Undo a specific commit by creating a new one
Creates a new commit that undoes the changes introduced by an earlier commit.Unlike
reset or amend, it does not modify history, so it's safe on shared branches.
3. Merging Branches
What is merging?
Merging is the process of combining changes from one branch into another.
You usually merge feature branches back into the main (or master) branch after development is done.
Git tries to automatically integrate the changes, if the same line of code were changed in both branches, a merge conflict may occur and will need to be resolved.
Common Merging Commands
This section covers Git commands used to merge branches and resolve conflicts:
- git merge
- git status
- git diff
- git log
Note: Branch names like feature-branch are just examples. You can name your branches based on the task you're working on.
git merge
Merge another branch into the current one
Merges the features fromfeature-branch into your current branch.
This is usually done after switching to the main branch.
Example Use:
If there are no conflicts, Git will automatically complete the merge.git status
Check the status of your working directory
Shows which files are staged, modified, or untracked.Useful during a merge to see if there are conflicts that need to be resolved.
git diff
See the differences between changes
Shows the differences between your current changes and the last commit.Example Use: You can also compare branches or commits:
This shows the changes infeature-branch compared to main.
git log
View your commit history
Displays a list of past commits in the current branch, including author, date, and message.Shorter, one-line summary per commit
Shows each commit as a single line for a more concise history.4. Pushing to Remote
What does it mean to "push"?
In Git, pushing means sending your local commits to a remote repository (like GitHub).
It updates the shared version of the project so others can see your changes.
You typically push after making and committing changes locally, once you're ready to share your work or back it up.
Common Pushing Commands
This section covers Git commands used to push changes and check your remote settings:
- git push
- git remote
git push
Push commits to the remote repository
Sends your local commits to the remote repository (e.g., GitHub) for the currently tracked branch.Only works if the branch is already linked to a remote.
Push and set upstream tracking
Pushes the specified branch and sets it to track the remote branch.This allows you to run
git push alone in the future without specifying a branch.
Example Use:
This pushes thefeature-login branch to GitHub and links it to the remote.
Force push (use with caution)
Replaces the remote branch with your local version, overwriting history.Only use when necessary (e.g., after an amended commit) and never on shared branches.
git remote
View configured remote repositories
Lists the remote connections linked to your local repository.This shows where your code will be pushed or pulled from.
Example Output:
origin https://github.com/username/repo-name.git (fetch)
origin https://github.com/username/repo-name.git (push)
5. Step-by-Step Workflow Examples
What is a Git workflow?
A Git workflow is a common sequence of commands developers use to make and share changes to a project.
The examples below show how multiple Git commands work together in a real-world scenario.
Note: Branch names like navbar-feature or bugfix/readme-link are just examples. You can name your branches based on the task you're working on.
Examples of Common Workflows
Starting a New Feature
git checkout -b navbar-feature
# Work on the navbar code
git add .
git commit -m "Add responsive navbar"
git push -u origin navbar-feature
Fixing a Small Bug
git checkout main
git pull
git checkout -b bugfix/readme-link
# Fix broken link in README
git add README.md
git commit -m "Fix broken link in README"
git push -u origin bugfix/readme-link
main, create a short-lived bugfix branch, and push your fix.
Merging a Finished Feature into Main
Merges your completed feature branch (navbar-feature) into the main branch and pushes the updated branch to GitHub.If there are no conflicts, Git will complete the merge automatically.
Updating the Last Commit
Updates your most recent commit with new changes.Because this rewrites Git history, you'll need to use
--force when pushing.Only do this if the commit hasn't been shared (i.e., pushed to a team-accessible remote).
Syncing with Main
Switches to themain branch and pulls the latest updates from the remote.This ensures your local copy is up to date before starting new work.
Reverting a Commit
Creates a new commit that undoes the changes from an earlier one, without rewriting history.This is the safest way to undo something on a shared branch.
6. Best Practices for Collaboration
Why Collaboration Practices Matter
When working with others on a shared codebase, following a few Git habits helps keep the project organized, readable and conflict-free. These practices make it easier for everyone to contribute smoothly while avoiding conflicts or accidental overwrites.
Examples of Good Git Habits
Use Feature Branches
Avoid committing directly to main.
Create a new branch for each new feature or fix:
Pull Often
Before starting new work (or before pushing), always pull the latest changes:
This pulls updates from the remote branch your current branch is tracking (usuallymain if you're on main).If you're not sure which branch you're on, run: Keeping your local copy current reduces the chance of merge conflicts and ensures you're building on the latest version of the code.
Write Clear Commit Messages
Use short, descriptive commit messages that explain what you've changed:
Avoid vague messages like"update" or "stuff", they don't help teammates understand your changes.
Communicate Before You Push Big Changes
If you're making major changes or rewriting history (e.g. with --force), let your team know first.
Misusing commands like git push --force can overwrite someone else's work.
Test Before You Commit
Make sure your code works before committing it, especially if it affects shared parts of the project.
Broken commits slow down everyone else.
Clean Up Old Branches
Once a feature is merged, delete the branch to keep things tidy:
For remote branches: