AB
A comprehensive guide to Git version control system, covering basic concepts, commands, branching, merging, and advanced features for effective code management and collaboration.
Git is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It allows multiple developers to collaborate on a project without interfering with each other’s work. Git tracks changes in the source code, maintaining a complete history of changes and enabling smooth teamwork.
Why is Git important?
Git is crucial for developers because it ensures:
A Version Control System (VCS) is a tool that helps manage changes to source code over time. It keeps track of every modification in a database, making it easy to revert to previous versions if mistakes occur.
Why do developers need a VCS?
In Git, “distributed” means every developer has a complete copy of the entire repository, including its full history. This differs from centralized systems where a single server hosts the repository.
push
and pull
commands.A local repository in Git is a copy of a project’s entire repository that is stored on a developer’s local machine. It includes all the project’s files, history, branches, and commits. Developers can perform all Git operations (such as commit, branch, merge, etc.) locally without needing a network connection. Changes can later be synchronized with a remote repository.
Key Components of Local Repository:
A remote repository in Git is a version of your project that is hosted on the internet or another network. It allows multiple developers to collaborate on the same project by pushing and pulling changes to and from the remote repository. Common platforms for hosting remote repositories include GitHub, GitLab, and Bitbucket. Developers synchronize changes with it via push
and pull
.
Always run sudo apt update
before installing any package in Ubuntu.
To install Git on Ubuntu:
sudo apt update
sudo apt install git
What does sudo apt-get install git-man
do?
It installs Git manual pages, which provide documentation for all Git commands, making it easier to learn and reference commands.
git init
. This creates a .git
folder to track changes.Example:
cd /path/to/project
git init
Verify Initialization:
Run ls -la
to see the .git
folder.
What is a Bare Repository?
The git init –bare command initializes a new, empty Git repository, but unlike a standard repository, it does not have a working directory. This type of repository is typically used as a remote repository to share changes between different developers.
Command:
git init --bare /path/to/repository.git
Why use a bare repository?
Why is this necessary?
Git associates each commit with a username and email, which is essential for collaboration and accountability.
Commands:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git status
Output of git status will be something like this:On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
Stage Changes:
git add .
This command adds all the files in the current directory to the staging area.
Commit Changes:
git commit -m "Your Commit Message"
This command commits the changes to the repository.
What is .gitignore
?
A .gitignore
file specifies files and directories Git should ignore. Example:
# Ignore all log files
*.log
# Ignore node_modules
node_modules/
Git logging Git logging refers to the process of viewing the commit history of a Git repository. It allows you to see what changes have been made, who made them, and when they were made. This is useful for tracking the progress of a project and understanding its history.
git log
git log --oneline
git log --graph
git log --author="Author Name"
git log --since="2 weeks ago"
git log --name-only
Git diff Git diff is used to show the differences between two commits, branches, or files. It is useful for reviewing changes, detecting conflicts, and ensuring that changes are made correctly.
git diff
git diff --cached
git diff <commit-hash>
In Git, a branch is a lightweight movable pointer to a commit. Branches are used to develop features, fix bugs, or experiment with new ideas in isolation from the main codebase. This allows multiple developers to work on different parts of a project simultaneously without interfering with each other’s work.
Why Use Branches?
git branch
git branch new-branch
git merge feature-branch
git checkout <branch-name>
git branch -d <branch-name>
git switch <branch-name>
Visualize Branches:
Merging branches in Git is the process of combining the changes from one branch into another. This is typically done to integrate new features, bug fixes, or updates from a development branch into the main branch (often called main or master).
Key points about merging branches:
git merge <branch-name>
git merge --no-ff <branch-name>
git merge --squash <branch-name>
In Git, HEAD is a reference to the current commit in the currently checked-out branch. It is essentially a pointer that tells you where you are in the repository’s history.
Key points about HEAD:
git checkout <commit-hash>
git checkout <branch-name>
git checkout -b <new-branch-name>
git reset --hard <commit-hash>
A “detached HEAD” state in Git occurs when the HEAD pointer is not pointing to a branch but rather to a specific commit. This means that you are no longer on any named branch and are instead working with a single commit directly.
Why avoid a detached HEAD?
Changes in this state are not tied to any branch and may be lost.
To start using a remote repository, follow these steps:
Create a Remote Repository:
https://github.com/username/repository.git
).Initialize a Local Repository:
git init
Link Your Local Repository to the Remote:
git remote add
command to connect your local repository to the remote one:git remote add origin <repository-url>
Push Local Changes to the Remote:
git push -u origin master
-u
mean?-u
flag sets the remote branch (origin/master
) as the upstream branch for your local branch. This means future git push
or git pull
commands will default to this branch without needing explicit specification.Cloning creates a complete copy of a remote repository on your local machine.
What does git clone
do?
git clone
command downloads the repository, including all branches, tags, and commit history, to your local machine.Steps to Clone:
git clone <repository-url>
What is a Pull Request (PR)?
What do git fetch
and git pull
do?
git fetch
:
git merge origin/master
git pull
:
git fetch
and git merge
in one step.git fetch origin
git merge origin/master
Or simply:
git pull
Summary
What are merge conflicts?
<<<<<<< HEAD
// Current branch changes
=======
// Incoming branch changes
>>>>>>> feature-branch
git add .
git commit -m "Resolved merge conflicts"
What is a fast-forward merge?
What is forking?
Key Points:
Rebasing in Git is the process of moving or combining a sequence of commits to a new base commit. It is an alternative to merging and is often used to maintain a clean, linear project history.
Rebase a branch onto another branch:
git checkout feature-branch
git rebase main
Interactive Rebase:
Interactive rebasing allows you to edit, reorder, squash, or drop commits during the process.
git rebase -i HEAD~n
Replace n
with the number of commits you want to rebase interactively.
Before rebasing, the history might look like this:
A---B---C feature-branch
\
D---E---F main
After rebasing the feature-branch
onto main
, it becomes:
D---E---F---A'---B'---C' main
In this scenario, the commits from the feature-branch
(A
, B
, C
) are replayed on top of the main
branch as new commits (A'
, B'
, C'
).
Cherry-picking in Git involves selecting specific commits from one branch and applying them to another branch. It allows you to pick and choose individual changes without merging entire branches.
git cherry-pick <commit-hash>
git cherry-pick <commit-hash1>..<commit-hash2>
Imagine you have the following branches:
Main branch:
D---E---F main
Feature branch:
A---B---C feature-branch
If you only want to apply commit B
from the feature-branch
to the main
branch, you can use:
git cherry-pick <hash-of-B>
After cherry-picking, the main
branch will include the B
commit:
D---E---F---B main
Interactive rebase allows you to modify the commit history by:
During rebasing or cherry-picking, Git may encounter conflicts when changes overlap. In such cases:
git add
to stage the resolved files.git rebase --continue
git cherry-pick --continue
Git is a powerful tool that enhances collaboration and code management for developers. By understanding its core concepts and commands, you can effectively track changes, work with branches, resolve conflicts, and contribute to projects. Whether you’re working solo or in a team, Git’s distributed nature provides flexibility and reliability for your development workflow.
Remember that Git has a learning curve, but mastering it will significantly improve your productivity and collaboration abilities as a developer. Start with the basics, practice regularly, and gradually explore more advanced features as your comfort level increases.