Git Tutorial

What is a Version Control System?
Version control is the system of tracking and managing changes to software code.
It helps software teams to manage changes to source code over time.
Version control enables the current team to analyze the deletion, editing, and creation of datasets made since the original copy. It brings clarity to the development of the software.
What are the benefits of using version control?
● All team members are free to work on any file at any time with the Version Control System (VCS). Later on, VCS will allow the team to integrate all of the modifications into a single version.
● The VCS asks to provide a brief summary of what was changed every time we save a new version of the project. We also get to examine exactly what was modified in the content of the file. As a result, we will be able to see who made what changes to the project.
● Inside the VCS, all the previous variants and versions are properly stored. We will be able to request any version at any moment, and we will be able to retrieve a snapshot of the entire project at our fingertips.
● A VCS that is distributed, such as Git, lets all the team members retrieve a complete history of the project. This allows developers or other stakeholders to use the local Git repositories of any of the teammates even if the main server goes down at any point in time.
Indeed, here are the main advantages of using a Version Control System (VCS) in a bold format:
Collaboration
Version Tracking
Revert to Previous Versions
Branching
Conflict Resolution
Backup
Code Reviews
Traceability
Efficient Collaboration
Open Source
The following are the types of VCS :
Centralized version control system (CVCS).
Distributed/Decentralized version control system (DVCS).

The drawback of CVCS:
➢ It’s not locally available, meaning you always need to be connected to a network to perform any action.
➢ Since everything is centralized, if the central repository or server fails or crashes, you will lose entire data.
What are centralized version control systems (CVCS)?
It is a version control where there is a single central repository hosted on a server. This server is expected to have the latest code and expects all its clients to contribute by always being connected to the server always.

What are distributed version control systems (DVCS)?
Distributed revision control synchronizes repositories by transferring patches from peer to peer. There is no single central version of the codebase instead, each user can download a working copy and the full change history. Hence not required to be connected to the server all the time.
Example: GIT
What is Source Code Management?
It is a process through which we can store and manage any code. Developers write code, Testers write test cases and DevOps engineers write scripts. We can store and manage this code in Source Code Management. Different teams can store code simultaneously. It saves all changes separately. We can retrieve this code at any point in time. Ex: Bitbucket, Git
What is Git and why is it used?
GIT is a distributed version control system and source code management (SCM) system with an emphasis on handling small and large projects with speed and efficiency.
It is most suitable when multiple people are working on projects as a team is used for tracking the project changes and efficiently supports the collaboration of the development process.
With the help of the versioning system, the developer can identify who has made what changes and then run tests and fix bugs if any, and then do the necessary feature implementation
Advantage of git
One of the biggest advantages of Git is its branching capabilities
Pull Requests
Open Source and Community Support
Offline Access: You can work with Git offline, making commits and managing branches without needing a network connection.
Faster Release Cycle
GitHub is a web-based Git repository hosting service
GitHub Alternatives
BitBucket
GitBucket
| Git branch manages branches within a Git repository | A Git repository is a container that stores code and history. |
What is the difference between Git and GitHub?
GIT is a distributed version control system installed on local machines that allows developers to keep track of commit histories and support collaborative work.
Git files are stored on both the server and client, allowing users to work independently, make changes offline, and collaborate with others by merging changes
| GIT is a software | GitHub is a service |
| Git can manage source code History | GitHub is a cloud-based hosting service that lets you manage Git repositories |
| Git is installed locally on the system | GitHub is maintained by Microsoft |
| Git is focused on version control and code sharing | GitHub is hosted on the web |
| Git is a command-line tool | GitHub is a graphical user interface |
| Git has no user management feature | GitHub is focused on centralized source code hosting |
| SVN and Mercurial are the competitors | GitHub has a built-in user management feature |









Some Important Questions!
What are the Git commands that you use to commit changes to your remote repository?
Git Add
Git Commit
Git Push
What is the difference between git fetch and git push?
Git fetch retrieves changes from a remote repository to your local repository without merging.
Git push sends your local commits to a remote repository, updating its branches. Both are essential for collaboration and keeping local and remote repositories in sync.
What is the difference between git pull and git fetch?
Git fetch will download new commits from a remote repository to a local repository git pull does the same as git fetch and also merges the same into your local working files.
SVN vs GIT
SVN (Subversion): Centralized version control system. Single central repository. Slower, complex branching and merging, tracks changes per file.
Git: Distributed version control system. Each developer has a local repository. Faster, easy branching and merging, tracks changes per-commit. Adaptable workflows, extensive community, and tool support. Ideal for distributed and collaborative development.
What are .git and .gitignore?
.git Directory: The heart of a Git repository, containing all version history and configuration. Never manually modify it.
.gitignore File: A text file that specifies which files and directories Git should ignore when tracking changes. Helps keep irrelevant or sensitive files out of version control.
Git Workflow: Local to Remote Repository
Complete end-to-end workflow from local changes to pushing code to a remote repository
Step 1: Clone the Repository (If Not Already Local)
- Get the remote repo to your local machine:
git clone <repository-URL>
cd <repository-folder>
Step 2: Check Current Branch
git branch
- Ensures you are on the correct branch (e.g.,
mainordev).
Step 3: Make Changes Locally
- Edit or create files in your project folder.
Step 4: Check Status
- See which files are modified or untracked:
git status
Step 5: Stage Changes
- Stage specific files:
git add <file-name>
- Or stage all files:
git add .
Step 6: Commit Changes
- Commit with a descriptive message:
git commit -m "Add feature X or fix bug Y"
Step 7: Verify Remote Repository
- Check remote connections:
git remote -v
- If missing, add remote:
git remote add origin <repository-URL>
Step 8: Push Changes
- Push local commits to the remote branch:
git push origin <branch-name>
- Example:
git push origin main
Step 9: Verify in Remote
- Open GitHub/GitLab/Bitbucket to confirm your commits are updated.
Optional Steps
- Pull latest changes from remote before pushing to avoid conflicts:
git pull origin <branch-name>
- Resolve conflicts if any, then stage, commit, and push again.
🧠 Summary
“End-to-end Git workflow starts with cloning the repository locally. I make code changes, check the status, and stage files using
git add. Then I commit the changes with a message and push them to the remote repository usinggit push origin <branch>. Before pushing, I ensure my branch is up to date by pulling remote changes to avoid conflicts.
What are pre-commit hooks and post-commit hooks?
Pre-commit Hooks: These are scripts or actions that run before a Git commit is completed. They are used to enforce coding standards, perform code checks, or ensure that commit messages follow a specific format.
Post-commit Hooks: These are scripts or actions that run after a Git commit has been successfully made. They can be used for tasks like sending notifications, triggering automated builds, or updating issue trackers with commit information.
What are git webhooks?
When planning to cascade/notify new activities on the git server to a different tool like Jenkins, git webhook is used. the webhook contains information about the activity (ex: push on the server)
What is a ‘conflict’ in git?
A "conflict" in Git occurs when two different branches or commits have made conflicting changes to the same part of a file, making it unclear which change should be accepted. Git cannot automatically resolve such conflicts and requires manual intervention from the user to decide which version of the changes to keep. Conflicts typically happen during the process of merging or rebasing branches.
OR
A ‘conflict’ arises when the commit that has to be merged has some change in one place, and the current commit also has a change at the same place. Git will not be able to predict which change should take precedence.
How can conflict in git be resolved?
To resolve the conflict in git, edit the files to fix the conflicting changes and then add the resolved files by running “git add” After that to commit the repaired merge, run “git commit”.Git remembers that you are in the middle of a merger, so it sets the parents of the commit correctly.
OR
Identify the Conflict: Git will highlight conflicts in the affected files.
Open the File: Edit the conflicted file(s) in a text editor.
Manually Edit: Locate and remove conflict markers (e.g., <<<<<<<, =======, >>>>>>>) and keep desired changes.
Stage the File: Use git add to stage the resolved file.
Finish Merge/Rebase: Complete the merge or rebase (e.g., git commit, git rebase --continue).
Test and Commit: Verify code functionality and commit if needed.
Regenerate
What is git stash and talk about its use cases.
Git stash is a feature in Git that allows you to temporarily save your changes without committing them. It's useful when you need to switch branches or perform some other operation that requires a clean working directory.
Difference between git clone and git fork?
Git clone is a command used to create a copy of an existing Git repository on your local machine. It copies the entire repository, including all branches and commit history, allowing you to work on the project and contribute changes. Typically used for collaborating on an existing project as a contributor. git fork:
Git fork is not a Git command but a feature provided by Git hosting platforms like GitHub and GitLab. It allows you to create a personal copy (fork) of a repository hosted on these platforms.
what is amend in git?
In Git, "amend" refers to the action of modifying the most recent commit. It allows you to make changes to the last commit message or include additional changes without creating a new commit. This is done using the git commit --amend command. It's useful for making small corrections or adding missed changes to the last commit in your version history.
How to amend a commit in git?
Make your changes to the files.
Stage the changes with git add.
Use git commit --amend to update the last commit with your changes.
What is cherry-pick in git?
Cherry-pick allows you to pick a particular commit from a branch and insert into another branch. This is different from git merge in that, git merge will bring in all commits from branches while cherry-pick picks a specific commit only
How to create or initialize a Git Repository?
Git init
What language is used in GIT?
Git is primarily implemented in the C programming language, while its user interface and extensions can use various scripting languages.
Why is GIT better than Subversion (SVN)?
Git is an open-source version control system; it will allow you to run a ‘version' of a project. Multiple developers can check out, and upload changes and each change can then be attributed to a specific developer.
How do you undo the last commit?
git revert <commit_id>
How do I change the URL for a remote Git repository?
git remote set-url origin git://this.is.new.URL
What is the difference between git merge and git rebase?
git merge applies all unique commits from branch A into branch B in one commit with the final result.
git rebase gets all unique commits from both branches and applies them one by one.
git merge doesn’t rewrite commit history, just adds one new commit
git rebase rewrites commit history but doesn’t create extra commits for merging
What is another option for merging in Gi?
“Rebasing” is an alternative to merging in git.
What is the difference between ‘git remote’ and ‘git clone’?
Git remote add’ just creates an entry in your git config that specifies a name for a particular URL. While ‘git clone’ creates a new git repository by copying an existing one located at the URI.
Master or the main branch what is the other branch that is quite commonly used?
it's called a bug fix Branch or hotfix Branch
From which branch do you usually perform release?
Release branch
What is the branch that always is updated and up to date?
it's called your master trunk or main branch
Keywords
| git --version | It shows the version of Git installed on your machine |
| git init | It is used to initialize a new Git repository in a directory |
| git clone [repository URL] | This command is used to obtain a repository from an existing GitHub repo. |
| git add . | This command is used to add all the files to the staging area |
| git add [file name] | It will add all your files to the git staging area |
| git push | It will push all the local changes that you've made to the remote GitHub repository |
| git pull | It will pull ( fetch ) all the updated code from the remote branch and merge it with your local branch |
| git merge [branch name] | This is a command used to combine changes from one branch into another branch in a Git repository |
| git status | In simple terms, it will show you exactly which files/folders have been modified |
| git diff | This command in Git is used to display differences between different states of your Git repository. It can be used in various ways to compare changes between commits, branches, and working directories. |
| git commit -m | to set commit message |
| git log | It will list down the entire commit history i.e, all the commits that you've made till now |
| git branch branch name | This command is used to create a new branch in your local git repository |
| git branch | It will list down all the local branches that you've created |
| git branch -a | It will list down all the branches i.e., local branches + remote branches that are available for checkout |
| git branch -d branch name | to delete a branch |
| git branch -m <new name> | Used to rename the current working branch |
| git checkout branch name | to navigate a branch |
| git checkout . | to see the last commit |
| git checkout -id | to see that commit |
| git checkout master | to navigate the current position |
| git checkout -b <branch-name> | Creates a new branch and switches to the new one. |
| git stash | It's used to temporarily save and remove the changes that you've made on the working tree |
| git remote | It will give the name of the remote repository e.g, " origin " or " upstream " |
| git remote -v | It will give the name as well as the URL of the remote repository |
| git grep | command is used to search in the checkout branch and local files |
| git reset | Undo the changes to the local files, and restore the last commit. |
