Git Basics Cheat Sheet
Git is a distributed version control system that helps manage changes to source code throughout the software development lifecycle. It's the standard way for multiple developers to work on a single project simultaneously.
In this short cheat sheet, I'll cover all the jargon and basics you'll need to know as a developer:
Git Jargon You Should Know:
- Repository (Repo): A database storing the history of a collection of files.
- Commit: A snapshot of the state of your files at a given time. Branch: A separate development line used to work on new features or fix bugs independently of the main project.
- Merge: The process of combining changes from different branches.
- Clone: A copy of a repository on your computer, allowing you to edit files in your environment.
- Pull: Fetching and merging changes from a remote repository to your local repository.
- Push: Sending your local changes to the remote repository.
Basic Git Commands
Setting up Git
Configure your user information:
git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
Creating a Repository
Initialize a new Git repository:
git init
Cloning a Repository
Create a local copy of a remote repository:
git clone https://github.com/username/repository.git
Working with Branches
List all branches:
git branch
Create a new branch:
git checkout -b new-branch-name
Switch to a branch:
git checkout existing-branch-name
Staging and Committing
Stage changes all changes files:
git add .
Stage specific files:
git add index.js test.js
Commit changes with a message:
git commit -m "Commit message"
Pushing Changes
Push your commits to the remote repository:
git push origin my-branch-name
Pulling Changes
Update your local repository to the newest commit from the remote repository:
git pull
Merging Branches
Merge another branch into your current branch:
git merge other-branch
Commit History
Show the commit history:
git log
Handling Merge Conflicts
Merge conflicts occur when Git cannot automatically reconcile changes in different branches. When a conflict arises, you have to resolve the conflicts in the affected files manually, then mark them as resolved by staging them:
git add filename
General Tips
- Make small, incremental commits that include related changes.
- Write clear, descriptive commit messages.
- Regularly pull changes from the remote repository to keep your local repository up-to-date. It will help you avoid nasty conflicts.
- Use branches for new features, fixes, or experiments.