git - yuhannah/skills_map GitHub Wiki

Git 常用命令

配置工具

git config --list # 查看所有配置
# 全局配置(单用户时为所有仓库配置)
git config --global user.name "[name]"
git config --global user.email "[email address]"
git config --global color.ui auto # 彩色命令行输出
git config --global --unset user.name # 删除全局配置
git config --global --edit # 编辑全局配置文件
# 局部配置(多用户时在**各自的仓库**下配置)
git config --local user.name "[name]"
git config --local user.email "[email address]"

创建仓库

# 在本地创建仓库并连接远程空仓库
git init # 初始化仓库,在当前目录下生成 .git 文件夹
git remote add origin [url] # 链接到一个**空的远程仓库** 
# 拉取一个远程仓库到本地
git clone [url] # 克隆一个现有的远程仓库到当前目录下,不改名
git clone [url] newname # 克隆一个现有的远程仓库到当前目录下,改名

查看/添加/删除/修改仓库

git remote show origin # 查看当前仓库地址
git remote # 查看当前的远程仓库
git remote -v # 列出当前远程仓库的详细信息 [url]
# 添加仓库
git remote add [shortname] [url] # 添加远程仓库
git remote add origin [url]
# 删除仓库
git remote rm [name]

远程仓库改了名称,本地和远程的代码没有任何改变:

# 方法一:在本地修改远程仓库的地址
git remote set-url origin newAddress
# 方法二:先删除,后添加
git remote rm origin
git remote add origin newAddress

查看状态

git status

新建文档

touch filename

退出 vim 编辑器

# 先按 ESC
:q! # 强制退出不保存
:q # 退出不保存
:wq # 退出并保存后面也可以加个!

分支 Branches

(1) 创建和切换分支

git branch [branch-name] # 创建新的分支
git switch [branch-name] # 切换到[branch-name]分支
git checkout [branch-name] # 切换到[branch-name]分支

(2) 查看所有分支

git branch # 获得所有现有 branch 的列表
  experimental
* master # 星号标记当前所在的分支

(3) Combines the specified branch’s history into the current branch. This is usually done in pull requests(拉取请求PR), but is an important Git operation.

git merge [branch] # 拉取并合并[branch]到当前分支

如果没有冲突,则合并完成。如果有冲突,需要解决冲突再提交。使用git diff查看当前冲突内容。

(4) Deletes the specified branch

git branch -d [branch-name] # 删除无修改的分支
git branch -D [branch-name] # 强行删除分支

(5) 图形界面显示

gitk # 将以图形方式很好地显示结果历史记录

进行更改

(1) 查看日志

git log # 查看分支的历史版本
git log --follow [file] # 查看文件的历史版本
git log -p # 在每个步骤中看到完整的差异
git log --stat --summary # 在每个步骤中看到更改的概述

(2) 显示差异

git diff # 显示已进行但尚未添加到索引中的所有更改
git diff --cache # 查看即将提交的内容
git diff [first-branch]...[second-branch] # 显示两个分支之间的差异

(3) Outputs metadata(元数据) and content changes of the specified(指定) commit

git show [commit]

(4) Snapshots(快照处理) the file in preparation for versioning

git add [file] # 添加文件
git add . # 添加全部提交内容

(5) Records(记录) file snapshots permanently in version history

git commit -m "[descriptive message]"

(6) 合并 add 和 commit

git commit -a # 自动注意到所有已修改(但不是新的)文件,将它们添加到索引中并提交

重做提交 Redo commits

Erase mistakes and craft(构建) replacement history

(1) Undoes(撤销) all commits after [commit], preserving changes locally(在本地保存更改)

git reset [commit]

(2) Discards(放弃) all history and changes back to the specified(指定) commit

git reset --hard [commit]

CAUTION! Changing history can have nasty side effects(不良后果). If you need to change commits that exist on GitHub (the remote), proceed with caution. If you need help, reach out at github.community or contact support.

The .gitignore file

Sometimes it may be a good idea to exclude files from being tracked with Git. This is typically done in a special file named .gitignore. You can find helpful templates for .gitignore files at github.com/github/gitignore.

将已经添加到git中的文件再添加gitignore文件进行文件过滤

1.为避免冲突先同步下远程仓库

git pull

2.在本地项目目录下清除缓存

git rm -r --cached .

3.新建/修改.gitignore文件

4.再次Add所有文件

git add .

5.再次添加commit

git commit -m "add ignore"

6.最后提交到远程仓库

git push

同步更改 Synchronize changes

Synchronize(同步) your local repository with the remote repository on GitHub.com

(1) Downloads all history from the remote tracking branches

git fetch

(2) Combines(合并) remote tracking branches into current local branch

git merge

(3) Uploads all local branch commits to GitHub

git push
git push <远程主机名> <本地分支名>:<远程分支名>
git push -u origin master # 第一次要用-u 以后不需要

(4) Updates your current local working branch with all new commits from the corresponding remote branch on GitHub. git pull is a combination of git fetch and git merge

git pull
git stash # 将没有提交的修改暂存到 stash 中
git checkout . # 撤除本地没有提交的修改
# 回到稳定版本,撤销未提交的修改,删除 untrack files
git reset --hard commit_id  # 不保留未提交的修改
git reset --soft commit_id  # 默认方式,保留未提交的修改

辅助命令

查看命令手册

git help log

附录A:术语表Glossary

  • git: an open source, distributed(分布式) version-control system
  • GitHub: a platform for hosting(托管) and collaborating(协作管理) on Git repositories
  • commit: a Git object, a snapshot(快照) of your entire repository compressed into a SHA
  • branch: a lightweight(轻型) movable pointer to a commit
  • clone: a local version of a repository, including all commits and branches
  • remote: a common repository on GitHub that all team members use to exchange their changes
  • fork: a copy of a repository on GitHub owned by a different user
  • pull request: a place to compare and discuss the differences introduced on a branch with reviews(评审), comments, integrated tests(集成测试), and more
  • HEAD: representing your current working directory, the HEAD pointer can be moved to different branches, tags, or commits when using git checkout

删除untrack files

  • 删除当前目录下untrack文件,不包括文件夹和.gitignore中指定的文件和文件夹
git clean -f
  • 删除当前目录下untrack文件和文件夹, 不包括.gitignore中指定的文件和文件夹
git clean -df
  • 删除当期目录下的所有untrack的文件和文件夹
git clean -xdf
  • 显示会被删除的文件
git clean -nxfd
git clean -nf
git clean -nfd

使用git切换到tag

git clone 整个仓库后使用,以下命令就可以取得该 tag 对应的代码了。

git checkout tag_name 

但是,这时候 git 可能会提示你当前处于一个“detached HEAD" 状态。

因为 tag 相当于是一个快照,是不能更改它的代码的。

如果要在 tag 代码的基础上做修改,你需要一个分支:

git checkout -b branch_name tag_name

这样会从 tag 创建一个分支,然后就和普通的 git 操作一样了。

命名规则

GIT库名一律采用项目名-类型-年月的形式。其中对项目名的规范如下:

  1. 库名中不得出现下述规定的字符- \ @ ! # $ % ^ & * () [] {} | \ ; : '' < > · ~
  2. 库名应尽量避免使用 名.名的形式。
  3. 库名应尽量使用英文,禁止使用中文字符。一般情况下,库名中出现的各个单词的首字母应使用大写。各个单词不能使用连接符 -连接;如有必要,应使用下划线 _
  4. 缩写的单词一律使用大写。
  • 类型的规定如下:
  • TST——测试项目
  • BC——已完成项目
  • SUS——被搁置项目
  • ING——正在进行的项目
  • TST-BC——已完成的测试项目
  • TST-SUS——被搁置的测试项目
  • TST-ING——正在进行的测试项目

gitk 用于查看当前分支的所有commit和合并情况。但是不能同时查看不同分支的拆分情况。 git merge-base ID1 ID2 # 用于查看两个分支的最近的母commit

⚠️ **GitHub.com Fallback** ⚠️