0%

Git Cheatsheet

Good git resources

Git Move files from one repository to another while preserving git history

  1. mkdir merge_repo && cd merge_repo
  2. git clone --branch master --origin origin --progress -v git@github:xxx/projectA.git
  3. cd projectA
  4. git remote rm origin
  5. git filter-branch --subdirectory-filter subfolder/FOLDER_TO_KEEP -- --all
  6. git reset --hard && git gc --aggressive && git prune && git clean -fd
  7. (optional) mkdir anotherfolder && mv * anotherfolder && git add . && git commit
  8. cd ..
  9. git clone <git repository B url> projectB
  10. cd projectB
  11. git remote add repoA ../projectA
  12. git pull repoA master --allow-unrelated-histories
  13. git remote rm repoA
  14. git push

Git remote

Add new branch

  • git checkout -b branch
  • git switch -c branch

Add upstream remote

  • git push -u origin master
  • git branch --set-upstream-to origin/branch-name
  • git push --set-upstream origin master
  • git push origin --delete test

Git basic

  • git clone --single-branch --branch
  • git branch -a
  • Delete remote branch: git push origin --delete [branchname]
  • Delete local merged branch: git branch -d [branchname]
  • Delete local unmerged branch: git branch -D [branchName]
  • git log --graph --pretty=oneline --abbrev-commit
  • git log --graph --decorate --oneline --simplify-by-decoration --all
  • git log -L (where L is a number, for example git log -5)
  • If can not add a folder by git
    • git rm --cached folder
    • git add folder
  • Apply changes to another branch
    1. git stash
    2. git checkout anotherbranch
    3. git stash apply

Git advance

  • git reset
  • git rebase -i [previous commit]
  • change a commit: git commit --amend
  • git checkout -b test /test;
  • git checkout -t /test
  • git cherry-pick
  • git stash push path/to/file git stash push path/to/file

Git config

  • git config --global user.name “xx”
  • git config --global user.email “xx@xx.com
  • git config --global core.editor “vim”
  • Chinese mirror: github.com.cnpmjs.org
  • npm install -g commitizen cz-conventional-changelog
    echo ‘{ “path”: “cz-conventional-changelog” }’ > ~/.czrc
  • git config --global core.autocrlf true
  • sudo chown -R “${USER:-$(id -un)}” .