0% found this document useful (0 votes)
12 views211 pages

Git Basics: Commands and Workflow Guide

git tutorial

Uploaded by

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

Git Basics: Commands and Workflow Guide

git tutorial

Uploaded by

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

Using Git

Git commands are run on the shell, also


known as the terminal
The shell:
is a program for executing commands

can be used to easily preview or inspect


files and directories

Directory = folder

INTRODUCTION TO GIT
Useful terminal commands
pwd

/home/repl/Documents

ls

archive [Link] finance_data_clean.csv finance_data_modified.csv

INTRODUCTION TO GIT
Changing directory
cd archive

pwd

/home/repl/Documents/archive

INTRODUCTION TO GIT
Checking Git version
git --version

git version 2.46.0

INTRODUCTION TO GIT
Creating repos
INTRODUCTION TO GIT

George Boorman
Curriculum Manager, DataCamp
What is a Git repo?
Git repo = directory containing files and sub-directories

INTRODUCTION TO GIT
Creating a new repo
git init mental-health-workspace

cd mental-health-workspace

git status

On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

INTRODUCTION TO GIT
Converting a project into a repo
Convert an existing directory into a Git repo

git init

Initialized empty Git repository in /home/repl/mental-health-workspace/.git/

INTRODUCTION TO GIT
What is being tracked?
git status

On branch main

No commits yet

Untracked files:
(use "git add <file>..." to include what will be committed)

data/
[Link]

nothing added to commit but untracked files present (use "git add" to track)

Git recognizes there are modified files not being tracked!

INTRODUCTION TO GIT
Nested repositories
Don't create a Git repo inside another Git
repo
Known as nested repos

There will be two .git directories

Which .git directory should be updated?

INTRODUCTION TO GIT
The Git workflow
Edit and save files on our computer

Add the file(s) to the Git staging area


Tracks what has been modified

Commit the files


Git takes a snapshot of the files at the point in time
Allows us to compare and revert files

INTRODUCTION TO GIT
Staging versus committing
Staging area Making a commit

INTRODUCTION TO GIT
Adding to the staging area
Adding a single file

git add [Link]

Adding all modified files

git add .

. = all files in the current directory and sub-directories

INTRODUCTION TO GIT
Making a commit
git commit -m "Adding a README."

[main cb33c18] Adding a README.


1 file changed, 1 insertion(+)
create mode 100644 [Link]

-m Allows a log message without opening a text editor

Log message is useful for reference

Best practice = short and concise

INTRODUCTION TO GIT
The commit structure
Git commits have three parts:

1. Commit
contains the metadata - author, log message, commit time

2. Tree
tracks the names and locations of files and directories in the repo

like a dictionary - mapping keys to files/directories


3. Blob
Binary Large OBject
may contain data of any kind
a compressed snapshot of a file's contents

INTRODUCTION TO GIT
Visualizing the commit structure

INTRODUCTION TO GIT
Git hash
Last commit: b22eb75a82a68b9c0f1c45b9f5a9b7abe281683a

Pseudo-random number generator—hash function

Hashes allow data sharing between repos


If two files are the same,
then their hashes are the same

Git only needs to compare hashes

INTRODUCTION TO GIT
Git log
git log

Shows commits from newest to oldest

commit ad8accfe94cb924444c488132bdef7c54b9bca68
Author: Rep Loop <repl@[Link]>
Date: Wed Jul 24 07:48:27 2022 +0000

Added reminder to cite funding sources.


:

Press space to show more recent commits


Press q to quit the log and return to the terminal

INTRODUCTION TO GIT
Projects grow!
Larger project = more commits = larger output

INTRODUCTION TO GIT
Restricting the number of commits
We can restrict the number of commits displayed using - :

Restrict to the 3 most recent commits

git log -3

INTRODUCTION TO GIT
Restricting the file
To only look at the commit history of one file:

git log [Link]

INTRODUCTION TO GIT
Combining techniques
cd data

git log -2 mental_health_survey.csv

INTRODUCTION TO GIT
git log output
commit f35b9487c063d3facc853c1789b0b77087a859fa
Author: Rep Loop <repl@[Link]>
Date: Fri Jul 26 15:14:32 2024 +0000

Add two new participants' data.

commit 7f71eadea60bf38f53c8696d23f8314d85342aaf
Author: Rep Loop <repl@[Link]>
Date: Fri Jul 19 09:58:21 2024 +0000

Adding fresh data for the survey.

INTRODUCTION TO GIT
Customizing the date range
Restrict git log by date:

git log --since='Month Day Year'

Commits since 2nd April 2024:

git log --since='Apr 2 2024'

Commits between 2nd and 11th April:

git log --since='Apr 2 2024' --until='Apr 11 2024'

INTRODUCTION TO GIT
Acceptable filter formats
Natural language Date format
"2 weeks ago" "07-15-2024"

"3 months ago"


Recommend ISO Format 6801
"YYYY-MM-DD"
"yesterday"
Check system settings for compatibility,
e.g., 12-06-2024 could be 6th Dec or
12th June !

"15 Jul 2024" or "15 July 2024"


Invalid: "15 Jul, 2024"

1 [Link]

INTRODUCTION TO GIT
Finding a particular commit
git log

Only need the first 8-10 characters of the hash

git show c27fa856

1 [Link]

INTRODUCTION TO GIT
git show output

INTRODUCTION TO GIT
git show output

INTRODUCTION TO GIT
git diff
git diff - Difference between versions

Compare last committed version with latest version not in the staging area

git diff [Link]

INTRODUCTION TO GIT
git diff output

INTRODUCTION TO GIT
Comparing to a staged file
Add [Link] to the staging area

git add [Link]

Compare last committed version of [Link] with the version in the staging area

git diff --staged [Link]

INTRODUCTION TO GIT
Comparing to a staged file

INTRODUCTION TO GIT
Comparing multiple staged files
Compare all staged files to versions in the last commit

git diff --staged

INTRODUCTION TO GIT
Comparing two commits
Find the commit hashes

git log

Compare them

git diff 35f4b4d 186398f

What changed from first hash to second hash


Put most recent hash second
State in latest commit = HEAD
Compare second most recent with the most recent commit

git diff HEAD~1 HEAD

INTRODUCTION TO GIT
Comparing two commits

INTRODUCTION TO GIT
Summary
Command Function
git diff Show changes between all unstaged files and the latest commit

git diff [Link] Show changes between an unstaged file and the latest commit

git diff --staged Show changes between all staged files and the latest commit

git diff --staged


Show changes between a staged file and the latest commit
[Link]

git diff 35f4b4d


Show changes between two commits using hashes
186398f

git diff HEAD~1 HEAD~2


Show changes between two commits using HEAD instead of
commit hashes

INTRODUCTION TO GIT
Making an error

INTRODUCTION TO GIT
Reverting files
Restoring a repo to the state prior to the previous commit

git revert
Reinstates previous versions and makes a commit

Restores all files updated in the given commit


a845edcb , ebe93178 , etc

HEAD , HEAD~1 , etc

git revert HEAD

INTRODUCTION TO GIT
Reverting files
git revert HEAD

Save: Ctrl + O , then Enter


Exit: Ctrl + X

INTRODUCTION TO GIT
Reverting files
[main 7d11f79] Revert "Adding fresh data for the survey."
Date: Tue Jul 30 14:17:56 2024 +0000
1 file changed, 3 deletions(-)

INTRODUCTION TO GIT
git revert flags
Avoid opening the text editor

git revert --no-edit HEAD

Revert without committing (bring files into the staging area)

git revert -n HEAD

INTRODUCTION TO GIT
Revert a single file
git revert works on commits, not individual files

To revert a single file:


git checkout

Use commit hash or HEAD syntax

git checkout HEAD~1 -- [Link]

INTRODUCTION TO GIT
Checking the checkout
git status

On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)

modified: [Link]

INTRODUCTION TO GIT
Making a commit
git commit -m "Checkout previous version of [Link]"

[main daa6c87] Checkout previous version of [Link]


1 file changed, 1 deletion(-)

INTRODUCTION TO GIT
Unstaging a file

INTRODUCTION TO GIT
Unstaging a single file
To unstage a single file:

git restore --staged summary_statistics.csv

Edit the file

git add summary_statistics.csv

git commit -m "Adding age summary statistics"

INTRODUCTION TO GIT
Unstaging all files
To unstage all files:

git restore --staged

INTRODUCTION TO GIT
Summary
Command Result
git revert HEAD Revert all files from a given commit

git revert HEAD --no-edit Revert without opening a text editor

git revert HEAD -n Revert without making a new commit

git checkout HEAD~1 -- [Link] Revert a single file from the previous commit

git restore --staged [Link] Remove a single file from the staging area

git restore --staged Remove all files from the staging area

INTRODUCTION TO GIT
Congratulations
INTRODUCTION TO GIT

George Boorman
Curriculum Manager, DataCamp
Chapter 1 recap
Benefits and applications of Git for version control

Navigating the terminal - ls , cd


How to initiate a Git project - git init

How to use Git to track your files - git add . , git commit -m

INTRODUCTION TO GIT
Chapter 2 recap
How Git stores data
1. Commit
2. Tree

3. Blob
git log
3

--since

--until

git show c27fa856

INTRODUCTION TO GIT
Chapter 2 recap
Command Function

git diff [Link] Show changes between unstaged file and the latest
commit
git diff --staged
Show changes between staged file and the latest commit
[Link]

git diff 35f4b4d 186398f Show changes between two commits using commit hashes

git diff HEAD~1 HEAD~2 Show changes between two commits using HEAD syntax

INTRODUCTION TO GIT
Chapter 2 recap
Command Result
git revert HEAD Revert all files from a given commit

git revert HEAD --no-edit Revert without opening a text editor

git revert HEAD -n Revert without making a new commit

git checkout HEAD~1 -- [Link] Revert a single file from the previous commit

git restore --staged [Link] Remove a single file from the staging area

git restore --staged Remove all files from the staging area

INTRODUCTION TO GIT
What's next?
Branches

Remote repos
Rebasing

Bisecting
Submodules

INTRODUCTION TO GIT
What we will cover
Branches

Remotes

Conflicts

INTERMEDIATE GIT
What you should know
How Git stores data

How to create repos


How to make commits

How to compare versions


How to revert versions

INTERMEDIATE GIT
Branches
Branch = an individual version of a repo

Git uses branches to systematically track


multiple versions of files

In each branch:
Some files might be the same
Others might be different

Some may not exist at all

INTERMEDIATE GIT
Why use branches?
Live system Feature development

Works as expected Might encounter issues during development


Default branch = main and testing
Doesn't affect the live system

INTERMEDIATE GIT
Why use branches?
Multiple developers can work on a project simultaneously

Compare the state of a repo between branches

Combine contents, pushing new features to a live system

Each branch should have a specific purpose

INTERMEDIATE GIT
Visualizing branches

INTERMEDIATE GIT
Branching off

INTERMEDIATE GIT
Merging back into main

INTERMEDIATE GIT
Fixing a bug

INTERMEDIATE GIT
Identifying branches
Listing all branches

git branch

main
* ai-assistant

* = current branch

INTERMEDIATE GIT
Switching between branches
git switch main

Switched to branch 'main'

INTERMEDIATE GIT
Creating a new branch
Create a new branch called speed-test

git branch speed-test

Move to the speed-test branch

git switch speed-test

Switched to branch 'speed-test'

Create a new branch called speed-test and switch to it

git switch -c speed-test

Switched to a new branch 'speed-test'

INTERMEDIATE GIT
Terminology
Creating a new branch = "branching off"

Creating speed-test from main = "branching off main "

INTERMEDIATE GIT
Diff recap
Command Function
git diff Show changes between all unstaged files and the latest commit

git diff [Link] Show changes between an unstaged file and the latest commit

git diff --staged Show changes between all staged files and the latest commit

git diff --staged


Show changes between a staged file and the latest commit
[Link]

git diff 35f4b4d


Show changes between two commits using hashes
186398f

git diff HEAD~1 HEAD~2


Show changes between two commits using HEAD instead of
commit hashes

INTERMEDIATE GIT
Comparing branches
git diff main summary-statistics

INTERMEDIATE GIT
git diff output

INTERMEDIATE GIT
git diff output

INTERMEDIATE GIT
Navigating large git outputs
Can produce large outputs!

Press space to progress through and q to exit

INTERMEDIATE GIT
Modifying branches
git branch

main
* feature_dev

feature_dev

Need another branch for a second new feature being developed

Solution - rename feature_dev


Renaming a branch

git branch -m

INTERMEDIATE GIT
Renaming a branch
git branch

main
* feature_dev

feature_dev

Need another branch for a second new feature being developed

Solution - rename feature_dev


Renaming a branch

git branch -m feature_dev chatbot

INTERMEDIATE GIT
Checking our branches
git branch

main
* chatbot

INTERMEDIATE GIT
Deleting a branch
Large projects can have many branches

Delete branches once we are finished with them


Delete the chatbot branch with -d flag

git branch -d chatbot

Deleted branch chatbot (was 3edb989).

INTERMEDIATE GIT
Deleting a branch that hasn't been merged
If chatbot hasn't been merged to main , git branch -d chatbot will produce an error

error: The branch 'chatbot' is not fully merged.


If you are sure you want to delete it, run 'git branch -D chatbot'.

Delete with -D flag

git branch -D chatbot

Deleted branch chatbot (was 3edb989).

Difficult, but not impossible, to recover deleted branches


Be sure we don't need the branch any more before deleting!

INTERMEDIATE GIT
Summary
Command Function

git diff main chatbot


Compare the state of the main and chatbot
branches
git branch List all branches

git branch -m old_name


Rename branch called old_name to new_name
new_name

git branch -d chatbot Delete chatbot branch, which has been merged

git branch -D chatbot Delete chatbot branch, which has not been merged

INTERMEDIATE GIT
The purpose of branches
Each branch should have a particular purpose
Developing a new feature
Debugging an error

Once the task is complete, we incorporate the changes into production


Typically the main branch - "ground truth"

INTERMEDIATE GIT
Source and destination
When merging two branches:
the last commits from each branch are called parent commits
source —the branch we want to merge from

destination —the branch we want to merge into

When merging ai-assistant into main :


ai-assistant = source

main = destination

INTERMEDIATE GIT
Merging branches
Move to the destination branch:

git switch main

git merge source

From main , to merge ai-assistant into main :

git merge ai-assistant

From another branch: git merge source destination

git merge ai-assistant main

INTERMEDIATE GIT
Git merge output

INTERMEDIATE GIT
Git merge output

Parent commits

INTERMEDIATE GIT
Git merge output

Linear commit history: branched off main to create ai-assistant

Fast-forward: point main to the last commit in the ai-assistant branch

INTERMEDIATE GIT
Git merge output

INTERMEDIATE GIT
Git merge output

INTERMEDIATE GIT
Conflicts
Conflict
Inability to resolve differences in the contents of one or more files between branches
Edit the same file in two branches

Try to merge
Git doesn't know what version to keep

Conflict!

INTERMEDIATE GIT
Conflicting versions of [Link]
documentation branch main branch
# Contents and usage # Contents and usage

This repo contains source code This repo contains source code
for the DataCamp website. for the DataCamp website.

It also contains source code for an It is for internal use only,


AI-Assistant (recommendation system) external access is prohibited.
that takes prompts from learners and
returns suggested content
that they might be interested in.

It is for internal use only,


external access is prohibited.

INTERMEDIATE GIT
Merging
From the main branch

git merge documentation

Auto-merging [Link]
CONFLICT (add/add): Merge conflict in [Link]
Automatic merge failed; fix conflicts and then commit the result.

INTERMEDIATE GIT
Opening the file
nano [Link]

INTERMEDIATE GIT
Git conflict syntax

INTERMEDIATE GIT
Resolving the conflict

Save: Ctrl + O (not Ctrl + 0 ), then Enter


Exit: Ctrl + X

INTERMEDIATE GIT
Merging the branches
Merging now that the conflict is resolved

git add [Link]

git commit -m "Resolving [Link] conflict"

git merge documentation

Already up to date.

Prevention is better than cure!

INTERMEDIATE GIT
Remote repo

INTERMEDIATE GIT
Why use remote repos?
Benefits of remote repos
Everything is backed up
Collaboration, regardless of location

INTERMEDIATE GIT
Cloning a repo
Repo copies on our local computer = local remotes

Making copies = cloning

git clone path-to-project-repo

Cloning a local project

git clone /home/george/repo

Cloning and naming a local project

git clone /home/george/repo new_repo

INTERMEDIATE GIT
Cloning a remote
Remote repos are generally stored in an online hosting service
e.g., GitHub, Bitbucket, or GitLab
If we have an account:
We can clone a remote repo on to our local computer

git clone URL

git clone [Link]

INTERMEDIATE GIT
Identifying a remote
When cloning a repo
Git remembers where the original was
Git stores a remote tag in the new repo's configuration

List all remotes associated with the repo

git remote

datacamp

INTERMEDIATE GIT
Getting more information
Get more information about the remote(s)

git remote -v

datacamp [Link] (fetch)


datacamp [Link] (pull)

INTERMEDIATE GIT
Creating a remote
When cloning, Git will automatically name the remote origin

git remote add name URL

Create a remote called george

git remote add george [Link]

Defining remote names is useful for merging

INTERMEDIATE GIT
Remote vs. local

INTERMEDIATE GIT
Collaborating on Git projects

INTERMEDIATE GIT
Fetching from a remote
Fetch from the origin remote

git fetch origin

Fetch all remote branches

Might create new local branches if they only existed in the remote
Doesn't merge the remote's contents into local repo

INTERMEDIATE GIT
Fetching a remote branch
Fetch only from the origin remote's main branch

git fetch origin main

From [Link]
* branch main -> FETCH_HEAD

INTERMEDIATE GIT
Synchronizing content
Merge origin remote's default branch ( main ) into the local repo's current branch

git merge origin

Updating 9dcf4e5..887da2d
Fast-forward
tests/[Link] | 13 +++++++++++++
[Link] | 10 ++++++++++
2 files changed, 23 insertions (+)

INTERMEDIATE GIT
Pulling from a remote
Local and remote synchronization is a common workflow

Git simplifies this process for us!


Fetch and merge from the remote's default ( main ) into the local repo's current branch

git pull origin

INTERMEDIATE GIT
Pulling a remote branch
Pull from the origin remote's dev branch

git pull origin dev

Still merges into the local branch we are located in!

INTERMEDIATE GIT
Git pull output
From [Link]
* branch dev -> FETCH_HEAD
Updating 9dcf4e5..887da2d
Fast-forward
tests/[Link] | 13 +++++++++++++
[Link] | 10 ++++++++++
2 files changed, 23 insertions (+)

INTERMEDIATE GIT
A word of caution
git pull origin

Updating 9dcf4e5..887da2d
error: Your local changes to the following files would be overwritten by merge:
[Link]
Please commit your changes or stash them before you merge.
Aborting

Important to save locally before pulling from a remote

INTERMEDIATE GIT
Let's practice!
I N T E R M E D I AT E G I T
Pushing to remotes
I N T E R M E D I AT E G I T

George Boorman
Curriculum Manager, DataCamp
Pulling from a remote

INTERMEDIATE GIT
Pushing to a remote

INTERMEDIATE GIT
git push
Save changes locally first!

git push remote local_branch

Push into remote from local_branch

Push changes into origin from the local repo's main branch

git push origin main

INTERMEDIATE GIT
Push/pull workflow

INTERMEDIATE GIT
Push/pull workflow

INTERMEDIATE GIT
Push/pull workflow

INTERMEDIATE GIT
Pushing first
Pushing main to the remote before pulling

git push origin main

INTERMEDIATE GIT
Remote/local conflicts

INTERMEDIATE GIT
Remote/local conflicts

INTERMEDIATE GIT
Remote/local conflicts

INTERMEDIATE GIT
Remote/local conflicts

INTERMEDIATE GIT
Avoiding a conflict
Pull from the remote first

git pull origin main

INTERMEDIATE GIT
Pulling without editing
git pull --no-edit origin main

Not recommended, unless we are very confident in the history of our project!

INTERMEDIATE GIT
Pushing a new local branch
Working in hotfix branch locally

hotfix does not exist in the remote

INTERMEDIATE GIT
Creating a new remote branch
hotfix only exists locally

git push origin hotfix

Enumerating objects: 5, done.


Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 349 bytes | 349.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'hotfix' on GitHub by visiting:
remote: [Link]
remote:
To [Link]
* [new branch] hotfix -> hotfix
branch 'hotfix' set up to track 'origin/hotfix'

INTERMEDIATE GIT
Branches

INTERMEDIATE GIT
Working with branches
See all branches Compare two branches

git branch git diff main hotfix

Switch to an existing branch Rename a branch

git switch hotfix git branch -m hotfix bugfix

Create and switch to a new branch Delete a branch

git switch -c hotfix git branch -d hotfix

INTERMEDIATE GIT
Merging branches
From main , to merge ai-assistant into main :

git merge ai-assistant

From another branch: git merge source destination

git merge ai-assistant main

Handling merge conflicts

INTERMEDIATE GIT
Working with remotes
Clone a remote repo

git clone [Link]

Get information about all remotes

git remote -v

Add a new remote

git remote add george [Link]

INTERMEDIATE GIT
Synchronizing local and remote repos
git fetch origin

git pull origin

git push origin documentation

INTERMEDIATE GIT
What is git merge?
Git Merge Command:

git merge

Combines changes from one branch into


another

Finds the common base between two


branches

Use different merge strategies

ADVANCED GIT
Fast-forward merge
What is fast forward merge?

Keep a simple, straight history

Ideal for short-lived branches with simple changes

When not to use

Need to preserve branch history

Complex feature development in long-lived branches

Merge conflict between branches - fast forward fails immediately!

ADVANCED GIT
Fast-forward merge (before)

ADVANCED GIT
Fast-forward merge syntax
Git Merge Fast Forward Default

git checkout main


git merge feature_branch

Force Git Merge Fast Forward

git merge <branch> --ff-only

Example

git checkout main


git merge feature_branch --ff-only

ADVANCED GIT
Fast-forward merge (after)

ADVANCED GIT
Recursive merge
What is a recursive merge?

Creates a merge commit with two parents

Preserve the entire project history


Ideal for long-lived branches

Maintain branching structure

When not to use

When you want to maintain a simple and linear history


Quick and minor changes

ADVANCED GIT
Recursive merge (before)

ADVANCED GIT
Recursive merge syntax
Recursive Merge Command:

git merge --no-ff <branch>

Example

$ git checkout main


Switched to branch 'main'
Your branch is up to date with 'origin/main'.

$ git merge --no-ff feature_branch


Merge made by the 'recursive' strategy.
...

ADVANCED GIT
Recursive merge (after)

ADVANCED GIT
Summary
Fast forward merges keeps history simple and linear
Recursive merges preserves historical context

Recursive merges are better for more complex development

Fast Forward Merge Commands

git merge <branch_name> # default command


git merge --ff-only <branch_name> # force fast forward merge

Recursive Merge Command

git merge --no-ff <branch_name>

ADVANCED GIT
Git squash merging

Functionality Advantages
Creates a single new commit on the target Clean and linear history
branch
Simplifies code review for large features
Combines all changes from the source
Easier to revert the entire feature
branch
Adds a regular commit with one parent
(unlike recursive strategy)

Added to target branch (not feature


branch)
Doesn't preserve the detailed commit
history of the source branch

ADVANCED GIT
Merge squash example

ADVANCED GIT
Merge squash process
1. Checkout the main branch

$ git checkout main

2. Create a squash commit of all data-cleanup changes

$ git merge --squash data-cleanup

3. Commit the squash commit to main branch history

$ git commit -m "Implement and optimize data cleanup"

ADVANCED GIT
Merge squash result

ADVANCED GIT
Git octopus merge

Functionality Advantages
Merges three or more branches at once Useful for integrating multiple independent
features simultaneously
Creates a single merge commit with
multiple parents Used for synchronizing several release
branches for different versions of a project
Best used when branches don't conflict with
each other

ADVANCED GIT
Octopus merge example

ADVANCED GIT
Octopus merge commands
Git Octopus Merge Command

git merge -s octopus

Example

$ git merge -s octopus ingest transform load


Trying simple merge with ingest
Trying simple merge with transform
Trying simple merge with load
Merge made by the 'octopus' strategy.
...

ADVANCED GIT
Octopus merge result

ADVANCED GIT
Summary
Squash merge

Simplifies history, combines multiple commits into one

Use squash merges for a clean, simplified history

git merge --squash <source_branch>

Octopus merge

Preserves branch structure

Merges multiple branches simultaneously


Efficiently integrating multiple parallel developments

git merge -s octopus <branch 1> <branch 2> <branch 3>

ADVANCED GIT
Git rebase
Method to integrate changes Git Rebase Command:
Different from merge
git rebase <branch_name>
Removes merge commits for a cleaner
history

Maintains a linear commit graph for clarity

ADVANCED GIT
Rebase example - before

ADVANCED GIT
Rebase process
1. Checkout your data-cleanup feature branch.

git checkout data-cleanup

1. Rebase main branch onto data-cleanup branch.

git rebase main

data-cleanup commits are recreated after main 's latest commit

Rebased data-cleanup commits get new hashes

Note: If there are any conflicts, these would need to be resolved manually.

ADVANCED GIT
Rebase example - after

ADVANCED GIT
Interactive rebase
git rebase -i <commit_hash>

Functionality

Allows developer to make granular changes to multiple commits

Opens an editor

Warning!

Rebasing public branches can disrupt workflows


Do not use rebase on public branches, like main

Establish team rules on when to use rebase

ADVANCED GIT
Interactive rebase - before

Here is the data-validation branch history before we edit the commit history.

$ git log --oneline data-validation


abc1234 Optimize validation performance
def5678 Fix validation bug
ghi9101 Add data validation function
xyz1234 New Main Branch Commit
vwx7890 Main Branch Commit
jkl2345 Initial commit

ADVANCED GIT
Interactive rebase editor
Here's how we edit the commit history using pick and fixup commands in the open editor.

$ git rebase -i HEAD~3


pick ghi9101 Add data validation function
fixup def5678 Fix validation bug
fixup abc1234 Optimize validation performance

# Rebase xyz1234..abc1234 onto HEAD~3(xyz1234) (3 commands)


#
# Commands:
# p, pick <commit> = use commit
# f, fixup <commit> = like "squash", but discard this commit's log message
...

ADVANCED GIT
Interactive rebase - after
Before After

$ git log --oneline data-validation $ git log --oneline data-validatoin


abc1234 Optimize validation performance mno6789 Add data validation function
def5678 Fix validation bug xyz1234 New Main Branch Commit
ghi9101 Add data validation function vwx7890 Main Branch Commit
xyz1234 New Main Branch Commit jkl2345 Initial commit
vwx7890 Main Branch Commit
jkl2345 Initial commit The following commits are combined into one

abc1234
def5678
ghi9101

ADVANCED GIT
Merge versus rebase
Merge

Preserves full history and branch structure

Maintains context of parallel development


Rebase

Replays commits on top of the target


branch

Creates a linear history

Loses some contextual information

ADVANCED GIT
When to use merge versus rebase
Merge

For integrating completed features, preserving development context

Rebase

For keeping feature branches updated with main, or for cleaning up before merging

Remember

Rebase rewrites history - use it carefully

ADVANCED GIT
What is cherry-pick?
Applies the changes from a specific commit Purpose
to another branch
Apply specific bug fixes across branches
Cherry-Pick Single Commit
Roll back features to stable versions
Selectively apply experimental changes
git cherry-pick <commit-hash>
Recover lost commits
Cherry-Pick Multiple Commits

git cherry-pick <hash1> <hash2> ..

ADVANCED GIT
Cherry-Pick example

ADVANCED GIT
Cherry-Pick process
1. Checkout main branch

git checkout main

2. Run cherry-pick command to bring in


commit def456 changes from feature
branch.

git cherry-pick def456

ADVANCED GIT
Resolving cherry-pick conflicts
Conflict resolution steps
1. Manually edit conflicting files

2. Add resolved files to staging using git add <resolved-files>

3. Continue with the cherry-pick process by running --continue flag

git cherry-pick --continue

Stopping a cherry-pick
To stop a cherry-pick operation, use the --abort flag

git cherry-pick --abort

ADVANCED GIT
When to use cherry-pick
Use cases Cautions
1. Applying hotfixes 1. Can create duplicate commits

2. Testing isolated features 2. May complicate project history if overused


3. Larger changes: consider merging or
rebasing

ADVANCED GIT
What is git bisect?
A tool that uses binary search to find the Purpose
commit that introduced a bug
1. Find the bad commit fast
Git Bisect Command
2. Essential for data debugging
3. Speeds up root cause analysis
git bisect

ADVANCED GIT
Bisect - start
1. Initiate git bisect session

git bisect start

2. Initialize the current state as a bad state

git bisect bad

3. Mark the last known good state

git bisect good <commit-hash>

ADVANCED GIT
Bisect - search
1. Marks the commit state as a bad commit

git bisect bad

2. Marks the commit state as a good commit

git bisect good

ADVANCED GIT
Bisect - automated search
Checks if the commit version is good or bad
by running an automated test script.

git bisect run <script_name>

The script must return 0 if the tests passed.

If the tests fail, it should return a non-zero


number.

ADVANCED GIT
Bisect - result
Git Bisect Output Example

$ git log
b1a534f is the first bad commit
commit b1a534f89l2c3d4e5f6g7h8i9j0k1l2m3n4o5p
Author: Jane Doe <jane@[Link]>
Date: Thu Mar 14 14:30:00 2024 -0500

Update data transformation logic

Exits the git bisection process and return to our current HEAD

git bisect reset

ADVANCED GIT
When to use git bisect
Use cases

1. Find regressions in data workflows

2. Use test scripts for faster debugging

Tips

Automate testing with git bisect run <test-script>

Use descriptive commit messages to aid the process

ADVANCED GIT
What is git filter-repo?
Git Filter-Repo Command Purposes

git filter-repo
1. Remove sensitive data (e.g., passwords,
tokens)
A tool for rewriting Git repository history 2. Clean up unnecessary files
quickly and safely.
3. Restructure repositories
Rename files or directories 4. Reduce repository size
Operates on all branches simultaneously

ADVANCED GIT
Filter-Repo process
1. Install git filter-repo using pip

pip install git-filter-repo

2. Remove [Link] from every commit

git filter-repo --path [Link] --invert-paths

Filter-Repo Related Filters

--path : specifies which paths to operate on

--invert-paths : operate on all paths except the ones specified in --path

ADVANCED GIT
Filter-Repo result
Output

Parsed 150 commits


New history written in 0.10 seconds; now repacking/cleaning...
Repacking your repo and cleaning out old unneeded objects

Key Implications

All branches and commits were updated

All commit hashes were changed

A force push is needed after this step

Team members will need to clone repo again

ADVANCED GIT
When to use filter-repo
Use cases

Removing sensitive data (e.g., passwords)

Cleaning up bloated repositories


Renaming or reorganizing files across all commits

Tips

Always back up your repository before using filter-repo

Coordinate with collaborators before pushing rewritten history

ADVANCED GIT
What is Git Reflog?
1. Local record of ALL reference updates in
our repository

2. Reflog is stored in on our local system


under the .git/logs/refs/heads/
directory

3. Records changes to branch tips and HEAD


position

4. Acts a safeguard for our Git operations

5. Helps recover accidental changes or


deletions

ADVANCED GIT
Git Reflog versus Git Log
Feature Git Reflog Git Log
Shows reference updates and
Purpose rewrites in local repo Shows commit history only

Scope Local repository only Local and remote repositories


All ref updates (commits, resets,
Content Only commits
merges, etc.)
Persistence Temporary (usually 90 days) Permanent (part of repository history)
Recovering lost commits, Viewing project history, understanding
Use Case
understanding recent actions feature development

ADVANCED GIT
Reflog Commands
Displays the log data and HEAD activity

git reflog
git reflog show

Clean up old log or unreachable entries

git reflog expire

ADVANCED GIT
Reflog Output Structure
Component Description
short-hash Abbreviated commit hash

ref Usually HEAD, but can be


branch names
Position in the reflog (0 is the
index
most recent)
Type of action (commit, reset,
action merge, etc)
descriptions Description about the action

1 [Link]

ADVANCED GIT
Filtering: Since and Until
since = show entries from this point in time

git reflog --since "time-qualifer"

until = show entries up to this point in time

git reflog --until "time-qualifer"

Usage

git reflog --since="1 week ago"


git reflog --until="yesterday"
git reflog --until="2024-01-01"

ADVANCED GIT
Recovering Deleted Branches
Scenario Solution
Created a branch called etl-feature
1. Identify the hash of the commit at the tip
Committed ETL feature changes to this of deleted branch using git reflog
branch
2. Use git checkout to move HEAD to the
We accidentally deleted this branch commit hash of the deleted branch
All code changes were lost 3. Create a new branch using the commit
How can we restore the etl-feature HEAD is currently pointed
branch?
git reflog
git checkout <hash>
git checkout -b <branch-name>

1 [Link]

ADVANCED GIT
Git Reset
Moves the HEAD to the specific commit object.
Depending on the reset type, the working and staging area is updated.

Effect on Working Effect on Staging


Reset Type Command Directory Area
git reset --soft Changes remain
Soft No changes
<commit> staged

Mixed git reset --mixed Changes are


No changes
(Default) <commit> unstaged

git reset --hard Changes are


Hard Changes are discarded
<commit> discarded

ADVANCED GIT
Finding A Loss Commit
Scenario Solution
Tests started failing after commits and
1. Find the deleted commit hash using Git
rebases
Reflog
Unknown commit caused the failure
2. Use git reset to revert to the commit with
Need to revert to a passing state the passing tests
How do we revert back to the previous ETL
git reflog
script changes?
git reset --soft HEAD@{1}

ADVANCED GIT
Best Practices
Powerful for recovering lost code and
commits

Use descriptive commit messages

Push to remote regularly

Be cautious with force pushes

Reflog is our local time machine when we


make mistakes

ADVANCED GIT
What is a Git Worktree?
Git Worktree Command

git worktree

Can "checkout" multiple branches in your


workspace.

Similar to a repo checkout, but efficient

No need for stashing changes

No need to switch between branches


during development

ADVANCED GIT
Git Worktree versus Git Switch
This tables compares using git worktree vs git switch in a development workflow.

Git Worktree Git Switch


Multiple active branches One active branch at a time
Separate directories Single working directory
No need to stash changes May require stashing

ADVANCED GIT
Creating a Git Worktree
Create new work tree from <branch> into directory <path>

git worktree add <path> <branch>

Example

Create a new work tree from the bugfix/data-validation branch into the ../etl-bugfix
directory

git worktree add ../etl-bugfix bugfix/data-validation

ADVANCED GIT
Listing and Removing Worktrees
Lists all active worktrees: git worktree list

Example Output

$ git worktree list


flight-pipeline a1b2c3d [main]
flight-pipeline-feature e4f5g6h [feature]
flight-pipeline-hotfix i7j8k9l [hotfix]

Removes a worktree from a <path>: git worktree remove <path>

Example Output

$ git worktree remove flight-pipeline-hotfix


flight-pipeline-hotfix: deleted

ADVANCED GIT
When to use Git Worktrees
When to use:

Working on multiple features simultaneously

Handling urgent bug fixes without disrupting ongoing work


Running tests on different branches in parallel

Code reviews while continuing development

Reconsider when:

Disk space is limited

Projects with frequent updates and complex merge

ADVANCED GIT
Best practices for Git Worktrees
When using Git worktrees, keep these tips in mind:

1. Use clear naming conventions for worktree directories

2. Regularly prune unused worktrees to keep the workspace clean


3. Be mindful of disk space, especially with large projects

4. Use worktrees for short-lived parallel work to avoid confusion

ADVANCED GIT
What is a Git Submodule?
Git Submodule

git submodule

A repository nested within another


repository

Separate version control and history

Submodule changes does not affect main


repo

Main repo can reference a specific version


of a submodule

ADVANCED GIT
Adding a submodule
Adding a submodule using the link or directory under path folder.

git submodule add <repository link|dir> <path>

Example

Adds the data validator library to the ETL project under the libs/validator folder in the ETL
repo.

git submodule add [Link] libs/validator

ADVANCED GIT
Listing submodules
List all submodules in a project

git submodule status

Example

$ git submodule status


e1f2...7w8x9 data_cleaning_lib
a1b2...q7r8 api_connector
d9e8...t3u2 visualization_toolkit

ADVANCED GIT
Updating submodules
Update submodule with the latest changes

There are several options:

1. Updates all submodules where the source code is on your local computer.

git submodule update --init

2. Updates all submodule where the source code is on a remote repo.

git submodule update --init --remote

3. Updates a specific submodules

git submodule update --init <path_to_submodule>

ADVANCED GIT
Removing submodules
Remove a submodule process

1. Deinitialize the submodule.

git submodule deinit <submodule_name>

2. Remove the submodule from git repo index.

git rm <path>

ADVANCED GIT
Extracting a submodule from a large repo
1. Copy all files that need to be in the new submodule repo into another folder outside the
repo.

2. Inside the new folder, create a new repository for the submodule:

git init <new-submodule>

3. Use git filter-repo to extract the relevant files and history from the main project:

git filter-repo --path <extract_path> --invert-paths

4. Add the extracted repository as a submodule to the main project:

git submodule add <new-submodule_path> <path_to_store_submodule>

ADVANCED GIT
When to use submodules and best practices
Use cases:

1. Managing external libraries

2. Sharing code across projects


3. Maintaining specific versions of dependencies

Best practices:

1. Keep submodules updated

2. Use relative paths

3. Communicate changes with team

ADVANCED GIT
What is Git Large File System?
Git LFS Command: Benefits:

git lfs
1. Reduced repository size

2. Faster cloning and fetching


Git LFS: Git Large File Storage
3. Efficient binary file handling
Replace large files in repo
4. Improved collaboration on large files
Small pointer files

Large files separate from repo

ADVANCED GIT
Git LFS initialization process
Initialize Git LFS Commit new changes

git commit -m "Track CSV files"


git lfs install

Setup files to track and generate


.gitattributes file

git lfs track "*.csv"

Add to git index .gitattributes with


tracking config

git add .gitattributes

ADVANCED GIT
Git LFS update process
1. Add new file using git add

git add large_file.csv

2. Commit and push the changes

git commit -m "Update large CSV file"


git push origin main

3. Download changes

git pull
git lfs pull # If needed to explicitly download LFS content

ADVANCED GIT
When to use Git LFS
When to use: When not to use:

1. Need to track changes to large datasets 1. Infrequently updated large files


(CSV, JSON, etc.)
2. Small text files, like code
2. Machine learning models 3. Tight storage quotas
3. Binary assets (images, videos)

4. Version control compressed or installer files

ADVANCED GIT
Best practices
1. Efficient large file management
2. Improved collaboration on data-heavy projects

3. Seamless integration with Git workflow

Tips:

1. Track files selectively

2. keep your team informed about LFS usage

3. Regularly prune LFS cache

ADVANCED GIT
What is Trunk Based Development?
Source control branching CI/CD model
Developer no longer push to separate release branches

Changes are from short-lived branches pushed to main

Small and frequent updates

ADVANCED GIT
Core principles of Trunk Based Development
1. Frequent commits to main
2. Short-lived feature branches (< 1 day)

3. Continuous integration

4. Feature flags for incomplete work

ADVANCED GIT
Feature flagging in TBD
Manages incomplete features
Prevent user from being affected

Features gradually released

Example Feature Flag Code

if feature_flag_enabled('new_feature'):
# New feature code
else:
# Old feature code

ADVANCED GIT
Continuous integration in TBD
Commits to main trigger automated build and tests
Reduce maintenance and faster releases

Product alway reliable and stable

Ensure secure code and compliance with industry standards

Maintain code quality and reduce bug risk

ADVANCED GIT
Benefits and challenges of TBD
Benefits: Challenges:

Reduced merge conflicts Requires team discipline

Faster release cycles Needs robust testing


Improved code quality Initial learning curve

Better collaboration Managing incomplete features

ADVANCED GIT
Best Practices
1. Commit small changes frequently
2. Automate testing and deployment

3. Use feature flags for incomplete work

4. Conduct regular code reviews

5. Monitor after deployment

ADVANCED GIT
Chapter Learnings

ADVANCED GIT
Key Takeaways

ADVANCED GIT
Next Steps
Learn about git hooks

Explore topics on advanced CI/CD integration techniques

Apply your skills by contributing to open-source projects

Keep an update on new Git features and updates

ADVANCED GIT

You might also like