![]() |
VOOZH | about |
Git is a distributed version control system (DVCS) designed to track changes in source code during software development. In 2005, Linus Torvalds, who is also the creator of the Linux kernel, developed it. Today, more than 40 million people use Git universally. If you are a developer, DevOps engineer, or any professional in the tech industry, knowing Git is vital.
Git is a distributed version control system (DVCS) that is used to track changes in source code during software development. It permits multiple developers to work on a project together without interrupting each other's changes. Git is especially popular for its speed, and ability to manage both small and large projects capably.
A Git repository (or repo) is like a file structure that stores all the files for a project. It continues track changes made to these files over time, helping teams work together evenly. Git can control both local repositories (on your own machine) and remote repositories (usually hosted on platforms like GitHub, GitLab, or Bitbucket), allowing teamwork and backup.
Git | GitHub |
|---|---|
Git is a version control system used to track changes in files over time | GitHub is a platform where Git repositories can be stored and shared |
It runs locally on your computer | It is a cloud-based service |
Git can be used offline, as it operates locally on your machine. | GitHub requires an internet connection because it is hosted on the web |
In Git, "origin" states to the default name offered to the remote repository from which local repository was cloned. Git origin is used as a reference to control fetches, pulls, and pushes.
The '.gitignore' file tells Git which files and folders to ignore when tracking changes. It is used to avoid attaching unneeded files (like logs, temporary files, or compiled code) to your repository. This saves repository clean and targeted on important files only.
A version control system (VCS) records the work of developers coordinating on projects. It keeps the history of code changes, permitting developers to add new code, fix bugs, and run tests securely. If required, they can restore a past working version, verifying project security.
The 'git push' command is used to share local repository changes to a remote repository. It changes the remote repository with the recent commits from the fixed local branch.
The 'git pull' command updates the current local branch with changes from a remote repository and combining it with a local repository.
The git clone forms a copy of a remote repository upon your local machine. Git clone downloads all files, branches, and history, enabling you to start working on the project or contribute to it. With git clone -b , you can download and work on an individual branch of a repository.
Using Git provides multiple advantages:
The git init develops a new, empty Git repository in the present directory, while 'git clone' copies an existing remote repository, containing all files and history, to a local directory.
The git add command marks changes in your project for the next commit. It tells Git which files to involve in the later update, making them ready to be saved in the repository. This is the early step in recording changes in the Git repository.
The 'git status' command shows the recent status of your Git repository. It tells you which files have changed, which ones are ready to be committed, and which ones are new and unobserved. This benefits you monitor your work's growth and see what changes want to be set up or committed.
A commit in Git denotes a snapshot of changes made to files in a repository. It grabs all the changes you have made to files—like additions, or deletions of files at a particular moment. Each commit has a unique message explaining what was done. This helps you track your project's history, undo changes if requisite, and work with others on the same project.
The git clean command is used to erase ignored files from the working directory of Git repository. Its motive is to clean up the workspace by deleting files that are not being saved by Git, checking a clean state with only observed files present.
Git usually manages merges automatically, but conflicts occur when two branches edit the same line or when one branch deletes a file that another edits.
In Git, the 'Index' (also called as the "Staging Area") is a place where alterations are temporarily store before committing them to the repository. It permits you to select and prepare specific alterations from your working directory before properly saving them as part of the project's history.
To change the preceding commit in Git, use 'git commit --amend' after making changes, stage them with 'git add' , and save with the editor.
The 'git checkout' helps you switch between branches or return files to a previous state in Git. Now, it is suggested to use 'git switch' for changing branches and 'git restore' to return files. These commands are more intent on their particular tasks for better clearness and capability.
To switch branches in Git, use 'git checkout ' to move to a present branch. On the other hand, use git switch in newer Git versions for the same objective. This permits you to work on different versions or features of your project stored in separate branches.
Git has two types of repositories
Git provides the git rm command to remove files from the working directory and the staging area. If you only want to remove a file from Git but keep it in the working directory, use:
git rm --cached <file_name>Aliases can be created to simplify Git commands using:
git config --global alias.<alias_name> '<git_command>'To rename the current branch:
git branch -m <new_branch_name>To rename a different branch:
git branch -m <old_branch_name> <new_branch_name>Command | Description |
|---|---|
git fetch | git fetch retrieves updates from the remote repository but does not merge them into the local branch, allowing you to review changes before integrating. |
git pull | git pull fetches updates from the remote repository and immediately merges them into the current local branch (equivalent to git fetch followed by git merge). |
The Git rebase is a process to combine alterations from one branch into another. It forms a linear history, avoiding merge commits. Use it to clean up commit history, keep a project history sequential, and make feature branches up-to-date before uniting.
Command | Description |
|---|---|
git remote |
|
git clone | git clone creates a local copy of an existing remote repository, including all files, branches, and commit history, so you can start working immediately. |
Teams can together work on distinct parts of the system and later combine their changes using pull requests. This way boosts team capability.
A Git bundle is a collective file that wraps all data from Git repository, such as commits, branches, and tags. It acts as a handy approach for relocating a repository offline or sharing upgrades when network connection is not available. To form a git bundle, perform the following command:
git bundle create <bundle_file> <refs> Here the some advantages of Git over SVN:
The git stash is a command used to temporarily save changes that are not yet ready to be committed. It allows developers to switch branches or work on something else without losing their progress. Stashed changes can be reapplied later using git stash pop or git stash apply.
Key Git Stash Commands:
git stashgit stash listgit stash popgit stash applygit stash drop stash@{n}To revert a commit that has been pushed and made public, follow these steps:
git checkout <branch-name>git loggit revert <commit-hash>git push origin <branch-name>Operation | Description |
|---|---|
git reset | Moves the HEAD pointer to a previous commit, removing changes from the commit history. It can modify staged and working directory changes. |
git revert | Creates a new commit that undoes changes from a previous commit without modifying commit history. It is safer when working in a shared repository. |
git reflog | git log |
|---|---|
Tracks movements of | Displays commit history of a repository |
Shows all references, even those not in branch history | Only shows commits in the current branch history |
Can help recover lost commits | Does not track changes outside commit history |
Used to restore lost branches or commits | Used to review past commits and changes |
The git tag -a command is used to create an annotated tag in Git. Annotated tags store additional metadata such as the tagger’s name, email, date, and a message describing the tag. These tags are useful for marking important points in a repository, such as software releases.
git tag -a v1.0 -m "Version 1.0 Release"To push annotated tags to a remote repository:
git push origin v1.0HEAD | Working Tree | Index (Staging Area) |
|---|---|---|
A reference to the current commit or branch you are working on | The directory where your project files reside | A file that acts as a buffer between the working tree and the repository |
Points to the current branch or commit you are on | Contains the actual project files and unstaged changes | Holds changes that are staged and ready to be committed |
Located in .git/HEAD | Your local project directory | Located in .git/index |
To resolve a conflict in Git
git merge | git rebase |
|---|---|
git merge combines two branches and creates a merge commit | git rebase reapplies commits from one branch on top of another |
Preserves both branches’ histories and structure | Rewrites commit history to make it linear |
Results in a merge commit | Creates a clean, straight history without merge commits |
Best when you want to keep full branch history | Best when you want a clean, linear history |
Common in collaborative environments | Useful for personal branches and cleanup before pushing |
Yes, a merge commit is created | No merge commit; history is rewritten |
Git is mainly developed using the C programming language. The core features and commands of Git, containing its data structures and algorithms, are applied in C. This choice of language confirms productivity, speed, and portability across distinct operating systems and platforms.
To add a file to the staging area in Git, use the command:
git add <file>This stages the file for the next commit. To stage all changes, use:
git add .git diff shows the differences between files or commits. It compares changes in the working directory, staging area, or between two commits.
A Git commit hash is a unique identifier (SHA-1) for each commit. It helps to reference and track specific commits in the repository. You can use it to check out or reset to a particular commit.
A detached HEAD state occurs when you check out a specific commit rather than a branch. In this state, commits are not attached to any branch, meaning they may be lost if not properly managed.
To delete a remote branch:
git push origin --delete <branch-name>This removes the branch from the remote repository.
Here’s the purpose of git cherry-pick in points:
The command git ls-files lists all files that are currently tracked by Git in the repository. It displays the files in both the working directory and the staging area. This command helps identify which files are being tracked and are ready to be committed. It does not show untracked files or files ignored by .gitignore.
Run:
git fetch --allThis command fetches all branches and their latest changes from all remotes without merging them.
The Git object model consists of four major types of objects:
git rebase moves or reapplies commits from one branch onto another, creating a linear commit history.
When to use:
A Git hook is a script that runs automatically at certain points in the Git workflow, like before or after a commit, merge, or push.
Use cases:
To handle large files with Git, use Git LFS (Large File Storage). It replaces large files in the repository with pointers, while the actual file content is stored externally.
Steps:
1. Install Git LFS:
git lfs install2. Track large files:
git lfs track "*.psd"3. Commit and push as usual. Git LFS will handle the large file storage automatically.
A bare repository in Git is a repository that doesn't have a working directory. It only contains the Git data, such as branches, tags, and commits, without the actual project files. Bare repositories are typically used as remote repositories where other developers push and pull changes.
Branching in Git permits forming separate lines of development. It allows users to work on features or fixes separately from the main codebase, helping parallel development and simpler integration of changes.
A Git submodule is a repository embedded inside another Git repository. It allows you to keep track of an external repository as part of your project. Submodules are useful when you want to include external libraries or dependencies while keeping their history separate.
We can use git reset to undo a commit:
git reset --soft HEAD~1git reset --hard HEAD~1To squash multiple commits into one:
1. Start an interactive rebase:
git rebase -i HEAD~<number-of-commits>2. In the editor, change pick to squash (or s) for the commits you want to combine.
3. Save and close the editor to squash the commits.
4. Git will open another editor to combine commit messages. Edit the message and save.
To reset to a previous commit without losing changes:
git reset --soft <commit-hash>To apply a patch:
1. Fetch the patch from the remote repository:
git fetch <remote> <branch>2. Apply the patch:
git apply <patch-file>To set a specific user for a project:
git config user.name "Your Name"
git config user.email "your.email@example.com"The Git reflog tracks the history of changes to the HEAD and branches, including changes that are not part of the commit history (e.g., resets, rebase). It allows you to recover lost commits by providing references to previous states.
Use:
git reflogWe can manage multiple remotes using:
git remote add <remote-name> <remote-url>git remote -vgit remote remove <remote-name>Git hooks allow you to run custom scripts at various points in the Git workflow. For pre-commit checks:
chmod +x .git/hooks/pre-commitOne way to manage large binary files without using Git LFS is by using Git Large File Storage (LFS), but if you can't use LFS, you can store large files in a separate repository and reference them using Git submodules.
To fetch changes from all remotes:
git fetch --allTo perform a Git bisect:
git bisect startgit bisect badgit bisect good <commit-hash>git bisect reset