Skip to content

git 常用API #55

@coconilu

Description

@coconilu

概述

提供一份鸟瞰图:
git api

1. 初次配置

1. 账户信息

git config --global user.name ""
git config --global user.email ""

2. 凭证存储

git config  credential.helper store //(当前仓库)
git config --global credential.helper store //(全局仓库,与上方可以只设置一个或两个都设置,push 时会优先第一个,如果第一个没有,会再去找全局配置)

credential.helper:

  1. 默认所有都不缓存。
  2. “cache” 模式会将凭证存放在内存中一段时间。 密码永远不会被存储在磁盘中,并且在15分钟后从内存中清除。
  3. “store” 模式会将凭证用明文的形式存放在磁盘中,并且永不过期。 这意味着除非你修改了你在 Git 服务器上的密码,否则你永远不需要再次输入你的凭证信息。 这种方式的缺点是你的密码是用明文的方式存放在你的 home 目录下。

注意,如果设置为store后,如果你的git账户信息过期,或者你修改了密码后,将不能从远端拉取或推送代码。因为本地存储了你的账户信息,你需要重新设置credential.helper为空:git config [--global] credential.helper "",就可以手动输入新的账户信息,重新设置为store后又可以免去多次输入账户信息的麻烦。

3. 颜色

git config --global color.ui auto

4. 换行符

# 提交时转换为LF,检出时转换为CRLF
git config --global core.autocrlf true

# 提交时转换为LF,检出时不转换
git config --global core.autocrlf input

# 提交检出均不转换
git config --global core.autocrlf false

推荐:在Mac上设置 autocrlf = input, 在Windows上设置autocrlf = true(默认值)。这样即可保证仓库中永远都是LF。而且在Windows工作空间都是CRLF, 在Mac/Linux工作空间都是LF。

5. 查看所有可配置属性

git config --list

上面的指令可以查看所有可以配置的属性,和已经配置的属性值。

6. 忽略文件

可以通过添加.gitignore文件来避免纳入 Git 的管理范畴。

.gitignore文件只能作用在Untracked Files,也就是那些从来没有被 Git 记录过的文件(自添加以后,从未 add 及 commit 过的文件)。我们可以通过指令git rm --cached [file_name]来删除文件跟踪,但是不删除本地文件。

文件格式:

  1. 所有空行或者以 # 开头的行都会被 Git 忽略。
  2. 可以使用标准的 glob 模式匹配。
  3. 匹配模式可以以(/)开头防止递归。
  4. 匹配模式可以以(/)结尾指定目录。
  5. 要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。

2. 本地操作

1. 初始化git仓库和删除仓库

git init

rm -rf .git  # Linux
rd /s /q .git  # Windows

2. 查看状态和提交历史

  1. 查看状态报告
git status
-s,简览
  1. 查看文件差异
查看工作区和暂存区(暂存区不存在则为版本库)的差异,git diff [file-path]
查看暂存区与版本库的差异,git diff --cached [file-path]
查看工作区与指定版本库的差异,git diff <commit-id> [file-path]
查看两个版本库之间的差异,git diff <commit-id> <commit-id> [file-path]
  1. 查看提交历史
git log [branch_name]
-p,按补丁格式显示每个更新之间的差异。
--stat,显示每次更新的文件修改统计信息。
--pretty = oneline | format,使用其他格式显示历史提交信息。
--graph,显示 ASCII 图形表示的分支合并历史。
-{number},仅显示最近的 n 条提交。
  1. 查看某个commit的内容
git show [COMMIT]
git diff [COMMIT]^!
  1. git reflog

如果在回退以后又想再次回到之前的版本,git reflog 可以查看所有分支的所有操作记录(包括commit和reset的操作),包括已经被删除的commit记录,git log则不能察看已经删除了的commit记录。

3. 状态变更操作

  1. 跟踪文件
git add {file},把文件当前状态放入暂存区域
git add . ,提交新文件(new)和被修改(modified)文件,不包括被删除(deleted)文件
git add -u(--update),提交被修改(modified)和被删除(deleted)文件,不包括新文件(new)
git add -A(--all),提交所有变化
  1. 提交更新
git commit
-m {comment},备注信息
-a,自动把所有已经跟踪过的文件暂存起来一并提交,从而跳过 git add 步骤
-n --no-verify,绕过pre-coomit和commit-msg钩子
  1. 记录移除文件的操作,并连带从工作目录中删除指定的文件
git rm {file}
-f, 如果删除之前修改过并且已经放到暂存区域的话,则必须要用强制删除选项 -f。
--cached,想把文件从 Git 仓库中删除(亦即从暂存区域移除),但仍然希望保留在当前工作目录中。
  1. 移动文件
git mv {file_from} {file_to}

git mv 相当于下面三个操作:
$ mv {file_from} {file_to}
$ git rm {file_from}
$ git add {file_to}

4. 分支操作

  1. 新建分支、删除分支
git branch {branch_name}

git branch -d {branch_name},如果包含还未合并的工作,将会失败
git branch -D {branch_name},强制删除
  1. 切换分支
git checkout {branch_name},切换分支

git checkout -b {branch_name} [{remote_name}/{remote_branch}],创建并切换分支,还可以指定远程分支

git checkout -,切换到上一个分支
  1. 合并分支
git merge <branch>,合并分支
--no-ff,不使用fast-forward方式合并,保留分支的commit历史
--squash,使用squash方式合并,把多次分支commit历史压缩为一次
  1. 分支存档
git stash,存储工作目录和索引(暂存区域)的当前状态,并返回干净的工作目录
git stash save "comments",为stash添加备注
git stash pop,还原最近一次存储
git stash drop {stash_name},删除指定的stash
git stash list,查看存储列表
git stash show [-p] {stash_name},查看指定的stash的diff
git stash apply {stash_index},根据index还原存储

git stash会缓存下列文件:添加到暂存区的修改(staged changes);Git跟踪的但并未添加到暂存区的修改(unstaged changes)。

但不会缓存以下文件:在工作目录中新的文件(untracked files);被忽略的文件(ignored files)。可以通过加参数-u--include-untracked缓存新文件。

  1. 查看本地分支跟踪的远程分支
git branch -vv
  1. 切换本地分支跟踪的远程分支
git branch -u {remote_branch_name} {local_branch_name}

5. 版本操作

  1. 补充提交遗漏文件
git commit --amend
  1. git reset
git reset --soft {HEAD_pointer} [file_name],移动 HEAD 分支的指向
git reset --mixed {HEAD_pointer} [file_name],默认,使索引(暂存区域)看起来像 HEAD
git reset --hard {HEAD_pointer} [file_name],使工作目录和索引(暂存区域)看起来像 HEAD

说以下使用--soft的场景,假设你从特定版本(v1)开始修改代码,修改到了版本(v2),想要查看整个过程都修改了哪些文件的话,使用--soft把HEAD指向版本(v1),就可以看到整个过程修改的文件。

  1. git revert
git revert {HEAD_pointer},用一次新的commit来回滚之前的commit
  1. 合并快照
git rebase -i {startpoint}  [endpoint],合并startpoint到endpoint区间的提交操作

3. 远程操作

  1. 拷贝远程仓库
git clone {remote_url} [local_url]
  1. 添加远程仓库
git remote add {remote_name} {remote_url} # 添加一个新的远程仓库引用到当前的项目

git remote [-v] # 列出所有远程主机
git remote rm {remote_name} # 删除远程主机
git remote rename {remote_name} {new_remote_name} # 重命名远程主机
  1. 拉取
git fetch {remote_name} [branch_name] # 拉取远程主机更新,也可以指定某一分支

git pull {remote_name} {remote_branch_name}:{local_branch_name} # 拉取远程主机更新,再与本地指定的分支合并,如果是当前分支的话,:{local_branch_name}可以省略。
git pull origin master --allow-unrelated-histories,允许合并不相关历史的内容。

git pull = git fetch + git merge

  1. 推送
git push {remote_name} {local_branch_name}:{remote_branch_name} # 推送本地分支的更新到远程主机的指定分支。
git push --set-upstream {remote_name} {remote_branch_name} # 设置当前分支的默认远程主机上的分支。

如果推送的本地分支有跟踪分支,可以省略:{remote_branch_name}
如果推送的本地分支就是当前分支,可以省略{local_branch_name}
如果当前分支只有一个追踪分支,可以省略{remote_name}

  1. 跟踪分支
    跟踪分支是与远程分支有直接关系的本地分支。 如果在一个跟踪分支上输入 git pull,Git 能自动地识别去哪个服务器上抓取、合并到哪个分支。git push也是一样的。

git clone,会默认帮你把master和origin/master建立追踪关系

git branch -u {remote_branch_name} # 让当前分支和远程分支建立追踪关系

跟踪分支的设置在git config 里的 branch.<name>.merge

参考

Git远程操作详解
Git 分支 - 远程分支
git中配置autocrlf来正确处理crlf
git-stash用法小结
git取消文件跟踪
.gitignore模板

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions