Skip to content

Instantly share code, notes, and snippets.

@shnam7
Last active December 3, 2023 09:32
Show Gist options
  • Save shnam7/358c1029aba8670ebd5867383afff6ad to your computer and use it in GitHub Desktop.
Save shnam7/358c1029aba8670ebd5867383afff6ad to your computer and use it in GitHub Desktop.
Git command examples

Ancestry tree-ish

treeish: branch name, tag, SHA, HEAD, etc.

git show HEAD
git show HEAD^
git show HEAD^^
git show HEAD~1
git show HEAD~2

git ls-tree

git ls-tree wdk/

git commit

git commit -a                           // commit all (staging + non-staging)
git commit -am "msg"
git commit		                        // commit staging only
git commit -m "msg"

git restore

git restore <file>                      // restore working tree files

git log

git log -3
git log --since=2023-10-01
git log --until="3 days ago"
git log --after=2.weeks --after=3.weeks
git log --author="<user_name>"
git log --grep="<regex>"
git log <sha>..HEAD
git log <file>
git log -p              // show diff
git log --stat
git log --format=oneline | medium(default) | full | fuller | email | raw
git log --oneline
git log --graph
git log --graph --all --oneline --decorate
git log --onnline <branch_name>
git log -L 100,150:finename.txt         // show changes to lines 100~150 in filename.txt
git log -L 100,+50:finename.txt

git branch

git branch <new_branch_name>
git switch <new_branch_name>
git branch --merged                     // show list of all the merged branches into this branch
git branch --no-merged	                // show list of all the branches not merged into this branche
git branch -m <old_name> <new_name>     // change branch name
git branch -m <new_name>                // change current branch name to <new_name>
git branch -d <branch_name>             // delete branch
git branch -r                           // show remote branches
git branch -a                           // show both local and remote branches
git branch -u origin/branch branch      // -u (--set-upstream-to) enables traking
git branch -unset-upstream branch       // disables traking

git branch -r --merged                  // applies to remote. -r suould com first
git branch --merged july_release        // show merged branched since july_release
git branch --merged b3225a7c49          // show merged branched since b3225a7c49

git switch

git swithc <branch>
git switch -c <new_branch>              // create and switch to new branch (git checkout -b new_branch)

git checkout

git checkout -b <new_branch_name>
git checkout -n <new_branch> <starting_branch>
git checkout -- <file>...	            // discard changes in working directory for the <file>

git diff

git diff master..<branch>
git diff --color-words master..master^

git reset

git reset --soft <tree-ish>		        // soft: keep the change history in staging area. keep working dirtectory (similar to git commit --amend)
git reset --mixed <tree-ish>	        // mixed(this is default) clear change history. keep working directory
git reset --hard <tree-ish>	            // hard: clear change history. reset working directory

git merge

git merge <branch>		                // merge <branch> into this branch

git rebase

git rebase main                         // rebase current branch to tip of main
git rebase main <branch>                // rebase <branch> to tip of main
git rebase --onto <newbase> <upstream> <branch>
git rebase -i main new_feature          // rebase interactively
git rebase -i HEAD~3                    // rebase last 3 commits onto the same branch interactively)

# example - rebase
git switch -c new_feature               // create a new branch
git commit -am "Add commit #1"          // do commits after making some changes
git commit -am "Add commit #2"
git log --graph --all --decorate --oneline  // show repo tree
git merge-base main new_feature         // check SHA of the merge point(e7fd1a)
git rebase main                         // rebase the two commits to main
git merge-base main new_feature         // check show commit point(e7fd1a) where new_feature branch diverged

# example - roll back to the original merge point (undoing the rebase earlier)
git rebase --onto e7fd1a main new_feature
git log --graph --all --decorate --oneline  // show repo tree

# example - rebase to other branch
git rebase --onto ecommerce main new_feature // rebase to the top of ecommerce branch

git stash

git stash save <stash_name>
git stash -u save 	                    // include untracked files into stash
git stash list
git stash show stash@{0}                // show 0th stash
git stash show -p stash@{0}             // use -p option to show diff
git stash pop
git stash pop stash@{0}
git stash apply
git stash drop stash@{0}
git stash clear				            // delete allstashes

git remote

git remote                              // show remote list
git remote -v                           // verbose (show remote url)
git remote show
git remote show origin
git remote add <name> <url>
git remote add origin "https://..."
git remote rm origin                    // remove remote "origin"
git branch -r                           // list remote branches
git remote set-url origin <url>         // set url of the origin
git remote prune origin                 // delete stale remote-tracking branches
git remote prune origin --dry-run       // dry-run to preview

git clone

// clone automatically tracks the remote
git clone https://github.com/shnam7/ardukit.git <folder_name>

git fetch

// get changes from remote, but does not merge into local copy
git fetch                               // fetch branches and tags, from origin if there's no upstream branch of current branch
git fetch <repo>

git fetch --prune                       // prune stale branches before fetch
git fetch --p

git fetch --tags                        // fetch tags only

git push

git push
git push -u origin master
git push -u origin local_branch         // push local branch to origin with tracking enabled
git push branch:branch                  // push local branch to remote branch
git push :branch_to_delete              // colon means deleting branch --> old way of deting branch
git push -d origin branch_name          // recommended way of deleting
git push --delete origin branch_name     // recommended way of deleting

git push origin <tag>                   // push tag to origin
git push origin --tags                  // push all the tags to origin
git push -d origin <tag>                // delete remote tag

git pull

git pull                                // equivalent to git fetch + git merge
git pull --r                            // pull as rebase
git pull --rebase
git pull --rebase=preserve              // preserve merge commits, not fltten them
git pull --rebase=interactive           // use interactive mode
interact
# use interactive mode
```sh
git tag <tag> <SHA>                     // light tag
git tag issue_136 655da716e7

git tag -am "Version 1.0" v1.0 dd5c49428a0  // annotated tag (more common)

git tag -d v1.0
git tag --delete v1.0

git tag                                 // list tag alphabetically
git tag --l
git tag --list
git tag list -l "v2*"
git tag -n                              // list tag with annotations (1 line only)
git tag -n5                             // list tag with annotations up to 5 lines

git bisect

git bisect start
git bisect good <treeish>
git bisect bad <treeish>
git bisect reset

git config

git config --global alias.st "status"   // alias for "git st"
git config --global fetch.prune true    // always prune before fetch

Other git commands

# interactive mode
git add -i
git add --interactive

# patch mode (staging portions of file)
git add -p
git add --patch
git diff --cached                       // show changes made to to diff
``
git cherry-pick <SHA>                   // bring changes in <SHA> as new commit of current branch
git cherry-pick <SHA1>..<SHA2>          // cherry-pick the changes between <SHA1 and <SHA2>
git cherry-pick -e <SHA>                // cherry-pick with editing commit messages
git cherry-pick -edit <SHA>             // cherry-pick with editing commit messages

# create/apply diff file
git diff <from_commit> <to_commit> > out.diff
git apply out.diff

# create/apply patch in unix email format
git format-patch 2e33d..655da           // export all commits in the range
git format-patch main                   // export all commits in current branch that are not in main branch
git format-patch main..HEAD
git format-patch -1 655da               // export a single commit
git format-patch 2e33d..655da -o outfile
git format-patch 2e33d..655da --stdout > outfile.patch
git am feature/001-some-name.patch      // am means apply-mailbox. this creates nnew commit in current branch.
git am feature/*.patch                  // apply all the .patch file n the directory (in the right order)

git blame filename.txt                  // annotate file withn commit detals
git blame -w filename.txt               // ignore whitespaces
git blame -L 100,150 filename.txt       // annotate lines 100-150
git blame -L 100,+5 filename.txt        // annotate lines 100-105
git blame d9dba0 filename.txt           // annote file at revision d9dba0
git blame d9dba0 -- filename.txt        // annote file at revision d9dba0 (same as above?)

# prune and gc is automacally performed by git. No manual execution required.
git prune                               // prune all unreachable objects (totally different from git fetch -p)
git gc                                  // part of garbage collection

aliase

// Add alias "st=status" to ~/.gitconfig
git config --global alias.st "status"
git st

~/.gitconfig entries example

[color]
    ui = true
[alias]
    st = "status"
    co = "checkout"
    ci = "commit"
    br = "branch"
    df = "diff"
    dfs = "diff --staged"
    logg = "log --graph --decorate --oneline --all"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment