Git Advanced Tips
git clean
git clean
命令用来从你的工作区中删除所有没有 tracked过的文件. git clean
经常和git reset --hard
一起结合使用. 记住 reset 只影响被 track 过的文件, 所以需要 clean 来删除没有 track 过的文件. 结合使用这两个命令能让你的工作区完全回到一个指定的<commit>
的状态.
QA
- 什么叫没有 tracked?
不考虑.gitignore
的话:你创建了些文件 或者 IDE 自动创建了些文件,但是你从来没有 add 它们,也没有 commit 它们,它们就是没有 untracked。
- 与
git checkout .
的区别?
二者都是针对在工作区没有git add
的情况,但git checkout .
是对修改文件而言,git clean
是对新文件而言。
git clean 参数
-n 显示 将要 删除的 文件 和 目录(只是一个演习和提醒)
-f 删除 文件
-df 删除 文件 和 目录 zsh缩写:gclean
git clean -n
是一次 clean 的演习, 告诉你哪些文件会被删除. 记住他不会真正的删除文件, 只是一个提醒.
git clean -f
删除当前目录下所有没有 track 过的文件. 他不会删除 .gitignore
文件里面指定的文件夹和文件, 不管这些文件有没有被 track 过.
git clean -f <path>
删除指定路径下的没有被 track 过的文件.
git clean -df # gclean
删除当前目录下没有被 track 过的文件和文件夹.
git clean -xf
删除当前目录下所有没有 track 过的文件. 不管他是否是.gitignore 文件里面指定的文件夹和文件
git clean -fdx
这将删除所有本地未跟踪的文件/夹,所以只有 git 跟踪的文件保留:警告: -x 也将删除所有被忽略的文件!
git reset --hard
和 git clean -f
是一对好基友. 结合使用他们能让你的工作目录完全回退到最近一次 commit 的时候。
git rm
有时候在项目开发过程中,把某些目录或文件加入.gitignore
后发现并未生效,原因是.gitignore
只能忽略那些原来没有被 track 的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore
是无效的。那么解决方法就是先把本地缓存删除(改变成未 track 状态),然后再提交:
git rm -r --cached .
git add .
git commit -m 'update .gitignore
.gitignore
的匹配规则:
.a # 忽略所有 .a 结尾的文件
!lib.a # 但 lib.a 除外
/TODO # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
build/ # 忽略 build/ 目录下的所有文件
doc/.txt # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt
Case Sensitivity
To disable case sensitivity in Git, preventing it from treating file names with different cases as different files, you can set core.ignorecase
to false
in your Git configuration. This can be done with the following command:
git config --global core.ignorecase false
This command sets Git to ignore case sensitivity globally. If you want to apply this setting only to a specific repository, you can run the same command without the --global
option within that repository.
After that, you need to remove git cache and re-commit your relevant code as below:
git rm -r --cached .
git add .
git commit -m 'update .gitignore
git proxy
To set a proxy in Git, you can configure it using the Git configuration commands. You can set the proxy for HTTP and HTTPS protocols. Here are the steps to do so:
Check Current Proxy Settings
To check your current proxy settings, use:
git config --global --get http.proxy
git config --global --get https.proxy
Set Proxy
git config --global http.proxy http://proxyuser:proxypassword@proxy.server.com:port
git config --global https.proxy https://proxyuser:proxypassword@proxy.server.com:port
Removing Proxy Settings
If you need to remove the proxy settings, you can unset them:
git config --global --unset http.proxy
git config --global --unset https.proxy
If you need to set proxy settings for a specific repository instead of globally, remove the --global
option from the commands.