我的git日常使用笔记

来源:互联网 发布:select2变更数据 编辑:程序博客网 时间:2024/04/30 11:05
1.常用命令
git clean -df   删除未跟踪文件,但不包括.gitignore中的
git clean -n   测试删除的文件,实际不删除
git clean -dfx 删除所有文件
git branch -m master part2 # 重命名“master”分支為“part2”
git log --oneline path/dir 显示某个目录的历史,日志只显示一行

2.在git bash中输入中文
修改文件C:\Program Files\Git\etc\inputrc:
set output-meta on
set convert-meta off
说明:使得在 Git Bash 中可以正常输入中文,比如中文的 commit log。

3.ls时能正常显示中文
在Git\etc\git-completion.bash,加入:
alias ls='ls --show-control-chars --color=auto'

4.定制log格式
在文件~/.gitconfig中添加如下:
[alias]
lg = log --all --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %C(red)<%cn>%Creset %Cgreen(%cr)%Creset' --abbrev-commit --date=relative

效果:
E:\eclipse\test>git lg

* a1a0b7c - (master) master commit (11 minutes ago)
| * c7d2876 - (HEAD, mybranch) Revert "mybranch commit2" (45 minutes ago)
| * 647d614 - mybranch commit2 (46 minutes ago)
| * dd734ea - mybranch 修改1 (60 minutes ago)
|/
* b19b972 - master修改1 (61 minutes ago)
* 82eb3a6 - sas (31 hours ago)
* 266d613 - GBK (31 hours ago)

E:\eclipse\test>

5.配置Notepad2为编辑器,注释说明要设置编码格式为utf-8,不然会乱码
在文件~/.gitconfig中添加如下: 
[core]
editor = \"D:/Program Files/Notepad2/Notepad2.exe\"
6.配置比较工具
在文件~/.gitconfig中添加如下: 
[diff]   
    tool = BCompare   
[difftool "BCompare"]   
    cmd = \"D:/Program Files/Beyond Compare 3/BCompare.exe\" \"$LOCAL\" \"$REMOTE\"  
[difftool]   
    prompt = false  
[merge]  
    tool = BCompare  
[mergetool "BCompare"]  
    cmd = \"D:/Program Files/Beyond Compare 3/BCompare.exe\" \"$LOCAL\" \"$REMOTE\" \"$BASE\" \"$MERGED\"  
    trustExitCode = true  
    
7.配置diff
在文件~/.gitconfig中添加如下: 
[diff]
external = \"D:/Program Files/Beyond Compare 3/BCompare.exe\" 

8.git访问本地svn
git svn clone svn://localhost/respo   
(在创建SVN版本库的时候要注意配置好相应该文件,我在配置zuthz文件的时候就是没弄好,结果导致git无法访问svn,以为是git的bug.)

9.将本地git库导入svn(不丢失日志)
进入本地git库目录
git svn init svn://localhost/respo/dir
git svn fetch
git rebase git-svn
git svn dcommit

10.修改svn地址(第二步必须fetch至少一个版本)
1.Edit the svn-remote url URL in .git/config to point to the new domain name
2.Run git svn fetch – This needs to fetch at least one new revision from svn!
3.Change svn-remote url back to the original url
4.Run git svn rebase -l to do a local rebase (with the changes that came in with the last fetch operation)
5.Change svn-remote url back to the new url
6.Run git svn rebase should now work again!

11.找回丢失的commit
git rebase git-svn后,发现有冲突,修改后commit,才发现有个.c文件编辑冲突错了,所有修改都没有了,晕倒。。。
还好有后悔药(感叹git的强大啊,神马意外情况都考虑到了)满血恢复
git reflog
502dd0f HEAD@{0}: HEAD~1: updating HEAD
147b3b5 HEAD@{1}: commit: test3
502dd0f HEAD@{2}: commit: test2
0692c03 HEAD@{3}: commit (initial): test1


git reset --hard 502dd0f
git cherry-pick 147b3b5

如果使用tortoiseGit的话,在版本目录下shit+右键,可以看到tortoisegit 菜单多了show reflog菜单,进去可以查看到丢失的commit,之后就任你处置了....

git status 中文乱码
只要一行就行了
git config --global core.quotepath false

TortoiseGit产生sys$command文件
C:\msysgit\msysgit\lib\perl5\5.8.8\Term\ReadLine.pm
214行
-    } elsif (-e "con" or $^O eq 'MSWin32') {
+    } elsif (-e "con" or $^O eq 'MSWin32' or $^O eq 'msys') {

Git for Windows 终端字体警告
从 GitHub 下载的 Git for Windows 客户端,使用 Shell 过程中如果路径含有中文会报告一个警告。

Warning: Your console font probably doesn't support Unicode. If you experience strange characters in the output, consider switching to a TrueType font such as Lucida Console!           
                                                                                                  
Google 了半天不得要领。现将解决方法记录下来。其实很简单,将 CMD 的默认字体换位宋体即可
原创粉丝点击