Skip to main content

Git basics

Cautions​

  • Do not change filename casing, they will be ignored
    • Even if override with git config core.ignorecase false, the build pipeline is likely to have a problem later on

Git software​

https://git-scm.com/

Command references https://git-scm.com/docs

Install/update​

check version and download: https://git-scm.com/download/win

keep clicking “next” :)

Configure git author​

  • The config file is stored under [user]/.gitconfig on Windows
git config --global user.name "[user]"
git config --global user.email "[email]"

Set credentials​

Use a Git Credential Manager to avoid getting asked for credentials every time

Basic commands​

Untrack a file​

git rm [file]

Push​

git push -u origin master

Delete all​

remove-item -force .git

View commits​

https://fossbytes.com/git-stats-git-visualization/

git log vs. git reflog:

  • git log sees the commit log
  • git reflog sees the local undo history
    • a local git history which is pruned after 90 days by default
    • refs imply not just the commit but the entire history behind it.
    • anything ever committed will be available
    • you can use the reflog to see where you were before and git reset --hard back to that ref to restore

Edit commits​

If you do git reset --hard <SOME-COMMIT> then Git will:

  1. Set the current branch to <SOME-COMMIT> .
  2. Then make the files in your working tree and the index ("staging area") the same as the versions committed in <SOME-COMMIT>

To remove not yet pushed commits from git, run

git reset --hard HEAD^

Squash commits​

Switch to a previous commit​

Assume the commit number is 40fd

  • git reset if wants to delete commits after 40fd
  • git revert if wants to add new commits that undo the commits from 40fd to master
    • This is the better way if branch is already accessible by others

git rebase​

  1. rebase onto the target branch
    1. Continue rebase: git rebase --continue
    2. Stop rebase: git rebase --abort
    3. Auto squash: git rebase --autosquash
  2. Solve conflicts
  3. Force push to fix the git diff

Remotes​

Pull latest​

  • In VS code
    • Pull to update to current fork
    • “Pull from” to pull from “upstream” (where the original project sits)
  • Manually
    • git pull --rebase upstream master

Resolve conflict​

  • Want the local code: accept mine (yours)
    • Ours/mine/HEAD/current change: branch merged/rebased to, +<<< , on top
  • Want the committed code in repo: accept theirs
    • Theirs/incoming change : branch merged/rebased from, +>>>, on bottom