Git Good (Enough)

A graphical introduction to Git

The Problem with Git Tutorials

Git Graphs


gitGraph
   commit
   commit
   branch develop
   checkout develop
   commit
   commit
   checkout main
   merge develop
   checkout develop
   commit
   commit
   checkout main
   commit
   branch fix_1
   commit
   commit
   checkout main
   merge fix_1
   commit
   checkout develop

Git Graphs: Setup

Scenario

  • We have just created an empty directory
  • We are going to write some code
  • Then make some changes
  • and manage it all with git

Git Graphs

First we init the repo, then create a new file.


myscript.py
def add_numbers(num1, num2):
    """Add two things together"""
    answer = num1 + num2
    print(f"{num1} + {num2} is {answer}")

add_numbers(2, 2)

Git init

The init creates a new repo in the specified directory.

# Create a repo from scratch
mkdir new
git init new

# or leave off the path for current directory
cd new
git init

Git Graphs: Add

We add modified files so git knows we may want to save them.

< myscript.py >

[+1] def add_numbers(num1, num2):

[+2] “““Add two things together”“”

[+3] answer = num1 + num2

[+4] print(f”{num1} + {num2} is {answer}“)

[+5]

[+6] add_numbers(2, 2)

Git Add

The add command stages files, which means they are marked to be saved.

# add particular file
git add myscpript.py

# add all files
git add -A

# add all python files
git add *.py

# add all files in a folder
git add my_folder

Git Graphs: Commit

Then commit to saving them. Note the Tag.

[9d6b7c…] < myscript.py >

[+1] def add_numbers(num1, num2):

[+2] “““Add two things together”“”

[+3] answer = num1 + num2

[+4] print(f”{num1} + {num2} is {answer}“)

[+5]

[+6] add_numbers(2, 2)

Git Commit

The commit command saves the changes we added (staged). Add a message in the commit command or an editor will open automatically.

# commit staged changes with a simple message
git commit -m "simple message"

# add all changes to previously committed files
# but wont add new files
git commit -am "simple message"

Git Graphs: Commit

This creates our first commit (dot) on the default branch (main).

“HEAD” refers to the latest commit on the current branch.


gitGraph
   commit id: "9d6b7c"

Commit Hash

The structure of the full commit HASH:

6fd21da172fa169568a996bc2c7e97461bec7110

A very long string of hexadecimal numbers (0-9 and a-f)

Fortunately, you can reference a commit with a few of the left characters:

6fd21d

Git Graphs: Commit

Each commit has metadata.

[9d6b7c…] (HEAD, main)

Author:

Derrick Chambers
<d-chambers@users.noreply.github.com>

Date:

>Mon Sep 26 20:11:10 2022 -0600

Message:

work on myscript

Git Show

The show can be used to see metadata about a commit

# show metadata about 9d6b7c and its diff
git show 9d6b7c

# suppress the diff
git show --quiet 9d6b7c

Git Graphs: Modify

Next, we make some changes.


myscript.py
def add_numbers(num1, num2):
    """Add two things together"""
    answer = num1 + num2
    print(f"{num1} + {num2} is {answer}")

add_numbers(2, 2)

Git Graphs: Modify

Next, we make some changes.


myscript.py
def add_numbers(num1, num2):
    """Add two things together"""
    return num1 + num2

assert add_numbers(1, 1) == 2

Git Graphs: Diff

Then diff to see difference between saved and current state.

< myscript.py >

[-3] answer = num1 + num2

[-4] print(f”{num1} + {num2} is {answer}“)

[-6] print(f”{num1} + {num2} is {answer}“)

[+3] return num1 + num2

[+5] assert add_numbers(1, 1) == 2

Git Diff

The diff commands shows changes between commits, files, branches, etc.

# diff between last commit and unstaged changes
git diff

# diff between two commits
git diff 9d6b7c 046a7a

# diff between a file on latest commit for
# two different branches
git diff main:myscript.py branch_1:myscript.py

Git Graphs: Add (again)

Then add the changes.

< myscript.py >

-4 answer = num1 + num2

-5 print(f”{num1} + {num2} is {answer}“)

-7 add_numbers(2, 2)

+3 return num1 + num2

+5 assert add_numbers(1, 1) == 2

Git Graphs: Commit (again)

And finally commit them.

[046a7a]

< myscript.py >

-4 answer = num1 + num2

-5 print(f”{num1} + {num2} is {answer}“)

-7 add_numbers(2, 2)

+3 return num1 + num2

+5 assert add_numbers(1, 1) == 2

Git Graphs

Which adds a new commit to our main branch.


gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"

Knowledge Check


gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"

  • What is main?
  • What is 046a7a?
  • What information does it contain?
  • What commit does “HEAD” refer to?

Git Graphs: Branch

We can create branches with their own commits

(think parallel universe)


gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   branch branch_1
   commit id: "2-a577878"
   commit id: "3-c17d954"

Git Branch

The branch command is used to see, create, and destroy branches.

# list local branches
git branch

# list all branches (local and remote)
git branch -a

# create a new branch
git branch new_branch

# delete a branch
git branch --delete new_branch

Git Graphs: Merge

Then merge branches (e.g., back into main)


gitGraph
   commit id: "9d6b7c"
   commit id: "1-046a7a"
   branch branch_1
   commit id: "4c0f818"
   commit id: "221d22"
   checkout main
   merge branch_1

Git Merge

The merge command joins branches together.

# ensure current branch is main
# or HEAD is latest commit of main
git checkout main

# merge branch_1 into current branch
git merge branch_1

Git Graphs: Merge

Which has the same effect (on the code) as committing on main


gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"

Git Graphs: Merge

But what if changes are made on main before the merge?


gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   branch branch_1
   commit id: "4c0f818"
   commit id: "221d22"
   checkout main
   commit
   commit

Git Graphs: Merge

We can still merge branch_1 into main but….

there might be conflicts


gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   branch branch_1
   commit id: "4c0f818"
   commit id: "221d22"
   checkout main
   commit
   commit
   merge branch_1

Git Graphs: Merge

What should happen if the changes aren’t compatible?

  • Prioritize changes on main?
  • Prioritize changes on branch_1?
  • Make the merger sort it out?

Make the merger sort it out!

Git Conflicts

Dealing with conflicts isn’t fun (in git or life). Here are a few commands that might help.


# abort merge (after conflicts show up)
git merge --abort

# list files with merge conflicts
git diff --name-only --diff-filter=U --relative


When all else fails

Git Graphs: Conflicts

Example: myscript.py on main


myscript.py (main)
"""
A script to practice using functions.

A brilliant script written by Derrick.
"""
...

Git Graphs: Conflicts

Example: myscript.py on branch_2


myscript.py (branch_2)
"""
A script to practice using functions.

A really dumb script written by Peiyao.
"""
...

Git Graphs: Conflicts

myscript.py after merge command


myscript.py (merge conflict)
"""
A script to practice using functions.

<<<<<<< HEAD
A brilliant script written by Derrick.
=======
A really dumb script writen by Peiyao.
>>>>>>> branch_2
"""
...

Git Graphs: Conflicts

Next, manually edit myscript.py so it is correct


myscript.py (resolved)
"""
A script to practice using functions.

A brilliant script written by Derrick.
"""
...

Knowledge Check


Now that we have fixed the file:

  • What do we need to do next?
  • And after adding the changes?
  • What would be a good message for this commit?

Git Graphs: Tag

Commits can be be tagged (usually with a version)


gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a" tag: "v1.0.0"
   commit id: "4c0f818"
   commit id: "221d22" tag: "v1.1.0"

Git Tag

Tag is like adding an alias to refer to a commit with a shorter string. Usually, but not always, tags are used for versioning.

# list tags
git tag

# tag HEAD with version number
git tag v1.0.1

# Tag head with version, add annotation
git tag -a v1.0.1 -m "my favorite version"

# Show info about tags
git show v1.0.1

Git Graphs: Cherry-Pick

Commits can be Cherry-Picked


gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"
   checkout main
   commit id: "4c0f818"
   commit id: "221d22"

Git Graphs: Cherry-Pick

Commits can be Cherry-Picked


gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"
   checkout main
   commit id: "4c0f818"
   commit id: "221d22"
   commit id: "dc12a2"

Git Cherry-Pick

The cherry-pick command allows you to apply as single commit (from any branch) to HEAD. Watch out though, you can still get conflicts.

# Make sure HEAD is the last commit on main branch
git checkout main

# cherry pick a commit from a different branch to main
git cherry-pick 221d227eea

Git Graphs: Rebase

Branches can be rebased


gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"
   checkout main
   commit id: "4c0f818"
   commit id: "221d22"

Git Graphs: Rebase

Branches can be rebased.


gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"

Git Rebase

The rebase command changes the starting point of a branch. MIt is often used to bring changes back into a feature branch from the main branch.

Rebase changes the git history. Make sure no one else is working on the branch before rebasing.

# navigate to a feature branch
git checkout branch_1

# apply all the missing main commits from main
# onto branch_1
git rebase main

Git Graphs: Reset

HEAD can be reset, keeping or discarding commits/unsaved changes.

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"

Git Rest

The reset command cab be used to undo commits or staging.

# Undo git add
git reset

# Erase all unsaved changes
git reset --hard

# Remove last three commits, keep changes as staged
git reset --soft HEAD~3

Git Graphs: Reset Soft

Soft reset

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "Unsaved" type: HIGHLIGHT

Git Graphs: Reset Hard

Hard reset

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"

Danger

All commits and unsaved work after “046a7a” really are gone…

Git Graphs: Reset Hard

Hard reset

Git Graphs: Squash

Commits can be squashed (combined)


gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"

Git Graphs: Squash

Commits can be squashed (combined)


gitGraph
   commit id: "ab3b7c"
   commit id: "046a7a" type: REVERSE
   commit id: "4c0f818" type: REVERSE
   commit id: "221d22" type: REVERSE

Git Squash

There actually isn’t a squash command, but a few ways to squash commits together

# squash last 3 commits with reset
git reset --soft HEAD~3
git commit -m "squashed last 3 commits"

Git Distributed Graphs

Git is a distributed version control system

This means it can interact with remote copies of the repository (like one hosted by github).

Remote repos have names, like “origin” (think github).

Git Graphs: Clone

Git can clone remote (non-local) repositories.

remote: origin

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"

Git Graphs: Clone

Git can clone remote (non-local) repositories.

local

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"

remote: origin

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"

Git CLone

The clone command downloads a remote repository (and all its branches).

# Use http to download a repo
git clone https://github.com/opensourcecourse/git_repo

# Use ssh
git clone https://github.com/opensourcecourse/git_repo

# See all branches
git branch -a

Git Graphs: Fetch

Git can also fetch new branches since clone.

local

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"

remote: origin

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"

Git Graphs: Fetch

Git can also fetch new branches since clone.

local

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"

remote: origin

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"

Git Fetch

Git fetch gets specific info from a remote repo (like a small clone).

# fetch branch
git fetch branch_1
git checkout branch_1

# fetch all branches not on local
git fetch --all

# fetch tags
git fetch --tags

Git Fetch

Note: After cloning it may appear not all branches were downloaded. Just make sure to use the -a flag on branch to see them. All you need to do is checkout one of the remote branches to make a it a local one.

# list all branches (note the origin/branch_1)
git branch -a

# checkout branch, making a local copy in process
git checkout branch_1

Git Graphs: Push

We push local changes to the remote repo.

local

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"
   commit id: "5dab5d"

remote: origin

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"

Git Push

The push command shares local commits/branches with a remote repo. If the local and remote history have diverged you may have to force the push. Careful though, make sure no one else depends on that history!

# Push new commits on main to origin's main branch
Git push origin main

# Push a new branch to origin
git push origin branch_1

# Force push a rebased branch
git push origin branch_2 --force

Git Graphs: Push

We push local changes to the remote repo.

local

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"
   commit id: "5dab5d"

remote: origin

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"
   commit id: "5dab5d"

Git Graphs: Pull

We pull changes from remote to local (fetch + merge).

local

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"

remote: origin

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"
   commit id: "5dab5d"

Git Pull

The pull command is used to fetch and merge changes from a remote repo into your current branch.

# Update main branch with commits from remote main
git pull origin main

Git Graphs: Pull

We pull changes from remote to local (fetch + merge).

local

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"
   commit id: "5dab5d"

remote: origin

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"
   commit id: "5dab5d"

Git Graphs: Fork

fork creates remote clones.

remote: origin

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"
   commit id: "5dab5d"

Git Fork

There is no fork command. Forking done from Github’s web interface.

Git Graphs: Fork

fork creates remote clones.

remote: fork

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"
   commit id: "5dab5d"

remote: origin

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch develop
   commit id: "dc12a2"
   commit id: "ca2259"
   commit id: "5dab5d"

Other Useful Git Commands

  • checkout - Move HEAD to different location (commit or branch)
  • log - Display info about previous commits
  • status - Show local state (current branch) and unsaved changes
  • show - Show info about a git object
  • stash - Save changes outside of a commit

Knowledge Check

Name that Git Command (1)

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"

Knowledge Check

Name that Git Command (1)

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   branch develop

Knowledge Check

Name that Git Command (2)

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"

Knowledge Check

Name that Git Command (2)

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"

Knowledge Check

Name that Git Command (3)

remote: origin

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   branch develop
   commit id: "34ad12"

Knowledge Check

Name that Git Command (3)

local

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"

remote: origin

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   branch develop
   commit id: "34ad12"

Knowledge Check

Name that Git Command (4)

local

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"

remote: origin

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"

Knowledge Check

Name that Git Command (4)

local

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"

remote: origin

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"

Knowledge Check

Name that Git Command (5)

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   branch develop
   commit id: "4c0f818"
   checkout main
   commit id: "14359ab"
   commit id: "a2b55ce"

Knowledge Check

Name that Git Command (5)

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "14359ab"
   commit id: "a2b55ce"
   branch develop
   commit id: "4c0f818"

Common Workflow

fork a repo

remote: origin

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"

Common Workflow

fork a repo

remote: fork

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"

remote: origin

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"

Common Workflow

clone the fork

remote: fork

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"

Common Workflow

clone the fork

local

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"

remote: fork

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"

Common Workflow

branch to create a new feature

local

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"

Common Workflow

branch to create a new feature

local

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch new_feature

Common Workflow

Write some code, add, commit

local

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch new_feature

Common Workflow

Write some code, add, commit

local

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch new_feature
   commit id: "a342bc"
   commit id: "adeb12"

Common Workflow

push new code to fork

local

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch new_feature
   commit id: "a342bc"
   commit id: "adeb12"

remote: fork

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"

Common Workflow

push new code to fork

local

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch new_feature
   commit id: "a342bc"
   commit id: "adeb12"

remote: fork

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch new_feature
   commit id: "a342bc"
   commit id: "adeb12"

Common Workflow

Submit a pull request to add changes to origin/main.

Get it approved!

Common Workflow

squash commits on branch

remote: fork

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch new_feature
   commit id: "a342bc"
   commit id: "adeb12"

Common Workflow

squash commits on branch

remote: fork

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch new_feature
   commit id: "ba34dc"

Common Workflow

merge remote/new_feature into origin/main

remote: fork

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch new_feature
   commit id: "ba34dc"

remote: origin

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"

Common Workflow

merge remote/new_feature into origin/main

remote: fork

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   branch new_feature
   commit id: "ba34dc"

remote: origin

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"
   commit id: "ba34dc"

Git Ignore

Git can ignore certain files, or patterns, with a single file called “.gitignore”.

# pycharm
.idea

# Ipython stuff
.ipynb_checkpoints

# linux/osx garbage
*~
*.swp

Branching Strategies: Versioning

# MAJOR.MINOR.PATCH
e.g, 1.2.3
  • CalVer is also becoming popular
# YYYY.MINOR.MICRO (Unity)
# YY.MINOR.MICRO (pip)
# YY.MM (Ubuntu)
# YY.MINOR.MICRO (boltons)

Branching Strategies

  • Main only
  • Single Trunk
  • Double Trunk

Branching: Main Only

  • Working by yourself
  • You only need checkpoints not branches
  • Easiest strategy


gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a"
   commit id: "4c0f818"
   commit id: "221d22"

Branching: Single Trunk

  • Features and Bug fixes in separate branches
  • All branches merge back to main
  • Version bump depends on recent commits
  • Main is always deployable

gitGraph
   commit id: "9d6b7c"
   commit id: "046a7a" tag: "v1.0.1"
   branch bug_fix_1
   commit id: "6bc3a3"
   commit id: "f23da3"
   checkout main
   branch feature_1
   commit id: "4c0f818"
   commit id: "221d22"
   commit id: "ac1d25"
   checkout main
   merge bug_fix_1 tag: "v1.0.2"
   merge feature_1 tag: "v1.1.0"

Branching: Double Trunk

  • Features and Bug fixes in separate branches
  • Features merge to develop, bug fixes to main
  • Bug fix releases are done on main
  • Develop is merged into main for feature releases
  • Main is merged into develop

Branching: Double Trunk


gitGraph
   commit id: "ab3490"
   branch develop
   commit id: "ac231c"
   commit id: "0931dc"

   checkout main
   commit id: "046a7a" tag: "v1.0.0"
   branch bug_fix_1
   commit id: "6bc3a3"
   commit id: "f23da3"
   checkout main
   merge bug_fix_1 tag: "v1.0.1"
   checkout develop
   branch feature_1
   commit id: "4c0f818"
   commit id: "221d22"
   checkout develop
   merge feature_1
   checkout main
   merge develop tag: "v1.1.0"

Summary: the Tools

  • add - Stage changes
  • commit - Save changes (with metadata)
  • clone - Download remote repo
  • checkout - Move HEAD to specific branch/commit
  • branch - Create new branch
  • merge - Merge branches
  • pull - Get changes from remote
  • fetch - Get branch/tags from remote
  • push - Push changes to remote