Home

Git

Git can be thought of as a database of commits.

A commit is a snapshot of the entire project made of a pointer to this snapshot, metadata(who created it, when it was created, and the commit message), and a pointer backwards to the parent commit. This creates a chain of commits.

Git comrpresses files and if a file doesn’t change between commits, it’s only stored once.

Config

Config commands Description
git config list Lists out all of your configs.
git config set <section>.<key> <value> Sets the key for that section.
git config get <section>.<key> Gets the value for that section and key.
git config unset <section>.<key> Removes the key from that section.
git config remove-section <section> Removes a section and all the keys in that section.

Status

Status commands Description
git status Shows current state of your files.
git add <file/folder> Stages a file or folder
git add -A Stages all changes
git commit -m "message" Commits staged changes with a message.
git commit --amend -m "message" Change the last commit message.
git rm --cached <file/folder> Unstages a file/folder.

Branches

A branch is a named pointer to a commit. Committing to a branch updates it to point to the new commit.

Branch commands Description
git branch List all branches. * is your current branch.
git branch -m <oldname> <newname> Rename a branch.
git branch <name> Creates a new branch pointing to your current commit.
git branch <name> <commitHas> Creates a new branch pointing to the commit hash.
git branch -d <name> Deletes a branch.
git switch <branch> Switches to a branch.
git switch -c <name> Creates a new branch at the current commit and switches to it.

Merge

A merge commit combines changes from two branches into a single commit that has two parent commits.

Before:
A - B - C  main(current branch)
   \
    D - E  branch

After:
A - B - C - F  main(current branch)
   \     /
    D - E      branch
Merge commands Description
git merge <branch> Creates a merge commit on your current branch from the branch you specified.
Before:
      C   branch
     /
A - B     main(current branch)

After:
            branch
A - B - C   main(current branch)

Rebase

Rebase moves the commits from one branch onto the tip of another branch.

Before:
A - B - C   main
   \
    D - E   branch(current branch)

After:
A - B - C         main
         \
          D - E   branch(current branch)
Command Description
git rebase <branch> Rebases the current branch onto the specified branch.
Example with a merge:

Before:
A - B - C - E - F - G   main
          \   /
            D - H - I   branch(current branch)

After:
A - B - C - E - F - G         main
          \   /      \
            D         H - I   branch(current branch)

Reset and revert

These commands are used to undo commits.

Log

git log shows the history of commits.

Flags Description
--oneline Condensed view of commits.
--graph Draws lines for the path.
--decorate Give branch and tag information.
--all Commits reachable from all branches.
--parents Gives parent commit.
--author <regex> Searches for author.
git log --oneline --graph --decorate --all

Remote

Local repo setup

Commands Description
git clone <https/ssh> Copies an existing remote repo to your machine and initializes it.
git init Initializes a new empty local repo.
git remote -v Shows list of locations and their urls.
git remote add <location> <https/ssh> Adds a location.
git remote remove <location> Removes a loction.

Fetch and pull

Commands Description
git fetch <location> Updates metadata(trees/folders and blobs/files). Doesn’t update working directory.
git fetch By defualt it fetches from origin.
git merge <location>/<branch> Merges the remote location’s branch onto your current branch.
git push <location> <branch> Sends local changes to any remote location.
git pull <location> <branch> Fetches, then merges what was pulled into your current branch.
git pull --rebase <location> <branch> Fetches, then rebases your changes ontop of what was pulled.

Pull requests

Pull requests are a way to request merging changes from one branch into another on GitHub.

  1. Pull requests tab
  2. New pull request
  3. Set your branches so that the compare branch gets merged into the base branch.

Setting up ssh with GitHub

  1. ssh-keygen -t ed25519 -C <githubEmail>
  2. eval "$(ssh-agent -s)"
  3. inside file ~/.ssh/config
     Host *
         AddKeysToAgent yes
         IdentityFile ~/.ssh/id_ed25519
    
  4. Go to github settings -> SSH and GPG keys -> New SSH key
  5. Paste the contents of id_ed25519.pub into github and press Add Key
  6. To test if it worked run: ssh -T git@github.com and you should see Successfully Authenticated
  7. To use with SSH make sure your github repo locations are SSHs(they starts with git@github.com)

Workflow

Worktrees

https://youtu.be/ntM7utSjeVU?si=zyk8xC9Pii2EDW4z