Git

Merge & Rebase

Merge

  • Merging is nice because it’s a non-destructive operation. The existing branches are not changed in any way.

Rebase

  • Rebase moves all of the commits in feature on top of main, it changes the commit history of feature to make the history more clean

  • Make sure the feature is only for your use, since branch’s history has diverged from everybody else’s.

  • After rebased , force push is needed in order to rewrite the history

Sub Module

Create SubModule

 git submodule add (--force) -b <branch> <git url>

Update SubModule

cd directory
git pull

Existing SubModule Initialization

git submodule init
git submodule update

Delete SubModule

git rm <directory>

Fork

  • Firstly, it can make a copy of project to your repo through forking

  • Then, clone the code on local

  • After that, execute the following commands to update the code from source if needed

# Add the remote, call it "upstream":

git remote add upstream https://github.com/whoever/whatever.git

# Fetch all the branches of that remote into remote-tracking branches

git fetch upstream

# Make sure that you're on your main branch:

git checkout main

# Rewrite your main branch so that any commits of yours that
# aren't already in upstream/main are replayed on top of that
# other branch:

git rebase upstream/main

Reset

  • To cancel the committed change and reset the head

Hard

  • Discard all the committed changes and staged/ unstaged changes

Mixed

  • Put back your committed changes and staged changes into unstaged changes

Soft

  • Only put back your commited changes into unstaged changes

Tracing

Diff

  • To compare the change of file

git diff <your file>

Log

  • To get the commit history of file

git log <your file>

Blame

  • To get file change and each commit line by line

git blame <your file>

Grep

  • To search the file based on keyword

git grep <keyword>

Reflog

  • To record all your operations on local, including changing branch

git reflog <branch>
  • If you want to recover some of operated changes, such as reset change, deleted branch change

  • You can checkout based on the history

git checkout <historyNum>

Revert & Cherry pick

  • Cherry pick and revert can both select the commit of content from other branch into current branch

  • But revert is to select the opposite version of the commit

Git Hook

  • Hooks can reside in either local or server-side repositories, and they are only executed in response to actions in that repository. Here is the list of action:

  • pre-commit

  • prepare-commit-msg

  • commit-msg

  • post-commit

  • post-checkout

  • pre-rebase

References

Last updated

Was this helpful?