Git is a distributed version control system that allows developers to track code changes and collaborate effectively with other developers. Git is designed to make the development process fast, flexible, and efficient, allowing multiple developers to work on a project simultaneously without conflicts.
Key Features:
Local and Global Repositories:
The repository is a codebase maintained by Git to track changes. There are two types of repositories: Local Repository and Global Repository.Global Repository:
A global repository, also known as a remote repository, is hosted on a server and is accessible to all team members. It acts as the central source of truth for the project, containing the official version of the codebase.Local Repository:
A local repository is stored on the developer's computer, usually a copy of the global repository. Developers can make changes, work on new issues in their local system, and then merge to the global repository.
Distributed Development:
Git is a distributed version control system, allowing each developer to have a local copy of the global repository. This facilitates developers to collaborate and work together on different aspects of the same application simultaneously.Branching and Merging:
Git allows developers to create copies of the root project via Branching, enabling them to work on features and bug fixes independently and then merge to the root branch.Staging:
Staging is selecting the changes that must be committed (saved) to the local repository. Git introduces a staging area where changes can be selectively added before committing. This allows for more controlled and organized commits.History Tracking:
Git keeps a detailed history of changes, including who made them, when they were made, and the commit messages. This helps in understanding the evolution of the codebase.Open Source
Git is a software tool that is available for free as an open-source. This means that anyone can access and modify its source code. Due to this, there is a large community of developers who actively contribute to different projects.
Git Commands
git init
Initializes the new git local repository in the project folder, and helps record the changes to the codebase.
git add [file-name]
Stages the specified files. multiple file paths can be specified
git commit -m "message"
Records the staged changes in the local Git repository and prepares them for pushing to the remote repository. "-m" is a flag used to specify the message for the respective commit.
git checkout -b [new-branch-name]
Creates a new branch with the specified name and switches to that branch. The new branch is essentially a copy of the current branch (the branch you were on when you executed the command).git checkout [existing-branch-name] (or) git switch [existing-branch-name]
Switches to the specified branch from the current branch.git log
Prints the commits made to the current branch.git log [branch-name]
Prints the commits made to the specified branch.git log --oneline
Concise way to view the commit history in a Git repository. When you execute this command in your terminal within a Git repository, it displays a list of commits in a condensed format, showing only the commit hash (the unique identifier for each commit) and the commit message on a single line.git branch
Prints all the local branches present.git branch -a
Prints all the global and local branches present.git show [commit-hash]
Views the committed changes in the specified commit hash of the commit.git checkout [commit-hash]
Used to checkout to a specific commit using its commit hash. This allows you to view the state of the repository at that particular point in time
Note: When you check out a specific commit by its hash, you enter a "Detached HEAD" state. This means that you're not on any branch. If you make new commits in this state, they will not be part of any branch.git checkout -b [new-branch-name] [commit-hash]
If you want to make changes based on that commit and keep them, you can create a new branch from that commit. This command creates a new branch
new-branch-name
starting from commit[commit-hash]
and checks it out.git remote add origin [remote-repository-path]
Links the remote repository to the global repository.git remote set-url origin [new-remote-repository-path]
Resets the remote repository URL path to the specified new path
git push origin [branch-name]
Pushes the code to the branch with the specified name in the global repository. If the global repository does not have a branch with the same name as the current branch of the local repository, a new branch is created.git pull origin [branch-name]
Pulls the code from the specified branch name.git clone [remote-repository-path]
Clones the global repository to the local machine as a local repository.git remote -v
This will show you the current URL(s) associated with your remote(s), typically named
origin
.git revert [commit hash]
Helps to revert your changes and go back to the previous state in your Git branch.
i
to enter insert mode in the editor, type your message, then save and exit by typing :wq
. Following these steps ensures a smooth and effective reversal of changes in your Git repository.GitHub
GitHub is a cloud-based platform that allows you to create repositories globally and synchronize them with local repositories. It facilitates features such as creating pull requests, merging, branching, collaborating, and access control with a user interface.
Pull Requests
A pull request is a mechanism for submitting contributions to a codebase. It is an action of proposing the changes to the code owners which are later reviewed and merged into the master branch.
Merging
The process of combining the changes from one branch to another branch is called merging. When the developer opens the pull request, the code is reviewed by the code owners or team members, once the code is verified for its correctness, the code is merged with the requested branch.
Branch Rules
Branch rule is a feature that GitHub provides that helps code owners add rules to their branches to avoid illegal merges.
Conclusion
Git is a powerful and widely used distributed version control system with key features such as distributed development, branching and merging, staging, history tracking, and open-source availability. It allows developers to work collaboratively on projects, manage code changes efficiently, and maintain a detailed history of the codebase evolution.
GitHub complements Git by providing a cloud-based platform for hosting repositories globally and facilitating collaboration through features like pull requests, merging, branching, and access control. Pull requests serve as a mechanism for proposing and reviewing code changes before merging them into the master branch. Merging is the process of combining changes from one branch to another, and GitHub provides tools like branch rules to enforce code quality and avoid illegal merges.
In summary, Git, along with GitHub, offers a comprehensive solution for version control and collaborative development, empowering developers to work efficiently and maintain code integrity throughout the development lifecycle.
For beginners embarking on version control for their software, this article serves as a mantra. If you have any questions or need further clarification, feel free to leave a comment – your feedback is welcome!