Version Control with Git Explained
1. Repository
A repository, or "repo," is a directory where your project's files are stored, along with the entire history of changes. It can be local (on your computer) or remote (on a server like GitHub or GitLab).
Example: When you initialize a new Git repository in a directory using the command git init
, Git creates a hidden directory called ".git" where it stores all the version control information.
2. Commit
A commit is a snapshot of your project at a specific point in time. Each commit records the changes you've made to the files in your repository, along with a message describing those changes.
Example: After making changes to your code, you can create a commit using git commit -m "Added new feature"
. This command saves the changes and associates them with the message "Added new feature."
3. Branch
A branch is a parallel version of your repository. It allows you to work on different features or fixes without affecting the main codebase. Branches are useful for isolating changes and for collaboration.
Example: You can create a new branch called "feature-x" using git branch feature-x
. This branch can then be checked out and worked on independently of the main branch.
4. Merge
Merging combines the changes from one branch into another. This is typically done to integrate the work from a feature branch back into the main branch.
Example: After completing work on the "feature-x" branch, you can merge it into the main branch using git merge feature-x
. This command integrates the changes from "feature-x" into the main branch.
5. Clone
Cloning creates a copy of a remote repository on your local machine. This allows you to work on the project locally and push your changes back to the remote repository.
Example: You can clone a repository from GitHub using git clone https://github.com/username/repo.git
. This command downloads the entire repository to your local machine.
6. Push
Pushing sends your committed changes to a remote repository. This allows others to access your changes and collaborate on the project.
Example: After making commits locally, you can push them to the remote repository using git push origin main
. This command uploads your changes to the "main" branch on the remote server.
7. Pull
Pulling fetches the latest changes from a remote repository and merges them into your local branch. This ensures that your local copy is up-to-date with the remote repository.
Example: To update your local repository with the latest changes from the remote, you can use git pull origin main
. This command downloads the changes and merges them into your local branch.
8. Fetch
Fetching retrieves the latest changes from a remote repository without merging them into your local branch. This allows you to review the changes before deciding to merge them.
Example: You can fetch the latest changes from the remote repository using git fetch origin
. This command downloads the changes but does not merge them into your local branch.
9. Remote
A remote is a version of your repository that is hosted on a server, such as GitHub or GitLab. Remotes allow for collaboration and sharing of your project with others.
Example: You can add a remote to your local repository using git remote add origin https://github.com/username/repo.git
. This command associates your local repository with the remote repository on GitHub.
10. Tag
A tag is a reference to a specific point in the repository's history, often used to mark release points (e.g., v1.0). Tags are useful for marking significant milestones in your project.
Example: You can create a tag for version 1.0 using git tag v1.0
. This command marks the current commit as version 1.0, making it easy to refer back to this point in the future.
11. Rebase
Rebasing is the process of moving or combining a sequence of commits to a new base commit. It is often used to integrate changes from one branch into another in a cleaner way than merging.
Example: You can rebase the "feature-x" branch onto the "main" branch using git rebase main
. This command applies the commits from "feature-x" on top of the latest commit in the "main" branch, resulting in a linear history.