Git


Commit tree

What is <tree-ish> some kind of tree structure i.e. dir or commit

Log

git log --since=2020-12-30
git log --until="3 days ago"
git log --after=2.weeks --before=3.days

git log -i --grep="thing" # <-- HOW TO SEARCH BASED ON COMMIT MESSAGE
git log b26f..HEAD
git log --author="ben"
git log fileName.txt

--format= <value>
<value>:
oneline
short
medium
full
fuller
email
raw

Tree

git ls-tree <tree-ish>
  examples:
    git ls-tree HEAD
    git ls-tree HEAD folderName/

Branches

Branch

git branch (returns all branches)
-r (remote) -a (all)

git branch newBranch (creates a new branch, will not checkout into the new branch)

git branch -u newBranch origin/develop (will track develop)
git branch --unset-upstream branchName (will untrack develop)

git branch --merged (what other branches are already merged into this branch)
git branch --no-merged (what other branches are not already merged into this branch)

git branch -m newBranchName
git branch -m oldBranchName newBranchName

git branch -d branchToBeDeleted

Checkout

git checkout newBranch
git checkout -b newBranch (creates a new branch and checkouts into it)

git checkout -- <fileName> (removes all the changes that you have made in the current branch on fileName)

Diff

git diff oldCommit..newCommit
git diff --color-words oldCommit..newCommit (just looks better and is easier to read trust me)

Reset

use here very handy: HEAD~1
its good practice to make a new branch before doing a reset so you don't get unlinked commits and loose them

Soft Reset

Brings all old commits/changes back with you into the staging area
Note: the code will still have the code from the new commits
you can go back to a new commit by using it's SHA


git reset --soft <tree-ish>

Mixed Reset (default)

the code will still have the code from the new commits

git reset --mixed <tree-ish>

Hard Reset

absolute reset all changes made in newer commits is removed

git reset --hard <tree-ish>

Merging a Branch

mas (current branch) - the one that's getting the new changes from another branch, in the following examples

bra - example branch that we want to get the new changes from


git merge bra

git branch --merged (what branches have been merged into your current branch)


<<<<<< HEAD
  current branch
======
  branch we are merging in


git show --color-words (shows the conflicts, red is current branch)
git merge --abort (reverts the merge completely)

Stash

Note: does not stash untracked changes

git stash save "name of stash"

git stash list

git stash show stash@{0} (-p will show the actual changes)

git stash pop (note it does remove it from the stash)

git stash apply stash@{0}

git stash drop stash@{1}

git stash clear

Remote

remoteName(origin)/master (your local copy of master from the remote server that tries to copy what is on the remote)

master (master that you are currently working on will not change when the remote master changes)

git remote (prints out all remotes)

git remote -v (shows all remotes with more details, i.e. which is push and fetch)

git remote add remoteName http://www.benlewisjones.com (adds a new remote)

git remote rm remoteName

git push -u origin master (-u makes it tell you if you are behind master)

git push origin :remote_branch_to_be_removed

git push origin --delete remote_branch_to_be_removed

git push origin feature:master

git clone https://... whatYouWantToCallIt

git fetch (updates the branches to what's going on at the remote)

git pull (git fetch + git merge)

Cherry pick

git cherry-pick <oldest_commit_sha>^..<newest_commit_sha>

Set text editor

git config --global sequence.editor "code --wait"

Work Tree

git worktree list

git worktree add ../repo-branch-name branch-name

git worktree add -b feature/my-thing ../repo-feature

git worktree add --detach ../repo-temp <commit-ish>

git worktree remove ../repo-feature

git worktree remove -f ../repo-feature

git worktree prune

git worktree move ../repo-old ../repo-new