STAY INFORMED
following content serves as a personal note and may lack complete accuracy or certainty.

Minimal-Mistakes instruction
Useful vscode Shortcut Keys
Git Note

2 minute read

Commands

Command Description
git init Initialize a new Git repository
git config user.name 'henry' Set the username
git config user.email 'henry@gmail.com' Set the email address
git add [file name] Add file to staging area
git add [directory name] Add all changed files in directory to staging
git add . Add all changed files in working directory to staging
git reset [option] [file name] change HEAD point
git reset [option] HEAD^ change HEAD point to previous file
git reset [option] HEAD~[n] change HEAD point to nth previous file
git status Show the current status
git commit -m "message" Commit staged changes
git help [command name] Show help for a command
git push -u origin master Push to remote repository for the first time
git push Push changes to remote repository
git pull Fetch and merge changes from remote repository
git clone [URL] Clone a repository from GitHub
git log Show commit history
git log --pretty=oneline Show commit history in one line per commit
git show [commit ID] Show changes in a specific commit
git commit --amend Modify the last commit
git config alias.[alias] [command] Set an alias for a command
git diff [A] [B] Compare two commits
git reset [option] [commit ID] Reset to a specific commit
git tag [tag name] [commit ID] Tag a specific commit
git branch [name] Create a new branch
git checkout -b [name] Create and switch to a new branch
git branch -d [name] Delete a branch
git checkout [name] Switch to a branch
git merge [name] Merge a branch
git merge --abort Cancel a merge
git fetch Fetch latest commits from remote
git blame Show who made changes to each line of a file
git revert Revert a specific commit
git reflog Show a log of all references for HEAD
git log --all --graph Show all commits in a graphical format
git rebase [branch] Reapply commits on top of another base commit
git rebase --continue Continue after resolving conflicts during rebase
git stash Save changes temporarily
git stash apply [ID] Apply stashed changes
git stash drop [ID] Remove stashed changes
git stash pop [ID] Apply and remove stashed changes
git cherry-pick [commit ID] Apply a specific commit

My Setting

git config alias.history 'log --pretty=oneline' # assign history to the command
git config pull.rebase true # use rebase method if conflict happens when pull

# deactivate pager
git config --global pager.log false
git config --global pager.diff false
git config --global pager.branch false
git config --global pager.tag false

etc

  1. unstage command: git restore --staged .

  2. remove or modify commit command:
    • git reset --soft HEAD~1(keep stage)
    • git reset HEAD~1(unstage as well)
    • git reset --hard HEAD~1(unstage and undo all modified things)
    • git revert [commit-id](cancel pushed commit, this create new commit)
  3. If a conflict occurs (only main branch):

    • git stash
    • git pull
    • git stash pop(conflict could be happened)
    • Resolve any conflicts if they occur
    • git add .
    • git commit -m "Resolve conflicts and apply stashed changes"
  4. The normal Git workflow for collaboration is as follows:

    • git switch -c [new-branch-name] [commit ID (optional)]
    • Make your changes
    • git add . && git commit -m "Implement feature"
    • git switch main
    • git pull(to ensure main is up-to-date)
    • git merge [new-branch-name](conflict could be happened, do 3.4 - 3.6)
    • Resolve any conflicts if they occur
    • git push

Tags:

Categories:

Updated: