Windows下,git提供了一个Linux风格的命令行,叫git-bash
用例:解决乱码问题,Windows路径无效问题,并修改字体大小
解决方法:在git-bash右键->Options->Text,修改Local为zh_CN,Character set为UTF-8,还可以Select,修改字体大小
$ git config --list
core.symlinks=false
core.autocrlf=true
core.fscache=true
color.interactive=true
color.ui=auto
help.format=html
diff.astextplain.textconv=astextplain
rebase.autosquash=true
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
credential.helper=!"D:/Software/PortableGit/mingw64/libexec/git-core/git-credential-store.exe"
user.email=[email protected]
user.name=DEDSEC_Roger
credential.helperselector.selected=store
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
remote.origin.url=https://<token>@github.com/DEDSEC-Roger/Speaker_Recognition.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.main.remote=origin
branch.main.merge=refs/heads/main
$ git config --global user.name DEDSEC_Roger
$ git config --global user.email [email protected]
$ git config --global --list
user.email=[email protected]
user.name=DEDSEC_Roger
# credential.helperselector是我之前设置的
credential.helperselector.selected=store
git config --global core.editor "nano"
git config --global --add safe.directory <your_project_directory>
# -R表示递归所有子文件夹,-v表示显示修改的过程
sudo chown <username> -Rv <directory>
用例:从GitHub,clone某个仓库的某个分支(branch)到当前文件夹
在本地新建一个文件夹,然后运行
$ git clone https://github.com/DEDSEC-Roger/Speaker_Recognition.git
Cloning into 'Speaker_Recognition'...
remote: Enumerating objects: 271, done.
remote: Counting objects: 100% (11/11), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 271 (delta 5), reused 3 (delta 1), pack-reused 260
Receiving objects: 100% (271/271), 32.10 MiB | 1.09 MiB/s, done.
Resolving deltas: 100% (50/50), done.
成功下载该仓库该分支的代码到本地了,是放在一个文件夹里面的,这个文件夹里除了.git文件夹,其他都称为工作区(working directory)
注意:clone包含.git文件夹,如果我们已经做了很多修改,那么.git文件夹会非常大,因为保存了以前的commit,可以采用–depth=1来限制只提取最近一次commit,并采用–branch来指定分支
$ git clone --depth=1 --branch=main https://github.com/DEDSEC-Roger/Speaker_Recognition.git
正克隆到 'Speaker_Recognition'...
remote: Enumerating objects: 77, done.
remote: Counting objects: 100% (77/77), done.
remote: Compressing objects: 100% (75/75), done.
remote: Total 77 (delta 0), reused 65 (delta 0), pack-reused 0
展开对象中: 100% (77/77), 完成.
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: Test.py
no changes added to commit (use "git add" and/or "git commit -a")
# 当前目录下所有文件
git add .
# 当前目录下单个文件
git add filename
# 当前目录下多个文件
git add filename1 filename2 filename3
# 当前目录下所有.py文件
# 一个*表示匹配任意数量字符
# 一个?表示匹配任一(包括无)字符
# .符号也会被匹配
git add *.py
# 当前目录下所有.pyc, .pyo, .pyd文件
# 一个[]表示匹配括号内的任一字符,也可以在括号内加连接符,如[0-9]匹配0至9的数
git add *.py[cod]
# 当前目录下除.py文件外的所有文件
# 一个!在前表示反转规则
git add !*.py
# 整个文件夹,必须是非空文件夹
git add folder
# folder文件夹下,以及子文件夹下的所有文件
git add folder/*
# folder文件夹下,以及子文件夹下的所有.py文件
# 两个*表示匹配任意子文件夹
git add folder/**/*.py
$ git add Profile/*Delete/*.txt
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged ..." to unstage)
new file: Profile/ECAPA_TDNN_GLOB_c512-ASTP-emb192-ArcMargin-LM_Delete/dummy.txt
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: Test.py
$ git commit -m "add files"
[main 487222b] add files
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 Profile/ECAPA_TDNN_GLOB_c512-ASTP-emb192-ArcMargin-LM_Delete/dummy.txt
$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: Test.py
no changes added to commit (use "git add" and/or "git commit -a")
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: Test.py
no changes added to commit (use "git add" and/or "git commit -a")
$ git restore .\Test.py
$ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
$ git add .\Test.py
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged ..." to unstage)
modified: Test.py
$ git restore --staged .\Test.py
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: Test.py
no changes added to commit (use "git add" and/or "git commit -a")
git add .\Test.py
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged ..." to unstage)
modified: Test.py
$ git rm --cached .\Test.py
rm 'Test.py'
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged ..." to unstage)
deleted: Test.py
Untracked files:
(use "git add ..." to include in what will be committed)
Test.py
$ git add .\Test.py
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged ..." to unstage)
modified: Test.py
# --soft表示退回本地分支的commit到缓存区
# HEAD后面跟几个~就是最近的几次commit
# HEAD后面也可直接跟~数字,比如HEAD~~等价于HEAD~2
$ git reset --soft HEAD~
# --mixed表示退回本地分支的commit到缓存区,再把缓存区的添加全部去除
$ git reset --mixed HEAD~
# 慎用!--hard表示退回本地分支的commit到缓存区,再把缓存区的添加去掉
# 再把工作区的修改也恢复,工作区的恢复是全部恢复
$ git reset --hard HEAD~
--hard
能够恢复本地工作区,所以reset可以用来从commit恢复文件# 首先,获取需要恢复的commit的id
$ git log
commit 4468947dc8de8bc88480a21c1572576b26c87e59 (HEAD -> master, origin/master, origin/HEAD)
Author: DEDSEC_Roger <[email protected]>
Date: Tue Feb 28 16:46:42 2023 +0800
scores/*, config.yaml, train.log
commit 6587f7c2f72c64096b93f3b8928a71f384afeef1
Author: DEDSEC_Roger <[email protected]>
Date: Tue Feb 28 16:41:51 2023 +0800
ecapa_tdnn, ecapa_tdnn_lm
commit 8eff57d84861daf8bafe84212664dddaa2e600fe
Author: DEDSEC_Roger <[email protected]>
Date: Tue Feb 28 16:39:22 2023 +0800
extract_vox, prepare_data, score, score_norm
# 根据commit的comment来确定需要恢复的commit
# 比如第二个commit的id为6587f7c2f72c64096b93f3b8928a71f384afeef1
# 然后恢复
$ git reset --hard 6587f7c2f72c64096b93f3b8928a71f384afeef1
Your branch and 'origin/main' have diverged, and have 1 and 2 different commits each, respectively. (use "git pull" to merge the remote branch into yours)
```bash
$ git log
commit b05c085c53f7f5e3beaa1054445c809ffedaaed0 (HEAD -> master)
Author: DEDSEC_Roger
Date: Tue Feb 28 19:28:47 2023 +0800
modified test.txt
commit 5e440cbe2aed805fc19b0e995bc4c5f2622163ad
Author: DEDSEC_Roger
Date: Tue Feb 28 19:07:11 2023 +0800
create test.txt
$ git revert 5e440cbe2aed805fc19b0e995bc4c5f2622163ad
CONFLICT (modify/delete): test.txt deleted in parent of 5e440cb...
create test.txt and modified in HEAD. Version HEAD of test.txt left in tree.
error: could not revert 5e440cb... create test.txt
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add ' or 'git rm '
hint: and commit the result with 'git commit'
# commit还在
$ git log
commit b05c085c53f7f5e3beaa1054445c809ffedaaed0 (HEAD -> master)
Author: DEDSEC_Roger
Date: Tue Feb 28 19:28:47 2023 +0800
modified test.txt
commit 5e440cbe2aed805fc19b0e995bc4c5f2622163ad
Author: DEDSEC_Roger
Date: Tue Feb 28 19:07:11 2023 +0800
create test.txt
# 文件未被修改
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
You are currently reverting commit 5e440cb.
(fix conflicts and run "git revert --continue")
(use "git revert --skip" to skip this patch)
(use "git revert --abort" to cancel the revert operation)
Unmerged paths:
(use "git restore --staged ..." to unstage)
(use "git add/rm ..." as appropriate to mark resolution)
deleted by them: test.txt
# 修改好文件后,再次add并commit
$ git add .
$ git commit -m "finish revert"
$ git log
commit 817c9918ca5272b31cf5caa95d7de4eeefbf779d (HEAD -> master)
Author: DEDSEC_Roger
Date: Tue Feb 28 19:45:29 2023 +0800
finish revert
commit b05c085c53f7f5e3beaa1054445c809ffedaaed0
Author: DEDSEC_Roger
Date: Tue Feb 28 19:28:47 2023 +0800
modified test.txt
commit 5e440cbe2aed805fc19b0e995bc4c5f2622163ad
Author: DEDSEC_Roger
Date: Tue Feb 28 19:07:11 2023 +0800
create test.txt
```
# 修改错了test1.txt,提交了commit
$ git log
commit 27012e98329790632fdaa40a277347caa7099cda (HEAD -> master)
Author: DEDSEC_Roger <[email protected]>
Date: Tue Feb 28 19:56:16 2023 +0800
modify test1
commit 817c9918ca5272b31cf5caa95d7de4eeefbf779d
Author: DEDSEC_Roger <[email protected]>
Date: Tue Feb 28 19:45:29 2023 +0800
finish revert
# 从“finish revert”恢复,但又不删除“modify test1”
$ git checkout 817c9918ca5272b31cf5caa95d7de4eeefbf779d -- test1.txt
# test1.txt已恢复,“modify test1”这个commit还在
$ git log
commit 27012e98329790632fdaa40a277347caa7099cda (HEAD -> master)
Author: DEDSEC_Roger <[email protected]>
Date: Tue Feb 28 19:56:16 2023 +0800
modify test1
commit 817c9918ca5272b31cf5caa95d7de4eeefbf779d
Author: DEDSEC_Roger <[email protected]>
Date: Tue Feb 28 19:45:29 2023 +0800
finish revert
# 指定仓库名origin,查看该仓库的信息
# 直接git remote,查看连接了哪些仓库
$ git remote show origin
* remote origin
Fetch URL: https://github.com/DEDSEC-Roger/Speaker_Recognition.git
Push URL: https://github.com/DEDSEC-Roger/Speaker_Recognition.git
HEAD branch: main
Remote branch:
main tracked
Local branch configured for 'git pull':
main merges with remote main
Local ref configured for 'git push':
main pushes to main (local out of date)
$ git remote set-url origin https://<token>@github.com/DEDSEC-Roger/Speaker_Recognition.git
$ git remote show origin
* remote origin
Fetch URL: https://<token>@github.com/DEDSEC-Roger/Speaker_Recognition.git
Push URL: https://<token>@github.com/DEDSEC-Roger/Speaker_Recognition.git
HEAD branch: main
Remote branch:
main tracked
Local branch configured for 'git pull':
main merges with remote main
Local ref configured for 'git push':
main pushes to main (local out of date)
up to date
# pull的对象是某个仓库的某个分支,加上仓库名origin和分支名main更严谨一些
$ git pull origin main
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), 1.27 KiB | 81.00 KiB/s, done.
From https://github.com/DEDSEC-Roger/Speaker_Recognition
b41da3b..3149ba8 main -> origin/main
Updating b41da3b..3149ba8
Fast-forward
...CAPA_TDNN_GLOB_c512-ASTP-emb192-ArcMargin-LM.onnx | Bin 24861931 -> 0 bytes
Resource/origin.jpg | Bin 1852464 -> 0 bytes
2 files changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 Model/ECAPA_TDNN_GLOB_c512-ASTP-emb192-ArcMargin-LM.onnx
delete mode 100644 Resource/origin.jpg
# 此时再查看状态,本地分支的修改还在,而且新增的文件处于Untracked状态
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: Test.py
Untracked files:
(use "git add ..." to include in what will be committed)
Profile/ECAPA_TDNN_GLOB_c512-ASTP-emb192-ArcMargin-LM_Delete/
no changes added to commit (use "git add" and/or "git commit -a")
$ git remote show origin
* remote origin
Fetch URL: https://<token>@github.com/DEDSEC-Roger/Speaker_Recognition.git
Push URL: https://<token>@github.com/DEDSEC-Roger/Speaker_Recognition.git
HEAD branch: main
Remote branch:
main tracked
Local branch configured for 'git pull':
main merges with remote main
Local ref configured for 'git push':
main pushes to main (fast-forwardable)
# push的对象是某个仓库的某个分支,加上仓库名origin和分支名main更严谨一些
$ git push origin main
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 380 bytes | 380.00 KiB/s, done.
Total 4 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To https://github.com/DEDSEC-Roger/Speaker_Recognition.git
af33ce1..251eb2b main -> main
# Customization
Audio/**/*.wav
Model/**/*.onnx
Profile/**/*.npy
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
$ git rm -r --cached .
rm '.gitignore'
rm '.vscode/launch.json'
rm 'Audio.py'
rm 'Audio/hzf_certain.wav'
rm 'Audio/hzf_certain_2.wav'
...
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged ..." to unstage)
deleted: .gitignore
deleted: .vscode/launch.json
deleted: Audio.py
deleted: Audio/hzf_certain.wav
deleted: Audio/hzf_certain_2.wav
...
$ git add .
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged ..." to unstage)
modified: Audio.py
deleted: Audio/hzf_certain.wav
deleted: Audio/hzf_certain_2.wav
deleted: Audio/hzf_certain_3.wav
deleted: Audio/hzf_certain_4.wav
deleted: Audio/hzf_certain_5.wav
...
$ git commit -m "completely update .gitignore"
[main fa38978] completely update .gitignore
62 files changed, 1673 insertions(+), 1673 deletions(-)
delete mode 100644 Audio/hzf_certain.wav
delete mode 100644 Audio/hzf_certain_2.wav
delete mode 100644 Audio/hzf_certain_3.wav
delete mode 100644 Audio/hzf_certain_4.wav
delete mode 100644 Audio/hzf_certain_5.wav
...
$ git push origin main
# 正处于要进行重命名的本地分支master下
$ git branch -m main
# 不处于要进行重命名的本地分支master下
$ git branch -m master main
# 重命名后,关联远程仓库
$ git remote set-url origin https://<token>@github.com/DEDSEC-Roger/WeSpeaker.git
# 上传分支,此时在远程,存在名为master的分支
$ git push origin main
# 上传后,会存在master和main两个分支
# 正处于要进行重命名的本地分支main下
$ git branch -m master
# 不处于要进行重命名的本地分支master下
$ git branch -m main master
# 重命名后,删除远程分支
$ git push origin --delete main
# 删除远程分支后,最好再关联远程仓库一次
$ git remote set-url origin https://<token>@github.com/DEDSEC-Roger/WeSpeaker.git
# 上传分支
$ git push origin master
# 上传后,会存在master分支,关联远程分支
$ git branch --set-upstream-to=origin/master