essay | tech | year-summary | about

返回上级菜单

Git tips


日期:2024-04-30T00:00:00Z

Create a git mirror branch

https://stackoverflow.com/question/34100048

# after git v2.27
# this will create a total empty branch
git switch --orphan <new_branch_name>
git commit --allow-empty -m "init mirror commit"
git push -u <mirror_remote> <new_branch_name>
# this will only create a empty branch with existing files
git checkout --orphan <new_branch_name>
git commit --allow-empty -m "init mirror commit"
git push -u <mirror_remote> <new_branch_name>

Revert all files to a commit

https://stackoverflow.com/questions/65705294/git-rebase-while-maintaining-the-latest-version-of-a-file-in-one-branch

#! /bin/sh
git checkout <temp-tag> -- <path>
git diff-index --quiet HEAD || git commit
#! /bin/sh
git checkout <temp-tag> -- <path>
git diff-index --quiet HEAD || git commit --amend --no-edit

eg.

#! /bin/sh
git checkout abc22df -- .
git diff-index --quiet HEAD || git commit

git shadow

after clone with depth 1, if you want a full history, then use unshadow
https://stackoverflow.com/questions/6941889/is-it-safe-to-shallow-clone-with-depth-1-create-commits-and-pull-updates-aga

git clone --depth 1 xxxxxx.repo.git
git pull --unshallow

git namespace and git delete remote branch

https://stackoverflow.com/questions/4703200/how-to-delete-remotes-origin-branch
https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-locally-and-remotely

git delete local branch

git branch -d <branch_name>
git branch -D <branch_name>

git delete fetched local remote branch

git branch -r -d origin/MT-2766

git delete remote branch

git push <remote_name> --delete <branch_name>
git push <remote_name> :<branch_name>

git namespace

.git/config

[remote "origin"]
        url = [email protected]:JingZheCyberSpace/BlaBla.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[remote "candy"]
        url = [email protected]:CandyWater/BlaBla.git
        fetch = +refs/heads/*:refs/remotes/candywater/*

cp without .git

https://stackoverflow.com/questions/4585929/how-to-use-cp-command-to-exclude-a-specific-directory

rsync -r --verbose --exclude 'exclude_pattern' ./* /to/where/
rsync -r --verbose --exclude ".git" ./aaaa ./bbb   

git submodule revert changes

https://stackoverflow.com/questions/10906554/how-do-i-revert-my-changes-to-a-git-submodule

git submodule deinit -f .
git submodule update --init

git submodule delete

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

git rm <submodule_path>