Skip to content

Git 常用命令

Update local clone after remote branch name changes

sh
git branch -m <OLD-BRANCH> <NEW-BRANCH>
git fetch origin
git branch -u origin/<NEW-BRANCH> <NEW-BRANCH>
git remote set-head origin -a

One-line example (change master to main):

sh
git branch -m master main && git fetch origin && git branch -u origin/main main && git remote set-head origin -a

Separate subfolder in current Git repo to a new repo

  1. Create a new folder where would place the new repo
  2. git clone the original repo under this folder
  3. Download git-filter-repo from here: (Do not chagne its extension)
  4. cd the cloned repo root folder, and run following: python git-filter-repo --path <directory_to_separate_to_new_repo> --force
  5. Copy follwing files to root path of the new folder created in step 1:
    • Remaining files under <directory_to_separate_to_new_repo>
    • .git directory in original repo root folder
    • Run git status in the new folder root, to check the git status
    • Run git rev-list --count main to check the commits count of separated directory
  6. git add ., and commit the change as a re-organizing of file structure
  7. Create new repo in Github, and get its url, then run:
    sh
    git remote add origin https://github.com/<owner>/<new_repo>.git
    git remote -v
    git push -u origin main

See: Quickly rewrite git repository history (filter-branch replacement)