Git - Tips

01 - 临时保存和恢复当前改动

执行git stash保存后,git status将显示无任何改动。html

git stash  # Temporarily stores all modified tracked files

git stash list  # Lists all stashed changesets
git stash pop <stash> # Restores the stashed files, and delete the stashed files.
git stash apply <stash> # Restores the stashed files, and reserve the stashed files.

git stash drop  # Discards the most recently stashed changeset
git stash show  # Show the latest changes recorded in the stash as a diff between the stashed state and its original parent. 
git stash clear  # Remove all the stashed states.

02 - 推送失败

不一样的人修改同个文件的同一个地方,而后推送到远程库是会发生“推送失败”,由于推送有冲突。
解决方法:先用git pull抓取最新的提交,而后在本地合并,解决冲突,再推送。
使用git pull前,必须指定本地branch分支与远程origin/branch分支的连接(git branch --set-upstream-togit

03 - 多人协做

尝试用git push origin <branch name>推送修改。
若是推送失败,可能由于远程分支比本地更新早,使用git pull试图合并。
若是合并有冲突,则须要解决冲突,并在本地提交,再用git push origin <branch name>推送。windows

04 - 配置local repository

Local配置优先级高于global配置,并且Local的配置必须在local repository目录下完成。
示例:安全

$ git config --local user.name "anliven"  # 配置local repository的用户名
$ git config --local user.email "anliven@yeah.net"    # 配置local repository的邮箱

$ git config --local --list # 显示local repository配置信息
$ git config --local --unset [value-regex]  # 去除local repository配置

$ git config --local --edit  # 交互式local repository配置

05 - 配置文件

Git配置文件优先级:local > global > systembash

配置文件 有效范围 查看 配置方法 名称及目录
local 本地仓库 git config --local --list git config --local --edit 本地仓库目录下,例如:<local repository>\.git\config
global 全部仓库 git config --global --list git config --global --edit 用户目录下,例如:C:\Users\xxx.gitconfig
system 不建议改动 git config --system --list git config --system --edit git的安装目录下,例如:C:\Program Files\Git\mingw64\etc\gitconfig

06 - 合并多个commit

利用git rebase -i把其它commits标注为squash,从而将其它commits并入一个commit。
合并多个 Commit
合并 commit 保持分支干净整洁app

07 - 命令执行

  • 注意git命令的执行目录、生效目录和执行结果中的目录信息
  • 利用tab键补全目录、文件和命令名称
  • 使用完整的git命令,便于理解和确认

08 - 对比git pull和git pull --rebase

link
git pull = git fetch + git merge
git pull --rebase = git fetch + git rebasefetch

09 - 修改文件权限

# 在相应Repository目录中查看文件权限
git ls-tree HEAD

# 修改权限(权限修改后,至关于文件进入了index)
git update-index --chmod=+x <test.sh>

# 提交修改
git commit -m "script permission update"

# 确认修改结果
git ls-tree HEAD

10 - 报错:^M: bad interpreter

检查文件格式,必要时使用dos2unix命令转换文件格式。
在windows git下,建议关闭自动换行,并启用安全换行检查。ui

# 关闭自动换行的设置
git config --global core.autocrlf false

# 启用安全换行符检查
git config --global core.safecrlf true

11 - Git添加空文件夹

默认状况下,git将忽略空文件夹,也就是说空文件夹没法加入到repository。
解决办法:在空文件夹下建立包含!.gitignore内容的.gitignore文件便可。.net

相关文章
相关标签/搜索