0% found this document useful (0 votes)
22 views

Session2 - Introduction To GitLab

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Session2 - Introduction To GitLab

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Git

Git, a powerful version control system used by developers all over the world. Git helps us track
changes in our code, collaborate effectively with others, and revert to previous versions if
needed. Whether you're a seasoned programmer or just starting out, Git is an essential tool to
have in your arsenal.

Version control is a system for


- tracking changes to files and directories over time.
- It allows you to revert to previous versions if needed.
- It helps multiple developers work on the same project simultaneously.
Agenda

Git , Command Line and Git Configurations

Commits, Branches and Merging

Creating Push to a remote repository

Clone , Fetch, Conflicts and Rebasing


Git
• Git is a distributed version control system created by Linus Torvalds, the creator of Linux.
• It is free, open-source, and widely used.
• Git is fast, efficient, and can handle large projects with ease.

Git is a bit different from traditional version control systems. It's a distributed system, which
means that every developer has a complete copy of the codebase. This allows for offline work
and makes collaboration much smoother. Git is also incredibly fast and efficient, even for large
projects with massive amounts of code.
Benefits of using Git

1 Version Control 2 Collaboration


Git meticulously tracks every change Git facilitates seamless collaboration among
made to your codebase. This allows developers.
you to revert to previous versions if
needed, ensuring a safety net for
development.

3 Security 4 Offline Work


Git leverages secure hashing Git empowers you to work offline. Since you have a
algorithms to protect your codebase. complete copy of the repository locally, you can make
This ensures the integrity of your data changes and commit them even without an internet
and prevents unauthorized connection. Once you're online, you can push your
modifications. local commits to the remote repository.
Git Installation
Windows Linux Mac OS
URL : https://git-scm.com/download/win URL: https://git-scm.com/download/linux https://git-scm.com/download/mac

Installer will install git and gitbash Ex : If using ubuntu Brew :brew install git
Gitbash emulate sort of Linux behavior apt-get install git MacPorts : sudo port install git
Command Line and GUI

Command Line Git GUI SourceTree or Extensions


like gitlens

Gitbash , terminal or command Git GUI comes with Git Source tree or extensions helps
line can be used to execute git installation and helps to to provide more insights on
commands perform git operations the time lines of commits,
options to revert etc.
Git Configuration

1 git config –-global user.name “your name”


Setting up Git for the first time, above command helps
to add your name

2 git config --global user.email "[email protected]


Setting up Git for the first time, above command helps
to add your email

3 git config --global core.editor "code --wait"


To use Visual Code as the default editor

git config --global merge.tool winmerge


4
git config --global mergetool.winmerge.path "C:/Program Files (x86)/WinMerge/WinMergeU.exe"
Merge tools helps when resolving the merge conflicts and can be configured as above. We can also
use the vimdiff, which comes by default in gitbash
Git work flow
Clone
• Clone: Create a local copy of a remote repository.
• Modify: Make changes to the file in your working directory.
• Add: Stage the changes you want to commit.
• Commit: Create a permanent snapshot of the changes with a Push Modify
message.
• Push: Upload your local commits to the remote repository.

Commit Add
Git Workflow Commands

git clone repo_url

git add file_name

git commit -m "message”

git push origin <origin>


Git Branching
Use Case Permissions Environments
Branch

Only leads/Managers
Master Source of truth UAT or performance envs
will be able to merge

Features are merged into


Develop All developers will
develop for cit testing dev or cit environments
have access to Merge

Stories assigned All developers will have


Feature will be developed access to create and Merged into develop after
in feature branch commit to feature branches unit testing

Any prod issues are All developers will have access to Merged into Master after
Hotfix/Bugfix fixed in hotfix or Create and MR’s will be unit testing
bugfix branches merged by leads or Managers

Created from Release or transition management Merged into beta or prod after
Release master to deploy teams can have access to deploy UAT and pert testing acceptance
into beta or prod
Git Merge
• Assume a scenario, where developer wants to merge changes from develop to master
• Steps involved are
• Make sure you are in develop branch and execute below commands to pull changes into
develop
• Git checkout develop
• Git pull origin develop
• Change the branch to the master
• Git checkout master
• Git merge develop

• Push changes to master


• Git push origin master
Creating Pull requests using CLI(hub)
• Assume a scenario, where developer wants to raise a MR or pull request from develop to
master
• Steps involved are
• Hub is a plugin or tool which helps to create the merge or pull requests
• Make sure you are in develop branch and execute below commands to pull changes into
develop
• Git checkout develop
• Git pull origin develop
• hub pull-request -b master -h develop -m "Pull request title" -m "Pull request description"
git fetch
• Git fetch downloads commits, files , references from a remote repository into your local
repository
• It updates your remote-tracking branches, say origin/main but does not merge them
with your local branches
• Commands
• git fetch origin
• git log origin/main

• Compare local branch with remote:


• git diff main origin/main.
• This command shows the differences between your local main branch and the
fetched origin/main branch.
• Safe to use as it does not affect your working directory.
• Allows you to review changes before merging.
git pull
• git pull is a combination of git fetch followed by git merge
• It fetches changes from a remote repository and immediately merges them into the
current branch.

• Pull changes from the remote repository and merge them into the current branch
• git pull origin main

• Downloads and merges changes into the current branch.


• Can cause merge conflicts that need to be resolved immediately.
• Directly updates your working directory.
Git merge and git rebase
• git merge integrates changes from one branch into another.
• It creates a new commit that combines the changes from both branches.
• The commit history retains the context of the branches, showing where the branches
diverged and merged.
• Assume, doing a git merge from feature to develop
A---B---C (target_branch, i.e develop )
\
D---E (source_branch, I,e feature)
• After git merge

A---B---C---------F (target_branch with merge commit)


\ /
D---E (source_branch)
Git generates a new commit that encapsulates the changes from both branches.
Git merge and git rebase
• git rebase re-applies commits from one branch on top of another base commit.
• It creates a linear history by transferring the completed work from one branch onto
another.
• This can make the commit history cleaner and easier to understand.
• Assume, doing a git rebase from feature to develop
A---B---C (target_branch, i.e develop )
\
D---E (source_branch, I,e feature)
• After git rebase

A---B---C---D'---E' (target_branch with rebased commits)


Git generates a new commit that encapsulates the changes from both branches.

By default, Git does not create an extra merge commit when merging a commit that is a
descendant of the current commit. Instead, the tip of the current branch is fast-forwarded.
When set to false, this variable tells Git to create an extra merge commit in such a case
(equivalent to giving the --no-ff option from the command line). When set to only, only such
fast-forward merges are allowed (equivalent to giving the --ff-only option from the
elopers are working on the same branch, using git rebase can create synchronization issues, as it requires everyone to force update ( git push --force) their branches, which ca

Why should we avoid git rebase

1 Rewriting History
Loss of Original Context: Rebasing rewrites commit history, which means that the original context of how
branches diverged and merged is lost. This can make it harder to understand the historical sequence of
events in a project.
Changing Commit SHA-1: Every rebased commit gets a new SHA-1 hash, which effectively creates new
commits. This can confuse collaborators who might have already based their work on the original commits.

2 Collaboration
Conflicts with Shared Branches: Rebasing public or shared branches can cause significant problems. If other
developers have based their work on commits that you have rebased, they will need to reconcile their work
with the new commit history, leading to potential merge conflicts and confusion.
Difficulties in Synchronization: When multiple developers are working on the same branch, using git rebase
can create synchronization issues, as it requires everyone to force update (git push --force) their branches,
which can be risky.
Why should we avoid git rebase
3 Impact on Code Reviews
Difficulty in Tracking Changes: Rebasing can make code reviews more difficult, as it changes commit hashes
and can make it harder for reviewers to track what changes have been made compared to a merge-based
workflow.
Loss of Incremental Context: Rebasing can strip away the incremental context provided by merge commits,
making it harder to understand the development process and review changes comprehensively.

4 Force Pushing

Risk of Data Loss: After rebasing, you often need to use git push --force to update the remote branch. Force
pushing can potentially overwrite changes on the remote repository if not used carefully, leading to data loss.
Override Protection Mechanisms: Force pushing bypasses the usual protection mechanisms that prevent
accidental overwrites, increasing the risk of mistakes.
Best Practices

1 Commit messages 2 Regular updates


Use clear, concise messages. Pull from remote frequently to stay updated.
Explain the why and what of the
changes.

3 Branch usage 4 Avoid rebasing


Create branches for features, hotfixes, Rebasing changes the commit history.
etc.

You might also like