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
- Even if override with
Git software​
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 loggit 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
- a local git history which is pruned after
Edit commits​
If you do git reset --hard <SOME-COMMIT>
then Git will:
- Set the current branch to
<SOME-COMMIT>
. - 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​
- Easiest way is just to use “Squash and commit” button on repo webpage
- Don’t use “interactive rebase” (
git rebase -i
) since there could be too many commits - https://gitbetter.substack.com/p/how-to-squash-git-commits
- https://gist.github.com/patik/b8a9dc5cd356f9f6f980
- https://www.git-tower.com/learn/git/faq/git-squash/
Switch to a previous commit​
Assume the commit number is 40fd
git reset
if wants to delete commits after40fd
git revert
if wants to add new commits that undo the commits from40fd
to master- This is the better way if branch is already accessible by others
git rebase
​
- rebase onto the target branch
- Continue rebase:
git rebase --continue
- Stop rebase:
git rebase --abort
- Auto squash:
git rebase --autosquash
- Continue rebase:
- Solve conflicts
- Force push to fix the git diff
Remotes​
- Add remote
git remote add upstream [url]
git remote set-url origin [url]
- View remote
git remote -v
- Add multiple 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