Git Basics

ferris

Git Basics

To understand git use the terminal

Once you understand git, use your IDE functionality

git config

Local vs Global

git config --local
git config --global

user and email

git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
git config --global --list

This is the bare minimum config to be able to commit

but wait there's more

man git config

git init

You might get a warning about branch naming.

git config --global init.defaultBranch main 

git init

Everything in git is tracked using files in .git

As long as you dont remove or modify files manually inside .git it is generally difficult permanently to lose work (in tracked files)

commits

  • Itentified by SHA (Secure Hashing Algorithm)
  • Sha generated with your name, email, time, commit msg
    • aka unique and ordered
  • Each commit* has at least a parent generating a graph (DAG)

Making a commit

Making a commit

Files get added to the staging area

Making a commit

A commit integrates changes in the staging area into the git history

Exercise

  1. Initialize a new repository
  2. Create two commits with two files
    • Remember to add the files prior to making the commit

Branches and HEAD

Branch pointers get updated as we generate commits

Remote

Creating a new branch

git branch

git branch shortcuts

git branch

Branches are just updatable pointers

diverging branches

where's my_new_branch and its last commit?!

git log --graph --all

main and my_new_branch have diverged

git merge

merge-commits have two parents

git merge

What will happen here when we git merge main?

git merge

  • Not all merges will result in a merge-commit
  • Sometimes merges can be done with a fast-forward merge

moving between branches

Note how HEAD is being updated on each switch

Exercise

  1. Make a fast-forward merge
    • Your commits should not diverge to be able to fast-forward
  2. Make a merge-commit
    • Your commits should diverge so that a merge-commit is necessary
    • Use different files in your commits to avoid merge-conflicts (we are not there yet!)

fetching from remote

fetching from remote

  • Fetching allow us to inspect the state of the upstream
  • It will not affect our work

But, can we inspect those changes prior to merging them?

checking out commits

checking out commits

checking out commits

Exercise

  1. Travel through your commit history
  2. Inspect on each commit the contents of the working directory
  3. Once you are done, switch back to your main branch

merge-conflicts

git pull is equivalent to fetching and merging

merge-conflicts

merge-conflicts

You can use git merge --abort to start over

Exercise

  1. Generate a conflict
    • Use git status to track to what branches you are commiting
  2. Make a merge-commit solving the conflict
    • Don't use your IDE functionality, use the raw text to solve the conflict

git reset

git reset [commit]

You can use HEAD~N to refer to the Nth commit

git reset

After the reset commit 0bad is dangling.

No branch points to it and no one has it as its parent. Did we lose our work?

git reflog

git reflog shows the history of the HEAD and can help us reach dangling commits

Exercise

  1. Create a new branch, then make a commit in it such that it has as its parent the first commit in your history.
  2. Merge this new branch into your main
    • Use git reset if you did it backwards ;)

git tag

  • Tags are inmobile pointers to commits
  • Frequently used to indicate versions and releases

git revert

revert-commits apply the inverse change of the commit they revert

cheatsheet. moving in history

git checkout [commit|branch] Move to a [commit] or [branch]
git reset [commit] Reset branch to [commit]
git reflog History of HEAD
git log --oneline --graph --all Represent repo in graph form

cheatsheet. Misc

git checkout [file] Remove changes with respect to HEAD in [file]
git reset --hard Clear all changes in tracked files
git diff -w Show difference ignoring white-space

checkout and reset --hard can result in loss of work if the changes removed have not been commited anywhere

Tips. TLDR

but wait there's more

git rebase rebase can prevent merges by replaying changes, making histories more linear
git cherrypick with cherrypick you can apply changes from specific commits instead of whole branches
git worktree with worktrees you can have several branches/commits checked out

Learning Resources

Where to find me