Skip to content

git

Remove from git but keep local

# Added but not commited
git reset

# Commited
git rm --cached {someFile}
git rm --cached -r {someDir}

Edit a commit

  • autosquash
git commit -m "fixup! <msg/hash>"
git rebase <hash>~N -i --autosquash

Reset

  • Hard
    • 直接回到該 commit
git reset --hard <HEAD/hash>~N # 回到前 N 個
  • Soft
git reset --soft <hash>

Quick fix the last commit

git commit --amend --no-edit

submodule

  • Add submodule
git submodule add
# e.g.
git submodule add git://github.com/majutsushi/tagbar.git .vim/bundle/tagbar
  • Update submodule
# `git pull` in each git repo
# or pull all submodules
git submodule foreach --recursive git pull origin master
  • Delete a submodule
git rm --cached /path/to/files
rm -rf /path/to/files
  • Delete the corresponding lines in .gitmodules
[submodule "/path/to/files"]
    path = /path/to/files
    url = xxx
  • Clone the repo with submodules
git clone --recursive <url to the repo>
  • Clone the reop first and then clone the submodules
git submodule update --init --recursive

pull

  • rebase
    1
    2
    git pull --rebase
    git rebase --continue
    

cherry-pick

Pick a specific commit to HEAD

git cherry-pick <SHA1> <SHA2> ...
  • Conflict?

    • git cherry-pick --continue
  • Edit commit message? Use -e

    • git cherry-pick <sha> -e

Revert

已經被推上去的 commit 發現錯誤,可以用 revert 來修復

git revert <Hash>

reflog - 移動紀錄

  • HEAD log

    git reflog
    

  • branch log

    git reflog <branch>
    

Search commit

git log --grep "<pattern>"

github two account

ssh-keygen -t rsa -C "your-email-address"
# Remember to enter filename id_rsaXXXX
  • Add to ~/.ssh/config

    Host github-COMPANY
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa_COMPANY
    

  • Remember to add your ssh key to github & use different user.name & user.email in repos

Other

  • Clean
git config --local credential.helper ""
git push origin master
  • Set default pull rebase
git config --global pull.rebase true
  • Remove submodules
    • Delete lines in .gitmodules and git add .gitmodules
    • Execute rm ./.git/config
    • git rm --cached path_to_submodule
    • rm -rf .git/modules/path_to_submodule
    • Commit
# Remove the submodule entry from .git/config
git submodule deinit -f path/to/submodule

# Remove the submodule directory from the superproject's .git/modules directory
rm -rf .git/modules/path/to/submodule

# Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule
git rm -f path/to/submodule
  • Merge two different repos
# clone a & b
cd a
git filter-repo --to-subdirectory-filter a
cd b
git remote add a ../a
git fetch a
git merge --allow-unrelated-histories a/master
git remote remove a
  • Checkout to tag
git tag -l
git checkout <tag>

References

Git Submodule 用法筆記 https://blog.chh.tw/posts/git-submodule/

https://stackoverflow.com/questions/1260748/how-do-i-remove-a-submodule/36593218#36593218