John Siu Blog

Tech - Business Tool, Personal Toys

Git Commands

☰ Table of Content

Some git commands.

Push

Branch

1
2
3
git push -u <name> <branch>
git push -u origin master
git push -u origin main

Remote

Show

1
git remote -v

Remove

1
2
git remote remove origin
git remote remove <name>

Add/Stage

1
2
git add . # Add everything in current dir tree. This include sub-dir recursively.
git add <file/dir>...

Remove From Stage

1
git rm <file/dir>...

Commit Staged Changes

1
2
3
git commit -m '<comment>'
git commit -a # Commit all staged files.
git commit <file/dir> # Commit files directly even not staged.

Tag

Add

1
2
3
4
5
git tag # List tags
git tag <version> # Light weight tag
git tag -a v0.1 -m "Version 0.1"
git tag -a <version> -m '<comment>'
git show <version> # Show tagged commit

Push to server

1
git push --tags

Delete

1
2
git tag -d <version>
git tag -d v1.0.1

Delete from server

Yes, it is a push command.

1
2
git push -d <version>
git push -d v1.0.1

Branch

List

1
2
git branch
git branch -l

Create

1
2
3
4
git branch <new branch>
git branch <new branch> <from branch>
git branch <new branch> <from tag>
git branch <new branch> <from commit>

Switch

1
2
3
git switch <branch>
# switch & create
git switch -c <branch>

Submodule

Add

1
2
git submodule add https://github.com/J-Siu/binario themes/binario
git submodule add <url> <path>

First Pull

1
git submodule update --init --recursive

Update

1
git submodule update --recursive --remote

Status

1
git status

Log

git log optionDescription
--name-onlyShow file in commit, following commit line
--oneline(Compact)Show ref and commit message in one line
<tag>..HEADShow log since <tag>
(Long)Default format

git reflog: Compact mode, one commit per line.

Reset

1
2
git revert --hard [<commit>]
git revert --soft [<commit>]
  • --hard Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded.
  • --soft Does not touch the index file or the working tree at all (but resets the head to <commit>, just like all modes do). This leaves all your changed files “Changes to be committed”, as git status would put it.

This is prefer over revert when backing out from local commit, before pushing to remote.

Revert

1
2
git revert <ref#>
git revert b68bc59

Config

User

1
2
git config --global user.name "<Full Name>"
git config --global user.email "<email>"

List

1
2
git config -l
git config --global -l

Tag Date

1
git log --tags --simplify-by-decoration --pretty="format:%ai %d" | cat

Fork Merge/Sync with upstream

1
2
3
git remote add upstream <up stream url>
git fetch upstream
git merge upstream/master

John Siu

Update: 2022-05-22
comments powered by Disqus