Introduction:
Git is a distributed version control system to tracking changes in different
versions of source code of a software. Linus Torvalds created it in 2005 and has
since become one of the most widely used version control systems in the world.
Git is particularly popular for its efficiency, speed, and flexibility.
Here are some key concepts and features of Git:
Version Control: The sort of changes tracked by Git include: modifications, additions, and deletions of files.
Distributed System: Git is distributed where each user has a local copy
of the entire repository, including its history thus enabling more flexibility
in development workflows. This results in collaboration without the need for a
constant connection to a central server.
Branching and Merging: With Git one can easily create branches, which are
essentially separate lines of development. Developers can work on different
features or bug fixes in parallel without interfering with each other. Git also
provides tools to merge branches back together.
Commits: Changes to the codebase are recorded as commits. Each commit
represents a specific set of changes, and they are accompanied by a commit
message describing the modifications made.
Remote Repositories: Git supports remote repositories, allowing
developers to collaborate on projects. Common remote repository hosting services
include GitHub, GitLab, and Bitbucket.
Pull Requests and Merge Requests: Platforms like GitHub, GitLab, and
Bitbucket introduce the concept of pull requests or merge requests. These are
mechanisms for proposing changes, reviewing code, and eventually merging the
changes into the main branch.
Open Source: Git is open source and can be freely used and modified. This
has contributed to its widespread adoption and a large community of users.
Speed: Git is designed to be fast and efficient. Local operations, such
as branching and merging, can be performed quickly. Overall, Git is a powerful
tool that helps developers manage and track changes in their codebase,
collaborate with others, and maintain a reliable version history.
What is the difference between Git and GitHub?
Git is the version control system itself, handling the tracking of changes locally on a developer's machine while GitHub is a web-based platform providing a centralized location for hosting Git repositories, offering collaboration features and tools to enhance the development workflow. While Git can be used independently of GitHub, GitHub leverages Git and provides a platform for teams to collaborate more effectively. Other similar services include GitLab and Bitbucket, each with its own features and capabilities.
How to create a new branch in Git?
Creating a new branch in Git is a common operation when working on a new feature or fixing a bug without affecting the main development branch. Here are the steps to create a new branch:
Method 1: Create and Switch to the New Branch
In Fig 1, you can see the command that can be used to switch to a new branch on Git after creating it.
Fig 1: Switching to New Branch |
This command is a combination of two commands:
git branch new-branch-name
(creates a new branch) and git checkout new-branch-name
(switches to the newly created branch).Method 2: Create a New Branch and Switching to the New Branch Separately
The method in Fig 2, involves two separate commands to create the branch and then switch to it. You can also use
git switch
if you have a more recent version of Git.Fig 2: Creating and Switching to New Branch Separately |
Method 3: Create a New Branch with Git Switch
The
git switch
command can be used to create and switch to a new branch in a single command see Fig 3 below. Fig 3: Using the Git switch |
Once you've created and switched to the new branch, you can start making changes specific to that branch without affecting the main development branch. Remember to commit your changes and push the new branch to a remote repository if needed. For example:
Fig 4: Commit and Push after making changes |
The above steps / methods will help you in creating a new branch in your Git repository and start working in it.
How to revert a commit in Git?
Here are two common methods: using
git revert
and using git reset
. Each method has its own use case, so choose the one that best fits your requirements.Using git revert
:
This method creates a new commit that undoes the changes made in a previous commit.
Fig 5: The git revert command |
The
git revert
command is useful when you want to maintain a linear history and create a new commit that undoes the changes introduced by the commit you want to revert. It's safer for shared repositories as it doesn't rewrite history.Using git reset
:
This method is more aggressive and should be used with caution, especially in shared repositories. It moves the branch pointer to a previous commit, effectively discarding the commits after it.
Fig 6: The git reset command |
Replace
HEAD^
with the appropriate commit reference if you want to reset to a different commit. Be cautious when using --hard
as it permanently discards changes.Choose the method based on whether you want to preserve the commit history (git revert
) or if you're okay with rewriting history and discarding the commits (git reset
). If you've already pushed the commit to a shared repository, it's generally safer to use git revert
to avoid conflicts with other collaborators.
As an amazon associate I earn from qualifying purchases.
Here are some links to good books on learning Git and GitHub on Amazon.
- Version Control with Git: Powerful Tools and Techniques for Collaborative Software Development (link)
- Learning Git: A Hands-On and Visual Guide to the Basics of Git (link)
- Learn Git in a Month of Lunches (link)
- Gitting Things Done: A Visual and Practical Guide to Git (link)
- Git for Programmers: Master Git for effective implementation of version control for your programming projects (link)
For website development Contact
0 Comments