Git¶
Git GUI¶
Github Desktop¶
- simple
- fast
- intuitive
- only core features
GitKraken¶
- many features
- nice UI
- slow
login¶
repo¶
view all repos¶
create repo¶
pull related¶
git pull all subdiretories¶
commands¶
- login
gh auth login
- create repo
gh repo create
history¶
https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History
git log
git reflog
- each commit oneline
git log -p abcdef
- commit details of
abcdef
- commit details of
maintenance¶
optimize your git repo
might free up some space
Submodule¶
Add a submodule
Update all submodule
Remove a submodule
line endings lf & crlf¶
check the eol of a file¶
cat -A <file>
- CRLF -> end with
^M$
- LF -> end with
$
- CRLF -> end with
file <file>
auto eol translation¶
with gitattributes¶
auto translation for files when checked out (into LF for Unix, CRLF for Windows)
with core.autocrlf¶
git config --global core.autocrlf true
for Windows- convert to LF when commit
- convert to CRLF when checkout
git config --global core.autocrlf input
for Unix- convert to LF when commit
convert entire directory to LF¶
(there might be some redundant steps but idk & idc)
auto translation for files when checked out (into LF for Unix, CRLF for Windows)
renormalize current directory
git rm --cached -r . # Remove every file from git's index.
git reset --hard # Rewrite git's index to pick up all the new line endings.
- main refs
- supplementary refs
Working on a branch¶
Integrate new changes into your dev branch¶
In your dev branch
e.g.
Make your branch identical to a branch¶
e.g.
Stash¶
Stash all changes¶
-u
to include untracked files
Show stashed entries¶
Retrieve stash files¶
The latest one would be 0
commit related¶
delete all commits¶
git update-ref -d HEAD
https://stackoverflow.com/a/6637891/15493213
reset¶
git reset HEAD~2
to reset to latest_commit.parent.parent
revert a certain file to a version¶
git checkout <sha> -- <path-to-file>
discard changes¶
relevant docs - https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository - https://git-scm.com/book/en/v2/Git-Basics-Undoing-Things
for tracked files¶
git reset HEAD --hard
git restore .
git checkout -- .
git checkout -- haha.txt
- unmodify the modified untracked file
haha.txt
- unmodify the modified untracked file
for untracked files¶
git clean -df
- clear all untracked files
git clean -f haha.txt
- clear the untracked file
haha.txt
- clear the untracked file
remove remote but keep local¶
how many insertions and deletions for a user¶
git log --pretty=format:'' --numstat --author 'dlccyes' | awk 'NF' | awk '{insertions+=$1; deletions+=$2} END {print NR, "files changed,", insertions, "insertions(+),", deletions, "deletions(+)"}';
all commits¶
git log
git reflog
- every action
git log --graph --decorate --oneline $(git rev-list -g --all)
- tree graph of your git and where HEAD is at
- commit counts
git rev-list HEAD --count
diff¶
git diff
- git diff with vimdiff
git config --global diff.tool vimdiff
set git tool to vimdiffgit difftool
to view- https://stackoverflow.com/a/3713803/15493213
filter-branch - delete a file in all commits¶
Note that git filter-branch
only creates extra commit and then reroute your git commit graph. The original commits are still there.
by user¶
- all commits
git shortlog
- commit counts
git shortlog -s -n
https://stackoverflow.com/questions/677436/how-do-i-get-the-git-commit-count
squash¶
using reset & amend¶
設你有最近 3 個 commit 為
- 9a7792
- 9a7791
- 9a7790
- 9a7789
想要把三個 commits squash 在一起
git reset --soft 9a7789
git commit --amend
OR 直接去 gui- new commit message
git push -f
- 此時 remote & local diverge若 pull 則會把 remote pull 下來剛剛步驟就白費了所以直接 force push
- 完成
ref
- https://www.burntfen.com/2015-10-30/how-to-amend-a-commit-on-a-github-pull-request option 3
- https://gitbook.tw/chapters/using-git/reset-commit.html about git reset
using rebase & squash¶
設你有最近 4 個 commit 為
- 9a7792
- 9a7791
- 9a7790
- 9a7789
想要把 9a7792 & 9a7791 squash 到 97790
git rebase -i 9a7790
- 出現 editor,把 9a7792 & 9a7791 改成 squash (注意這時較早的 commit 是在上面)
- 出現 editor,把 多個 message 改成單一個 message
git push -f
- 完成
ref
https://gitbook.tw/chapters/rewrite-history/merge-multiple-commits-to-one-commit.html
Credential¶
Store credentials as a text file¶
git config --global credential.helper store
- push or pull in your repo, enter your username & passphrase, then they will be saved to
~/.git-credentials
- next time you push or pull, you won't need to enter the credentials
https://stackoverflow.com/questions/35942754
Store credentials in Mac's keychain Access app¶
Mac will store your credentials there automatically, even if you use credentials.helper store
. So if you want to change your password/token, you'll need to go the the keychain Access app for it to take effect.
config related¶
git config --global XXX YYY
will add or change XXX's value to YYY in ~/.gitconfig
clone related¶
partial clone¶
Clone only a certain directory
git init
git remote add -f origin <url>
git config core.sparsecheckout true
echo <dir1>/ >> .git/info/sparse-checkout
echo <dir2>/ >> .git/info/sparse-checkout
echo <dir3>/ >> .git/info/sparse-checkout
git pull origin master
works like a charm
ref:
- https://stackoverflow.com/a/43902478/15493213
- pasta from https://stackoverflow.com/a/17075665/15493213
- pasta from http://jasonkarns.com/blog/2011/11/15/subdirectory-checkouts-with-git-sparse-checkout/
branch related¶
show branch¶
show all branch
show all remote/branch
create branch¶
delete branch¶
delete local branch
delete remote branch
if git branch -a
still show the deleted branch
rename branch¶
switch branch¶
set/switch tracking remote branch¶
https://stackoverflow.com/a/23458073/15493213merge branch¶
To merge branch_1
into branch_2
copy everything from another branch to current branch¶
https://stackoverflow.com/a/7188232/15493213
push to another branch¶
Say you're on branch_1
and want to push to branch_2
or
https://stackoverflow.com/questions/13897717
remote related¶
Check if you have access to a git repo¶
You can use
check remote¶
push to another remote¶
add remote¶
switch default tracking repo¶
https://stackoverflow.com/a/32469272/15493213
ssh method¶
You can use the remote starting with git@
and add the ssh public key to your github repo (or account) to use ssh method.
- generate ssh key (doc)
- open git bash / linux terminal
ssh-keygen -t ed25519 -C <email>
- press enter to til the end
eval ssh-agent -s
- should return
Agent pid <a number>
- should return
- copy the content in
~/.ssh/id_ed25519.pub
- add it your github repo github (doc)
- go to your repository (or account)
- settings
- deploy keys (or ssh key)
- add key
- paste your public key
- check "allow write"
tag¶
https://git-scm.com/book/en/v2/Git-Basics-Tagging
Only point at a specific commit, to indicate version for example.
A tag can't be used on multiple commits.
Delete a tag¶
from remote
Troubleshooting¶
Unsafe directory error¶
git addressed a security vulnerability and changed something to solve it in April, 2022
see https://github.blog/2022-04-12-git-security-vulnerability-announced/
As a result, git directories can't normally use git by non-owners.
Workarounds:
-
do git as the owner
-
Add directories to ignore in gitconfig
to add all subdiretories
git push hung¶
Restart ssh agent