Git
Git is a distributed version control system that allows multiple developers to work on the same codebase without interfering with one another.
Setup & Configuration
Installing Git
Git is a distributed version control system, to install it (linux-based systems):
sudo apt-get install gitConfiguring Git
Set username and email: This sets your identification for every commit.
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"List all global configurations: Shows your git configuration details.
git config --global --listUnset a specific configuration: For instance, to remove the globally set username.
git config --global --unset user.nameSetting Up Authentication for GitLab
SSH Authentication:
Repository Operations
Setting Up GitLab Repository
A step-by-step guide to set up a new project on GitLab:
Cloning a Repository
Copy a remote repository to your local machine:
Integrating an Existing Folder with GitLab
Link your local folder with a GitLab repository:
Regular Workflow & Common Commands
Synchronize with Remote Repository
Update your local branch with the latest changes from the remote repository:
Committing Changes
After making your desired changes, save them to your local repository and then push to the remote repository:
Branch Management
Creating, Renaming, and Deleting Branches
Handle multiple lines of development within a single repository:
Switching Between Branches & Commits
Move between different versions or branches within the repository:
Recovering Lost Files
Check Reflog
Create Temporary Branch
Recover Files
Merge or Cherry-Pick to Current Branch
Restoring Committed Branches
To find the last commit of the deleted branch:
For example, you might see an entry like:
Let's assume you want to recover a branch named feature-branch:
feature-branch:Conflict Resolution & Reversion
Handling Merge Conflicts
When merging or pulling, conflicts may occur. These steps guide you through resolution:
Reverting and Resetting Changes
git revert
git reset
Understanding HEAD and Its Notation
git reset with HEAD Notation
Best Practices
Merging dev into main and Resolving Conflicts
dev into main and Resolving ConflictsMerging Branches
Once you've finished development on the dev branch and it's ready for merging into main:
dev branch and it's ready for merging into main:Identifying and Resolving Conflicts
Identify the conflicted files:
Once you've resolved the conflict in the file:
After resolving all conflicts, continue the merge process with a commit:
After successfully merging and resolving conflicts, you'd want to update the remote repository:
Cleaning Up Local References in VSCode
Pruning Local Branch References
To remove stale branches from your local that no longer exist on the remote:
Advanced Operations
Stashing, Tagging, and Searching
Various advanced operations for better Git management:
Viewing Commit History & Using git blame
Examine the modification history of your repository:
Troubleshooting & Miscellaneous
Dealing with Authentication Issues
Switch between SSH and HTTPS, or troubleshoot common authentication issues:
Update Default Branch on GitLab
Change the primary branch of your GitLab repository:
Detailed Commit Inspection
Show Commit Details
Display changes associated with a specific commit:
Reflog
Shows a log of where your HEAD and branch references have been:
Working with Remotes
List Remote Repositories
Show all remote repositories connected to the local repository:
Add Remote Repository
Connect a new remote repository to the local repository:
Remove Remote Repository
Remove a connection to a remote repository:
Fetch from Remote
Download branches and/or tags from another repository, without making any changes to your local branches:
Patching
Creating a Patch
Create a patch file of the changes between two commits:
Apply a Patch
Apply changes from a patch file to the code:
Rebasing
Rebase
Apply a series of commits from one branch onto another, effectively re-writing history. Useful for a clean commit history but can be dangerous if not understood well:
Abort Rebase
Stop the rebase process and return to the initial state:
Continue Rebase After Conflict Resolution
Continue the rebase process after resolving merge conflicts:
Cleaning Up
Remove untracked files from the working directory. This doesn't remove untracked folders or files specified in .gitignore:
.gitignore:Prune
Remove objects that are no longer pointed to by any commit. This cleans up your local repository:
Submodules
Add a Submodule
Add another repository as a submodule in the current repository. Useful when using other projects or shared code:
Update Submodule
Update the registered submodules:
Bisect
Find by binary search the commit that introduced a bug:
Last updated