Git Basics: Commands and Workflow Guide
Git Basics: Commands and Workflow Guide
Directory = folder
INTRODUCTION TO GIT
Useful terminal commands
pwd
/home/repl/Documents
ls
INTRODUCTION TO GIT
Changing directory
cd archive
pwd
/home/repl/Documents/archive
INTRODUCTION TO GIT
Checking Git version
git --version
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
INTRODUCTION TO GIT
Converting a project into a repo
Convert an existing directory into a Git repo
git init
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)
INTRODUCTION TO GIT
Nested repositories
Don't create a Git repo inside another Git
repo
Known as nested repos
INTRODUCTION TO GIT
The Git workflow
Edit and save files on our computer
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 .
INTRODUCTION TO GIT
Making a commit
git commit -m "Adding a README."
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
INTRODUCTION TO GIT
Visualizing the commit structure
INTRODUCTION TO GIT
Git hash
Last commit: b22eb75a82a68b9c0f1c45b9f5a9b7abe281683a
INTRODUCTION TO GIT
Git log
git log
commit ad8accfe94cb924444c488132bdef7c54b9bca68
Author: Rep Loop <repl@[Link]>
Date: Wed Jul 24 07:48:27 2022 +0000
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 - :
git log -3
INTRODUCTION TO GIT
Restricting the file
To only look at the commit history of one file:
INTRODUCTION TO GIT
Combining techniques
cd data
INTRODUCTION TO GIT
git log output
commit f35b9487c063d3facc853c1789b0b77087a859fa
Author: Rep Loop <repl@[Link]>
Date: Fri Jul 26 15:14:32 2024 +0000
commit 7f71eadea60bf38f53c8696d23f8314d85342aaf
Author: Rep Loop <repl@[Link]>
Date: Fri Jul 19 09:58:21 2024 +0000
INTRODUCTION TO GIT
Customizing the date range
Restrict git log by date:
INTRODUCTION TO GIT
Acceptable filter formats
Natural language Date format
"2 weeks ago" "07-15-2024"
1 [Link]
INTRODUCTION TO GIT
Finding a particular commit
git log
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
INTRODUCTION TO GIT
git diff output
INTRODUCTION TO GIT
Comparing to a staged file
Add [Link] to the staging area
Compare last committed version of [Link] with the version in the staging area
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
INTRODUCTION TO GIT
Comparing two commits
Find the commit hashes
git log
Compare them
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
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
INTRODUCTION TO GIT
Reverting files
git revert HEAD
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
INTRODUCTION TO GIT
Revert a single file
git revert works on commits, not individual files
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]"
INTRODUCTION TO GIT
Unstaging a file
INTRODUCTION TO GIT
Unstaging a single file
To unstage a single file:
INTRODUCTION TO GIT
Unstaging all files
To unstage all files:
INTRODUCTION TO GIT
Summary
Command Result
git revert HEAD Revert all files from a given 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
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
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 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
INTERMEDIATE GIT
Branches
Branch = an individual version of a repo
In each branch:
Some files might be the same
Others might be different
INTERMEDIATE GIT
Why use branches?
Live system Feature development
INTERMEDIATE GIT
Why use branches?
Multiple developers can work on a project simultaneously
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
INTERMEDIATE GIT
Creating a new branch
Create a new branch called speed-test
INTERMEDIATE GIT
Terminology
Creating a new branch = "branching off"
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
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!
INTERMEDIATE GIT
Modifying branches
git branch
main
* feature_dev
feature_dev
git branch -m
INTERMEDIATE GIT
Renaming a branch
git branch
main
* feature_dev
feature_dev
INTERMEDIATE GIT
Checking our branches
git branch
main
* chatbot
INTERMEDIATE GIT
Deleting a branch
Large projects can have many branches
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
INTERMEDIATE GIT
Summary
Command Function
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
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
main = destination
INTERMEDIATE GIT
Merging branches
Move to the destination branch:
INTERMEDIATE GIT
Git merge output
INTERMEDIATE GIT
Git merge output
Parent commits
INTERMEDIATE GIT
Git merge output
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.
INTERMEDIATE GIT
Merging
From the main branch
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
INTERMEDIATE GIT
Merging the branches
Merging now that the conflict is resolved
Already up to date.
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
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
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
git remote
datacamp
INTERMEDIATE GIT
Getting more information
Get more information about the remote(s)
git remote -v
INTERMEDIATE GIT
Creating a remote
When cloning, Git will automatically name the remote origin
INTERMEDIATE GIT
Remote vs. local
INTERMEDIATE GIT
Collaborating on Git projects
INTERMEDIATE GIT
Fetching from a remote
Fetch from the origin remote
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
From [Link]
* branch main -> FETCH_HEAD
INTERMEDIATE GIT
Synchronizing content
Merge origin remote's default branch ( main ) into the local repo's current branch
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
INTERMEDIATE GIT
Pulling a remote branch
Pull from the origin remote's dev branch
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
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!
Push changes into origin from the local repo's main branch
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
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
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
INTERMEDIATE GIT
Creating a new remote branch
hotfix only exists locally
INTERMEDIATE GIT
Branches
INTERMEDIATE GIT
Working with branches
See all branches Compare two branches
INTERMEDIATE GIT
Merging branches
From main , to merge ai-assistant into main :
INTERMEDIATE GIT
Working with remotes
Clone a remote repo
git remote -v
INTERMEDIATE GIT
Synchronizing local and remote repos
git fetch origin
INTERMEDIATE GIT
What is git merge?
Git Merge Command:
git merge
ADVANCED GIT
Fast-forward merge
What is fast forward merge?
ADVANCED GIT
Fast-forward merge (before)
ADVANCED GIT
Fast-forward merge syntax
Git Merge Fast Forward Default
Example
ADVANCED GIT
Fast-forward merge (after)
ADVANCED GIT
Recursive merge
What is a recursive merge?
ADVANCED GIT
Recursive merge (before)
ADVANCED GIT
Recursive merge syntax
Recursive Merge Command:
Example
ADVANCED GIT
Recursive merge (after)
ADVANCED GIT
Summary
Fast forward merges keeps history simple and linear
Recursive merges preserves historical context
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)
ADVANCED GIT
Merge squash example
ADVANCED GIT
Merge squash process
1. Checkout the main branch
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
Example
ADVANCED GIT
Octopus merge result
ADVANCED GIT
Summary
Squash merge
Octopus merge
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
ADVANCED GIT
Rebase example - before
ADVANCED GIT
Rebase process
1. Checkout your data-cleanup feature branch.
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
Opens an editor
Warning!
ADVANCED GIT
Interactive rebase - before
Here is the data-validation branch history before we edit the commit history.
ADVANCED GIT
Interactive rebase editor
Here's how we edit the commit history using pick and fixup commands in the open editor.
ADVANCED GIT
Interactive rebase - after
Before After
abc1234
def5678
ghi9101
ADVANCED GIT
Merge versus rebase
Merge
ADVANCED GIT
When to use merge versus rebase
Merge
Rebase
For keeping feature branches updated with main, or for cleaning up before merging
Remember
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
ADVANCED GIT
Cherry-Pick example
ADVANCED GIT
Cherry-Pick process
1. Checkout main branch
ADVANCED GIT
Resolving cherry-pick conflicts
Conflict resolution steps
1. Manually edit conflicting files
Stopping a cherry-pick
To stop a cherry-pick operation, use the --abort flag
ADVANCED GIT
When to use cherry-pick
Use cases Cautions
1. Applying hotfixes 1. Can create duplicate commits
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
ADVANCED GIT
Bisect - search
1. Marks the commit state as a bad commit
ADVANCED GIT
Bisect - automated search
Checks if the commit version is good or bad
by running an automated test script.
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
Exits the git bisection process and return to our current HEAD
ADVANCED GIT
When to use git bisect
Use cases
Tips
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
ADVANCED GIT
Filter-Repo result
Output
Key Implications
ADVANCED GIT
When to use filter-repo
Use cases
Tips
ADVANCED GIT
What is Git Reflog?
1. Local record of ALL reference updates in
our repository
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
ADVANCED GIT
Reflog Commands
Displays the log data and HEAD activity
git reflog
git reflog show
ADVANCED GIT
Reflog Output Structure
Component Description
short-hash Abbreviated commit hash
1 [Link]
ADVANCED GIT
Filtering: Since and Until
since = show entries from this point in time
Usage
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.
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
ADVANCED GIT
What is a Git Worktree?
Git Worktree Command
git worktree
ADVANCED GIT
Git Worktree versus Git Switch
This tables compares using git worktree vs git switch in a development workflow.
ADVANCED GIT
Creating a Git Worktree
Create new work tree from <branch> into directory <path>
Example
Create a new work tree from the bugfix/data-validation branch into the ../etl-bugfix
directory
ADVANCED GIT
Listing and Removing Worktrees
Lists all active worktrees: git worktree list
Example Output
Example Output
ADVANCED GIT
When to use Git Worktrees
When to use:
Reconsider when:
ADVANCED GIT
Best practices for Git Worktrees
When using Git worktrees, keep these tips in mind:
ADVANCED GIT
What is a Git Submodule?
Git Submodule
git submodule
ADVANCED GIT
Adding a submodule
Adding a submodule using the link or directory under path folder.
Example
Adds the data validator library to the ETL project under the libs/validator folder in the ETL
repo.
ADVANCED GIT
Listing submodules
List all submodules in a project
Example
ADVANCED GIT
Updating submodules
Update submodule with the latest changes
1. Updates all submodules where the source code is on your local computer.
ADVANCED GIT
Removing submodules
Remove a submodule process
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:
3. Use git filter-repo to extract the relevant files and history from the main project:
ADVANCED GIT
When to use submodules and best practices
Use cases:
Best practices:
ADVANCED GIT
What is Git Large File System?
Git LFS Command: Benefits:
git lfs
1. Reduced repository size
ADVANCED GIT
Git LFS initialization process
Initialize Git LFS Commit new changes
ADVANCED GIT
Git LFS update process
1. Add new file using git add
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:
ADVANCED GIT
Best practices
1. Efficient large file management
2. Improved collaboration on data-heavy projects
Tips:
ADVANCED GIT
What is Trunk Based Development?
Source control branching CI/CD model
Developer no longer push to separate release branches
ADVANCED GIT
Core principles of Trunk Based Development
1. Frequent commits to main
2. Short-lived feature branches (< 1 day)
3. Continuous integration
ADVANCED GIT
Feature flagging in TBD
Manages incomplete features
Prevent user from being affected
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
ADVANCED GIT
Benefits and challenges of TBD
Benefits: Challenges:
ADVANCED GIT
Best Practices
1. Commit small changes frequently
2. Automate testing and deployment
ADVANCED GIT
Chapter Learnings
ADVANCED GIT
Key Takeaways
ADVANCED GIT
Next Steps
Learn about git hooks
ADVANCED GIT