Version Control with Git
Key Concepts
- Repository
- Commit
- Branch
- Merge
- Clone
- Pull
- Push
Repository
A repository, or repo, is a directory where your project files are stored. It contains all the versions of your project, including the history of changes. You can create a new repository using the command git init
.
Example:
git init my-project
Commit
A commit is a snapshot of your project at a specific point in time. It records changes you've made to the files in your repository. Each commit has a unique identifier and a message describing the changes. You can create a commit using the command git commit
.
Example:
git add . git commit -m "Initial commit"
Branch
A branch is a separate line of development. It allows you to work on different features or versions of your project without affecting the main branch. You can create a new branch using the command git branch
and switch to it using git checkout
.
Example:
git branch feature-branch git checkout feature-branch
Merge
Merging combines changes from one branch into another. This is useful when you want to integrate the work done in a feature branch back into the main branch. You can merge branches using the command git merge
.
Example:
git checkout main git merge feature-branch
Clone
Cloning creates a copy of a remote repository on your local machine. This is useful when you want to start working on a project that is hosted on a remote server. You can clone a repository using the command git clone
.
Example:
git clone https://github.com/user/repo.git
Pull
Pulling fetches changes from a remote repository and merges them into your local branch. This is useful when you want to update your local copy with changes made by others. You can pull changes using the command git pull
.
Example:
git pull origin main
Push
Pushing sends your local commits to a remote repository. This is useful when you want to share your changes with others. You can push changes using the command git push
.
Example:
git push origin main
Examples and Analogies
Think of a repository as a project folder that keeps track of all your work. Commits are like saving different versions of a document. Branches are like creating separate drafts of a document. Merging is like combining two drafts into one. Cloning is like making a copy of a shared folder. Pulling is like downloading updates from the shared folder. Pushing is like uploading your changes to the shared folder.