Skip to content

Commit

Permalink
(feat) Updated the git command blog.
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveLeungYL committed Nov 27, 2024
1 parent f93f640 commit d086006
Showing 1 changed file with 18 additions and 28 deletions.
46 changes: 18 additions & 28 deletions content/post/Useful_Git_Commands.md
Original file line number Diff line number Diff line change
@@ -1,74 +1,64 @@
---
title: Useful Git Commands
date: 25 Aug 2019
date: 2024-11-26
categories:
- General
tags:
- blogs
- random_notes
---

`git log`: Output git commits history.

`git log --graph `: Output git commits history in graph mode.
`git add <filename>`: Add files to the git tracking and committing system.

`git status`: Check Git Status

`.gitignore`: Input files that will be ignored by Git. **But if files are already inside the git tracking system, it won't delete it from git. We have to use `git rm filename` to remove it.**

`git diff`: check file changed that is not staged.

`gif diff --staged`: check file changed that is staged.

`git add --all`: add all changes to the git tracking.

`git commit`: Submit changes to git.

`git commit -a`: Submit changes with all changes. Still need to call `git add all`

`git commit --amend`: Revert and re-commit the last git submission.

`git rm –cached < filename >`: Delete file from git submission. But does not delete local file.

`git reset – < file >`: change the current HEAD pointers to previous commits. Depends on --hard or --soft, will or will not change the local file.

`git checkout -- <file>`: Change to local file to specific commits. Does not move HEAD pointer in Git.

`git remote -v`: Output remote server and its URL.

`git remote add <short_name> <url>`:

`git remote rm <short_name>`:

`git fetch <remote-name>`: Receive the remote server's update. Will put into <remote-name>/branch.

`git pull`: Automatically fetch and then merge into current.

`git push <remote-name> <branch-name>`: push to the remote server.

`git push <remote-name> --tags`: push to the remote server with tags information.

`git tag -d <tagname>`: Delete tags information

`git checkout master`: Checkout the master branch.

`git merge <other_branch_name>`: Git will decide which branch will become its ancestor.

If we have conflicts in merge:
`git mergetool`: GUI tool used to fix conflicts. Then use `git merge` to merge.

`git mergetool`: GUI tool used to fix conflicts. Then use `git merge` to merge.
`git branch`: Output all branches information.

It is OK to repeatedly merge other branches to one branch. For example, we can repeatedly merge *test_branch* to *master* branch.

`git push origin --delete <branch_name>` Delete remote branch.

`git rebase -i HEAD~3` Change the last three commits, very useful tool to re-generate commits history.

### Added after March, 2023:

`git worktree add <path> <branch_name>`: Checkout another branch in a separate folder. The current branch folder and the new checkout folder will co-exist, and sync with the updates (for different branches).
`git worktree list`: List all currently checkout work trees.
`git worktree remove <worktree>`: Remove the work tree from the file system.

### Signing related:

`gpg --list-keys` && `git config --global user.signingkey <your gpg key id>`: Check you gpg key and import it to your git. Make sure the gpg key email matches your git registered email.
`git config --global commit.gpgsign true`: Auto sign by default.

`git worktree list`: List all currently checkout worktrees.
### Just interesting new command:

`git worktree remove <worktree>`: Remove the worktree from the filesystme.
`git push --force-with-lease`: Force push if the reference tree is not changed by others. Safer force push.
`git diff --word-diff`: Word diff instead of line diff.
`git config --global branch.sort -committerdate`: Sort branch based on commit date instead of alphabetic.
`git maintenance start`: On your most commonly used commit, auto run git maintenance jobs such as prefetching periodically.
`git clone --filter=blob:none`: Download the git commit tree and the latest blob, but skipped the history blob at the time. Git will fetch for blob if you ask for history commit data.

If you are working with super large monorepo, you can use `scalar` command, an alternative `git` command that wraps up some `git` commands optimized for large repos.

0 comments on commit d086006

Please sign in to comment.