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
See: Renaming a branch - GitHub Docs
Separate subfolder in current Git repo to a new repo
- Create a new folder where would place the new repo
- git clone the original repo under this folder
- Download
git-filter-repo
from here: (Do not chagne its extension)- https://raw.githubusercontent.com/newren/git-filter-repo/main/git-filter-repo
- Place
git-filter-repo
to the cloned repo root folder
- cd the cloned repo root folder, and run following:
python git-filter-repo --path <directory_to_separate_to_new_repo> --force
- 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
- Remaining files under
git add .
, and commit the change as a re-organizing of file structure- 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: 将子文件夹拆分成新仓库 - GitHub 文档
See: Quickly rewrite git repository history (filter-branch replacement)