Git Fundamental Concepts
Git Fundamental Concepts
Let’s understand Git’s fundamental concepts that are essential to understanding how it works. Git is a distributed version control system used to track changes in source code during software development. It allows multiple developers to work together on a project, track changes, and collaborate more effectively. This guide covers the fundamental concepts that every Git user should understand.
Repositories
A Git repository (or “repo”) is where all the files and the history of changes are stored. There are two types of repositories:
- Local Repository: This is the Git repository stored on your local machine, where you perform your day-to-day work.
- Remote Repository: A repository stored on a server (e.g., GitHub, GitLab, Bitbucket), allowing others to access it and collaborate.
Git Workflow
Git follows a simple workflow where changes pass through three main stages before being committed:
- Working Directory: Where you make changes to files. These changes are untracked until you add them to the staging area.
- Staging Area: This is where you prepare changes before committing them. It lets you selectively choose which changes to include in a commit.
- Repository: This is the final storage area. Once you commit changes, they are saved in the repository’s history.
Commit
A commit is essentially a snapshot of your project’s files at a particular point in time.
- What it includes:
- Unique identifier (commit hash)
- Author (who made the change)
- Timestamp (when the change was made)
- Commit message (explanation of what changes were made)
After adding a new feature, you would use git add command to stage changes and git commit command to create a commit.
- Example:
git commit -m "Add login feature"
Branch
A branch in Git is a separate independent line of development, allowing you to work on different features or fixes without affecting the main project (usually the main
or master
branch).
- Usage:
- Create a new branch:
git branch feature-xyz
- Switch to it:
git checkout feature-xyz
orgit switch feature-xyz
- Create a new branch:
The branch command keeps your work separate from the main branch until it’s ready.
Merge
Merging is the process of integrating changes from one branch into another.
- Automatic merge: Git will attempt to merge changes without issues if there are no conflicts.
- Conflicts: If changes in both branches conflict, Git will ask you to manually resolve them.
- Example:
git checkout main
git merge feature-xyz
If there are no conflicts, Git combines the changes automatically.
Pull Request (PR)
A pull request is a request to merge changes from one branch (often a feature branch) into another (usually the main branch), usually accompanied by a review process.
- Commonly used on: Platforms like GitHub to review code and discuss changes before merging them.
- Example: On GitHub, after pushing your feature branch, you open a pull request for review.
Clone
Clone creates a copy of a remote repository on your machine, allowing you to work on the project offline.
- For Example:
git clone https://github.com/sampleuser/repository.git
- This command sets up your local working environment and connects it to the remote repo for syncing changes.
Push
Git push sends your local commits to a remote repository to share your changes with others. After committing changes locally, use the following command to upload your changes to the remote repository on GitHub. This updates the remote main branch with your local changes.
- Example:
git push origin main
Checkout
Checkout changes the current working directory to reflect the state of a different branch or commit.
- Example:
- Switch to another branch:
git checkout feature-xyz
- To view the state of your project at a specific commit, you can use:
git checkout <commit-hash>
- Switch to another branch:
Staging Area (Index)
The staging area is where you prepare changes before committing them.
- You decide which changes to include in the next commit by adding files to the staging area with
git add
. - Example:
git add file1.txt
to stagefile1.txt
git commit -m "Updated user profile"
Diff
The diff command shows the differences between files or commits.
- Useful for: Reviewing changes before making decisions to commit, merge, or resolve conflicts.
- Example:
git diff
shows the differences between the working directory and the latest commit.
- To see changes between your working directory and the last commit, use git diff. To compare two commits, use
git diff <commit1> <commit2>
Additional Concepts
- Remote Repository: A version of your project hosted on a server (e.g., GitHub, GitLab). You can
push
andpull
changes to and from this remote version. - Fetch: Retrieves the latest changes from a remote repository but doesn’t merge them into your local branch.
git fetch origin
- Rebase: An alternative to merging, where you “reapply” your changes on top of another branch, typically used to maintain a cleaner history.
Example Workflow
- Clone the repository:
git clone <repo-url>
- Create a new branch:
git checkout -b feature-xyz
- Make changes to files.
- Stage changes:
git add .
- Commit changes:
git commit -m "Add feature xyz"
- Push changes to remote:
git push origin feature-xyz
- Open a pull request (PR) for review.
- After approval, merge the PR to the main branch.