~/.gitconfig - config file location.
Most of the time it can be modified by git config --global <configsection>.<configname>
link: https://forum.freecodecamp.org/t/push-a-new-local-branch-to-a-remote-git-repository-and-track-it-too/13222
-> go to a prefered branch
-> git branch <new branch name>
OR
-> git checkout -b <new branch name>
How to undo (almost) anything with Git
link: https://github.blog/2015-06-08-how-to-undo-almost-anything-with-git/
git reset HEAD~ this will undo last commit and changes will be unstaged
link: https://stackoverflow.com/questions/927358/how-do-i-undo-the-most-recent-local-commits-in-git
git add * stage all files
git add ./my-file.txt stage single file
git reset * unstage all the files
git reset ./my-file.txt unstage single file
first stage the modified content, then:
git commit --amend --no-edit
It will add modfied content to existing commit (last commit)
git show <commit>git show HEAD ## for last commitgit show HEAD^2- show commit details for merge commits
OR using git diff
git diff commit-ish~ commit-ishex:git diff HEAD~ HEAD
git diff <old-commit><new-commit>- find common base branch for 2 commits:
git merge-base <commit1> <commit2>
Note: show commit details for merge commits: https://stackoverflow.com/questions/40986518/how-to-git-show-the-diffs-for-a-merge-commit
--force-with-lease is safer option than --force
--force - overwrites remote, remvoes other teammates changes if any.
--force-with-lease - overwrites remote, but does not remove other teammates changes if any.
- revert back to previous commit after pushing it to remote:
git revert --hard Head~1
git push --force- git revert back to some commit id after pushing to remote:
git revert --hard <commit-id>
git push --forcegit push -u origin "<branch-name>"git config --local --list
git config --global --list
git config --local user.name "Foo Bar"
git config --local user.email "foo.bar@example.com"
git config --local --unset user.name
git config --global --edit # manually editing configgit config --global alias.cl "clone"
git config --global alias.fe "fetch"
git config --global alias.ps "push"
git config --global alias.psf "push --force-with-lease"
git config --global alias.pl "pull"
git config --global alias.bi "bisect"
git config --global alias.bis "bisect start"
git config --global alias.big "bisect good"
git config --global alias.bib "bisect bad"
git config --global alias.bire "bisect reset"
git config --global alias.biru "bisect run"
git config --global alias.bil "bisect log"
git config --global alias.br "branch"
git config --global alias.ch "checkout"
git config --global alias.chb "checkout -b"
git config --global alias.co "config"
git config --global alias.col "config --local"
git config --global alias.cog "config --global"
git config --global alias.coll "config --local --list"
git config --global alias.cogl "config --global --list"
git config --global alias.colu "config --local --unset"
git config --global alias.cogu "config --global --unset"
git config --global alias.cole "config --local --edit"
git config --global alias.coge "config --global --edit"
git config --global alias.cm "commit"
git config --global alias.cmm "commit -m "
git config --global alias.cma "commit --amend"
git config --global alias.cmane "commit --amend --no-edit"
git config --global alias.cp "cherry-pick"
git config --global alias.lg "log"
# git config --global alias.lgo "log --oneline --decorate --all --graph"
# git config --global alias.lgo11 "log --oneline --decorate --all --graph -11"
git config --global alias.lgo 'log --oneline --color=always --format="%C(auto)%h %<(15,trunc)%an %ad %s %Creset" --date=format:"%Y-%m-%d"'
git config --global alias.lgod 'log --oneline --color=always --format="%C(auto)%h %<(15,trunc)%an %ad %d %s %Creset" --date=format:"%Y-%m-%d"'
git config --global alias.lgoM 'log --oneline --color=always --format="%C(auto)%h %<(15,trunc)%an %ad %s %Creset" --date=format:"%Y-%m-%d" --invert-grep --grep="Merge"'
git config --global alias.lgodM 'log --oneline --color=always --format="%C(auto)%h %<(15,trunc)%an %ad %d %s %Creset" --date=format:"%Y-%m-%d" --invert-grep --grep="Merge"'
git config --global alias.lgomy 'log --oneline --color=always --format="%C(auto)%h %<(15,trunc)%an %ad %s %Creset" --date=format:"%Y-%m-%d" --author="__NAME_HERE__" --invert-grep --grep="Merge"'
git config --global alias.lgo11 'log --oneline -11 --color=always --format="%C(auto)%h %<(15,trunc)%an %ad %d %s %Creset" --date=format:"%Y-%m-%d"'
git config --global alias.lgo22 'log --oneline -22 --color=always --format="%C(auto)%h %<(15,trunc)%an %ad %d %s %Creset" --date=format:"%Y-%m-%d"'
git config --global alias.rb "rebase"
git config --global alias.ss "status"
git config --global alias.sh "stash"
git config --global alias.shm "stash push -m"
git config --global alias.shp "stash pop"
git config --global alias.shl "stash list"
git config --global alias.shs "stash show -p"
git config --global alias.shd "stash drop"
git config --global alias.shd0 "stash drop 0"
git config --global alias.sw "show"
git config --global alias.swh "show HEAD"
git config --global alias.di "diff"
git config --global alias.dis "diff --staged"
git config --global alias.di- "diff --"
git config --global alias.dis- "diff --staged --"
git config --global alias.smdf "submodule deinit -f ."
git config --global alias.smui "submodule update --init"
git config --global alias.undo "reset --soft HEAD~1"git-delta
example usage:
[core]
pager = delta
[interactive]
diffFilter = delta --color-only --features=interactive
[delta]
features = decorations
side-by-side = true
[delta "interactive"]
keep-plus-minus-markers = false
[delta "decorations"]
commit-decoration-style = blue ol
commit-style = raw
file-style = omit
hunk-header-decoration-style = blue box
hunk-header-file-style = red
hunk-header-line-number-style = "#067a00"
hunk-header-style = file line-number syntax
To get diff from github, put .diff at the end of PR's url
## using `patch` command (works fine for suckless patches)
patch -N --verbose -i mychanges.patch
## using `git apply` command (usually don't work with suckless patches)
git apply --stat mychanges.patch
git apply --check mychanges.patch
git apply --reject --whitespace=fix mychanges.patch
# reverting an applied patch
git apply -R <patch>- link: https://stackoverflow.com/questions/2249852/how-to-apply-a-patch-generated-with-git-format-patch
- link: https://stackoverflow.com/a/15375869
git cherry-pick commitSha
master - a - b
`- feature - c - d - e
git checkout master; git cherrypick e;
master - a - b - e
`- feature - c - d - e link: https://stackoverflow.com/q/10418975
- Checkout Windows-style, commit Unix-style (recommended for windows)
git config --local core.autocrlf true - Checkout as-is, commit Unix-style (recommended for linux)
git config --local core.autocrlf input - Checkout as-is, commit as-is (not conversion by git)
git config --local core.autocrlf false
find . -type f -exec dos2unix {} \;- here:
.is current directory,-type fis all file,\;is end of exec statement - stackoverflow: https://stackoverflow.com/questions/7068179/convert-line-endings-for-whole-directory-tree-git
link: https://docs.github.com/en/pull-requests/committing-changes-to-your-project/creating-and-editing-commits/changing-a-commit-message Displays a list of the last 3 commits on the current branch
$ git rebase -i HEAD~3
pick e499d89 Delete CNAME
pick 0c39034 Better README
pick f7fde4a Change the commit message but push the same commit.Replace pick with reword before each commit message you want to change (in vi editor).
pick e499d89 Delete CNAME
reword 0c39034 Better README
reword f7fde4a Change the commit message but push the same commit.then force push
git push --force origin example-branchgit stash clear
git log --pretty=format:"%h%x09%an%x09%s" --ancestry-path <commit-ish-old>..<commit-ish-new> -- path/to/file/or/folder
git log --oneline -11 --color=always --format="%C(auto)%h %<(15,trunc)%an %ad %d %s %Creset" --date=format:"%Y-%m-%d"
# search by commit message
git log --grep="<part-of-commit-msg>"
# search by commit message - case insensitive
git log --grep="<part-of-commit-msg>" -i
## search by file content (search for deleted lines)
# search by file content
git log -S <string> [path/to/file]
# search by file content using regex
git log -G <regex> [path/to/file]
# search by author
git log --author="<author-name>"git for-each-ref --format='%(authordate:short) %09 %(authorname) %09 %(refname)' --sort=authordate | grep <author_name>
git rebase <base>- this will rebase current branch on top of . Here is commit-sh.- reference-atlassian-git-rebase
- ex:
# Create a feature branch based off of main git checkout -b feature_branch main # Edit files git commit -a -m "Adds new feature" # git rebase <base> git rebase main
- https://docs.github.com/en/get-started/using-git/splitting-a-subfolder-out-into-a-new-repository
- git-filter-repo
- link to python script (raw):
https://raw.githubusercontent.com/newren/git-filter-repo/main/git-filter-repo - save to
git-filter-repo.py - running:
pyhton git-filter-repo.py
- link to python script (raw):
link: https://github.com/conventional-changelog/commitlint
please check ssh.md for details
# adding a submodule
git submodule add <git-repo-clone-url> <name>
git submodule add git@exaple.com
## resetting all submodules (delete all submodules & clone again)
git submodule deinit -f .
git submodule update --init
## updating submodules commits to latest (best way to update submodules)
git submodule update --remote --merge- link: https://stackoverflow.com/questions/2706797/finding-what-branch-a-git-commit-came-from
git branch -a --contains <commit>git reflog show --all | grep a871742git log --merges <commit>..- subsequent merge commit
git branch -m main master # main --> master in local
git branch # verify
git push -u origin master # create new branch in remote
# make master as default branch in remote (github)
git push origin :main # delete main branch in remote# delete local tag 'v1.0.0'
git tag -d v1.0.0
# delete same tag ('v1.0.0') in remote (like github)
git push origin :refs/tags/12345HEAD^1 and HEAD^2 are used with merge commits to specify which parent you're referring to.
HEAD^1: Refers to the first parent of the merge commit. This is typically the branch you were on when you initiated the merge.HEAD^2: Refers to the second parent of the merge commit. This is the branch that was merged into the current branch.
So, in short, HEAD^1 is like looking at your past, and HEAD^2 is like peeking into the branch you just merged.