Git (Global Information Tracker) - Versioning
Git - A Distributed Version Control System
Git is a distributed version control system that was created by Linus Torvalds in 2005 for development of the Linux kernel. Git tracks changes made to a codebase over time and allows multiple developers to work on the same codebase simultaneously.
How Git Works
Git works by recording changes to a codebase as commits. Each commit captures a snapshot of the files and directories at a point in time. Git stores this information in a local database on the developer's machine. Developers can then push and pull commits to and from remote repositories to share changes and synchronize their local codebase with others.
Key Features of Git:
-
Distributed - Developers have a full copy of the Git repository and commits are stored locally. This allows work offline and without relying on a central server.
-
Branching and merging - Branches isolate work and changes can be merged between branches. This supports concurrent development.
-
Integrity - Git tracks checksums of file contents and directory structures to ensure integrity and prevent data corruption.
-
History tracking - Git tracks all changes in the history of a project, allowing revisiting any version.
Using Git
Some common Git commands and workflows include:
-
git clone - Clones/copies a remote repository to your local machine
-
git add - Stages file changes to be committed
-
git commit - Records changes in the local repository
-
git push - Pushes local commits to the remote repository
-
git pull - Pulls latest commits from remote repository
-
git branch - Manages branches to isolate work
-
git merge - Merges changes between branches
-
git log - Views commit history and changes
Rebase
Rebase is a feature that allows rewriting the commit history on a branch. It moves or combines a sequence of commits onto a new base. Rebase is used before merging branches to maintain a linear history and remove unnecessary commits.
To rebase:
git checkout feature-branch
git rebase main
This applies the commits from feature-branch
on top of main
, moving the main
commits onto the new base. Conflicts need to be resolved manually.
Rebase should not be used on commits pushed to public remote repositories, as it rewrites history. It is better suited for reorganizing local commits before sharing them.
Benefits of rebase:
- Reorganizes commits into a linear history
- Applies commits onto a new base
- Combines or removes commits
Rebase is powerful but confusing for beginners. Use with caution on remote branches.
Recover Droped Stash
- List the hash commits/stash that are in the cache
❯ git fsck --no-reflog | awk '/dangling commit/ {print $3}'
- Get commit/stash info to find the one you want to recover
❯ git show da820072f5484b93c3a197212c7a32ba7748a519
- Apply the found stash
❯ git stash apply da820072f5484b93c3a197212c7a32ba7748a519
Remove a file from your branch's GIT commit history
git filter-repo --path .env --invert-paths
Cherry Picking
Cherry-picking in Git allows you to selectively apply commits from one branch into another. This can be useful when you only want to take certain commits from a feature branch and merge them into a release branch, for example.
git cherry-pick 368263d1eff5
Without commit it:
git cherry-pick -n 368263d1eff5
Git Providers like GitHub
Git repository hosting providers like GitHub offer collaboration features on top of Git version control. These include:
-
Issue tracking - Track feature requests and bugs
-
Pull requests - Allows reviewing changes before merging
-
Access control - Manage user permissions
-
Visualizations - Graphs to visualize project activity
-
Project hosting - Hosts project websites and documentation
Conventional Commits
Conventional Commits are a widely adopted convention for standardizing commit messages in software projects. This practice helps maintain a clean and understandable commit history, making it easier to understand the changes made over time. The basic structure of a conventional commit consists of a type, an optional scope, and a concise description, separated by colons. For example: feat: add new feature X
.
The most common types are:
feat
: a new featurefix
: a bug fixdocs
: changes to documentationstyle
: formatting or code style changesrefactor
: code refactoring with no functional changestest
: addition or modification of testschore
: other changes that do not modify source code
Additionally, commit messages should be written in the present tense and clearly explain the reason for the change. Using conventional commits standardizes the development process, improves change traceability, and facilitates automated release note generation.
Conclusion
So in summary, Git is a distributed version control system that offers superior source code management. It is complemented by Git providers like GitHub that add collaborative functions like pull requests and issue tracking. Together they provide a complete version control and project hosting solution.
References
Learning more about GIT: https://learngitbranching.js.org