Git使用笔记

命令

基本命令

# 配置用户名 & 密码
git config --global user.name johnnywong233
git config --global user.mail 1224017485@qq.com
# 生成密钥文件
ssh-keygen -t rsa -b 4096 -C 1224017485@qq.com

高级命令

  • 克隆一个特定的远程分支(Clone a specific remote branch),不想克隆整个资源库分支
git init    
git remote add -t BRANCH_NAME_HERE -f origin REMOTE_REPO_URL_PATH_HERE    
git checkout BRANCH_NAME_HERE
  • 从其他分支签出文件但无需切换分支( Checkout File from Other Branch without Switching Branches )
    git checkout BRANCH -- PATH_TO_FILE
  • 树形输出
    git log --graph --pretty=format:'%C(yellow)%h%Creset -%C(cyan)%d%Creset %s %Cgreen(%an, %cr)' --abbrev-commit
  • 别名配置
    git config --global alias.ll "log --graph --pretty=format:'%C(yellow)%h%Creset -%C(cyan)%d%Creset %s %Cgreen(%an, %cr)' --abbrev-commit"
  • git 颜色输出配置
git config --global color.status auto
git config --global color.diff auto
git config --global color.branch auto
git config --global color.interactive auto

技巧

查找并打印出 git 项目中某个 committer 所有提交过(新增/更改/删除)的文件

git log --pretty="%H" --author="awesome_me" |
    while read commit_hash
    do
        git show --oneline --name-only $commit_hash | tail -n+2
    done | sort | uniq

统计git项目的代码行数

git log --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s removed lines: %s total lines: %s\n", add, subs, loc }'

常见问题

git 默认不区分文件名大小写

团队开发中发现他人提交的不规范文件名 readme.md,强迫症的我在本地修改文件名为 README.md,然后提交,发现代码没有变化。同时git status无任何提示信息。
这是因为,git 默认对于文件名大小写是不敏感,所以修改文件名大小写,git 检查不到任何改动。那么如何才能让 git 识别文件名大小写变化?

  1. 配置git 使其对文件名大小写敏感
    git config core.ignorecase false
  2. 先从 git 本地仓库删除,再提交
# 删除
git rm readme.md
# 重新添加
git add Readme.md
# 提交
git commit -m 'Readme.md'

推荐第一种方法,配置好git 对文件名大小写敏感。
【注】
2018年10月24日 19:13:23 补充
Git 版本2.19.1 win版本没有这个问题,即git config core.ignorecase=true时,也可以提交到远程 GitHub 服务器。

# 查看 Git 版本
git --version
git version 2.19.1.windows.1

Git忽略文件

git提供多种忽略文件的方法。

1. .gitignore 文件

在项目的根目录下的.gitignore为全局的文件过滤设置,即会影响本地和远程。.gitignore的配置可以使用表达式进行配置:

#忽略maven编译的target文件夹
target/
#忽略eclipse的工程文件夹settings
.settings/

注意:当文件已经被纳入版本控制中,如果此时想再忽略改文件的改动,通过配置.gitignore是无效的,需使用第三个方式来处理。

2. exclude 文件

exclude文件位于{项目路径}/.git/info/exclude。该文件主要用于忽略不想加入版本控制的文件而又不影响远端。只影响本机的文件忽略配置。具体配置方法和.gitignore一样。

3. git update-index --assume-unchanged

上面的两个方式,可以灵活的处理忽略文件的规则,第一个用于全局,第二个用于本机。但是它们都不能处理已经加入版本控制的文件。设想在项目中有一个公用数据库配置文件,而每个用户开发时需要将其中的数据库信息修改为本机的配置。如果直接修改,势必会被git识别为修改,而会将其更新。这样一来就会影响其他用户的开发。那么这里可以使用git update-index --assume-unchanged进行文件改动的忽略。当然任何时候又需要对通过该命令忽略掉的文件进行更新,可以使用git update-index --no-assume-unchanged来取消该文件的改动的忽略。 这个方式只适用于已经被加入版本控制(跟踪)的文件的改动忽略。

git提交大文件到远程服务器设置

git push时,如果文件过大,则会返回如下提示:
packet_write_wait: Connection to **.**.**.**: Broken pipe KiB/s
fatal:The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
参考:http://stackoverflow.com/questions/6842687/the-remote-end-hung-up-unexpectedly-while-git-cloning
解决方法:

  • 1.在打开的命令行窗口执行:
    git config http.postBuffer 524288000
  • 2.vim .git/config,添加如下配置,从而省去每次新开一个window都需要配置如上git config:
    [http] postBuffer = 524288000/1048576000
  • 3.全局设置,针对所有代码repository生效(有待验证)
    git global config http.postBuffer 524288000

filename too long

checkout文件时因为文件名太长而失败:filename too long
解决方法:git config --global core.longpaths true

GitHub地址迁移

因为公司的调整,且代码托管在GitHub上面,现GitHub地址迁移。
故而会出现执行git push命令失败,并且git pull也不行。报错信息:
ERROR: Repository ITOM-Shared-Services/suite-installer' is disabled. Please ask the owner to check their account. fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. 解决方法: 进入到.ssh目录下面 cd Users<user>.ssh把id_rsa.pub其中的文本配置到新的Github地址的SSH key里面。 同时修改之前git clone的代码下面的隐藏文件夹.git下面的config文件,修改字段为url = git@<new_github_url>:aaa/hello-world.git`

the RSA host key for “*” differ from the key for the IP address “” offending key for IP in known_hosts

这里写图片描述
解决方法:
If you encounter issue when pull from github.aaa.com; you need to delete the row which contains “github.aaa.com” in known_hosts file (C:\Users\<username>\.ssh or /home/<username>/.sshon Linux) then pull again.

SourceTree安装跳过Atlassian账号,免登陆,跳过初始设置

在开发过程中,遇到分支过多(流程很规范化,有master,dev,release,feature,hotfix等分支,还有tag,实际上个人任务tag就是一个不再提交代码的branch)的情况下,会感觉脑子不够用,命令行的打印输出log看得云里雾里,此时可以使用Git GUI来清晰地看出branch的分离,merge图谱;说到GUI工具,最专业的莫过于source tree。
source tree 安装遇到的一个问题就是需要注册账户,要么是使用Google账户要么是Atlassian账号,无论是哪一种,都很麻烦,要么能够FQ,要么不怕麻烦去注册一个账号。
这里写图片描述
这里就给出方法来省去这一步。安装时,看到上面这个界面,退出安装流程;转到用户本地文件夹下的 SourceTree 目录(C:\Users\<user>\AppData\Local\Atlassian\SourceTree)(mac用户类似的处理方式),找到accounts.json 文件,没有则新建该文件,内容如下:

[
  {
    "$id": "1",
    "$type": "SourceTree.Api.Host.Identity.Model.IdentityAccount, SourceTree.Api.Host.Identity",
    "Authenticate": true,
    "HostInstance": {
      "$id": "2",
      "$type": "SourceTree.Host.Atlassianaccount.AtlassianAccountInstance, SourceTree.Host.AtlassianAccount",
      "Host": {
        "$id": "3",
        "$type": "SourceTree.Host.Atlassianaccount.AtlassianAccountHost, SourceTree.Host.AtlassianAccount",
        "Id": "atlassian account"
      },
      "BaseUrl": "https://id.atlassian.com/"
    },
    "Credentials": {
      "$id": "4",
      "$type": "SourceTree.Model.BasicAuthCredentials, SourceTree.Api.Account",
      "Username": "",
      "Email": null
    },
    "IsDefault": false
  }
]

然后重新安装,即提示安装程序没有搜索找到Mercurial,选择使用其他版本控制工具,下一步。

git clone报"ssh: connect to host github.com port 22: Connection timed out"错误

如题,在公司的电脑上面执行操作git clone git@github.com:johnnywong233/test.git报错:

connect to host github.com port 22: Connection timed out
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

解决方法:
在存放公钥私钥(id_rsaid_rsa.pub)的文件夹下面,也就是.ssh隐藏文件夹下面,新建config文本,内容如下:

Host github.com
HostName ssh.github.com
User 1224017485@qq.com
IdentityFile ~/.ssh/id_rsa_github
PreferredAuthentications publickey
Port 443

其中id_rsa_github文件需要使用命令来生成:
ssh-keygen -t rsa -b 4096 -C 1224017485@qq.com
把生成的文件id_rsa_github.pub的内容copy添加到GitHub的SSH and GPG key页面。
问题很简单,解决方案Google一下是更简单的事情。
但是,为什么一开始拿到公司的新电脑时,并没有这样配置。直接把使用公司邮箱生成的id_rsa.pub文件内容copy添加到GitHub,没有报错,可以成功git clone,而后来不行,需要做这样的配置。为什么??

多个git账号之间的切换

.ssh隐藏文件夹下面,新建config文本,内容如下:

# Default gitlab user at work(****@**.com)
Host git.dev.sh.***.com
HostName git.dev.sh.***.com
User ****@**.com
IdentityFile ~/.ssh/id_rsa

# second user(1224017485@qq.com)
Host github.com
HostName ssh.github.com
User 1224017485@qq.com
IdentityFile ~/.ssh/id_rsa_github
PreferredAuthentications publickey
Port 443

Git Stash

# 保存本地的修改,切换到其他 branch 继续其他支线开发任务
git stash
# 查看现有的储藏,打印类似 stash@{0} 这样的内容
git stash list
# 应用储藏,即恢复工作内容
git stash apply # 等价于git stash apply  stash@{0}
git stash apply stash@{2}
# 移除stash
git stash drop stash@{0}
# 重新应用储藏,同时立刻将其从堆栈中移走
 git stash pop

gitlab上创建的分支用命令查不到

在GitLab server端,即浏览器登录,看到确实有新创建的分支。但是无论是用git bash命令行还是IDEA(实际上还是git bash命令行),git branch -a看不到新创建的分支,git checkout -b <local_branch> <remote_branch>都操作失败,提示分支不存在。
解决方法:同步版本库命令:git fetch

.keep文件

看到开源项目中有个.keep文件。查询资料得知:
首先,git是不允许提交一个空目录到版本库上的。
那么问题来了,在项目初期,作为项目代码库的owner,一般会希望设立项目工程的package包名。一个组织良好的框架,一定有着命名准确的包名。
解决方法:在空的文件夹里面建立一个.keep文件,然后提交去即可。
拓展:其实在git中 .gitkeep 就是一个占位符。其他文件如 .nofile等也可以作为占位符。

.gitmodules文件

看到开源项目里面有一个 .gitmodules文件。

[submodule "dep/gsl"]
	path = dep/gsl
	url = https://github.com/Microsoft/gsl

用途:子模块允许你将一个 Git 仓库当作另外一个Git仓库的子目录。这允许你克隆另外一个仓库到你的项目中并且保持你的提交相对独立。
通过git submodule add <url> <customized_name>将外部项目加为子模块,此命令生成一个隐藏文件 .gitmodules,

160000 模式。这在Git中是一个特殊模式,基本意思是你将一个提交记录为一个目录项而不是子目录或者文件。
所有的Git命令都在两个子目录里独立工作。

git checkout File exists

执行命令:git checkout develop
报错如下:

fatal: Unable to create 'E:/code/ppd/cbdbi/cloud-iview/iView-ui-v4/.git/index.lock': File exists.
Another git process seems to be running in this repository, e.g.an editor opened by 'git commit'. Please make sure all processes are terminated then try again. If it still fails, a git process may have crashed in this repository earlier: remove the file manually to continue.

只是记录遇到的问题,解决方法就是重试。

Pro Git 工具 - 子模块

posted @   johnny233  阅读(20)  评论(0编辑  收藏  举报  
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示