Skip to content
gobomb edited this page Jul 11, 2019 · 15 revisions

git 常用命令

git 撤回操作:

  • $ git add .暂存后还没有 commit 可以使用$ git reset回退

取消暂存某个文件

$ git reset HEAD <file>
  • commit 完了才发现漏掉了几个文件没有加,或者提交信息写错了。 想要撤消刚才的提交操作,可以使用 --amend 选项重新提交:
$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend

上面的三条命令最终只是产生一个提交, 第二个提交命令修正了第一个的提交内容。

git add 只加修改和删除的

git add -u

清除未 add 的操作

git checkout . && git clean -xdf
--dry-run      -n  -- only show what would and what would not be removed
--exclude      -e  -- skip files matching specified pattern
--force        -f  -- required when clean.requireForce is true (default)
--interactive  -i  -- show what would be done and clean files interactively
--quiet        -q  -- don't print names of files removed
-X                 -- remove only ignored files
-d                 -- also remove untracked directories
-x                 -- also remove ignored files

文件 .gitignore 的格式规范

  • 所有空行或者以 # 开头的行都会被 Git 忽略。

  • 可以使用标准的 glob 模式匹配(可查看 https://github.com/gobomb/myDoc/wiki/regex)。

  • 匹配模式可以以(/)开头防止递归。

  • 匹配模式可以以(/)结尾指定目录。

  • 要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。

例子:

# no .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in the build/ directory
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory
doc/**/*.pdf

检查配置信息

如果想要检查你的配置,可以使用 git config --list 命令来列出所有 Git 当时能找到的配置。

$ git config --list
user.name=John Doe
user.email=johndoe@example.com
color.status=auto
color.branch=auto
color.interactive=auto
color.diff=auto
...

你可能会看到重复的变量名,因为 Git 会从不同的文件中读取同一个配置(例如:/etc/gitconfig~/.gitconfig)。 这种情况下,Git 会使用它找到的每一个变量的最后一个配置

你可以通过输入 git config <key>: 来检查 Git 的某一项配置

$ git config user.name
John Doe

使用 git log 命令查看各个分支当前所指的对象

提供这一功能的参数是 --decorate

$ git log --oneline --decorate

274d99f (HEAD -> master) update mac.md regex.md
4aaf798 (origin/master, origin/HEAD) upload other-notes.md
34bdd22 格式微调

子模块

子模块允许你将一个Git仓库当作另外一个Git仓库的子目录。这允许你克隆另外一个仓库到你的项目中并且保持你的提交相对独立。

$ git submodule add git://github.com/chneukirchen/rack.git rack

在父项目可以看到:

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#      new file:   .gitmodules
#      new file:   rack
#

$ cat .gitmodules
[submodule "rack"]
      path = rack
      url = git://github.com/chneukirchen/rack.git

git rebase

git checkout feature
git rebase master

加 -i 交互式地 rebase

pick 33d5b7a Message for commit #1
fixup 9480b3d Message for commit #2
squash 5c67e61 Message for commit #3 

pick 使用 commit

fixup 忽略 commit 信息

squash 压缩 commit

如有冲突,处理完冲突使用 git rebase --continue 继续 rebase

参考:

Pro Git

Github 命令

复制仓库(非fork

$ git clone --bare https://github.com/exampleuser/old-repository.git
$ cd old-repository.git
$ git push --mirror https://github.com/exampleuser/new-repository.git

问题

git clone 的时候

Initialized empty Git repository in /root/git/mr_top/.git/
error:  while accessing https://github.com/cloudtogo/mr_top.git/info/refs

fatal: HTTP request failed

yum update nss nss-util nspr

保存 diff 和打补丁

git diff > patch

git apply patch

同步上游代码

git remote add [upstream name] [upstream git address]
git fetch [upstream name]
git rebase [upstream name]/[branch name]
git push

go get 开始命令行登录

env GIT_TERMINAL_PROMPT=1 go get  [git url]
  1. 改变最近一次提交:

    修改并git add/rm [file]后,git commit --amend,也可以修改 commit 说明

  2. 落后远程分支几个版本,强行回退:

    1. git reset [commitID],到上个版本
    2. git stash,暂存修改
    3. git push --force, 强制push,远程的最新的一次commit被删除
    4. git stash pop,释放暂存的修改,开始修改代码
    5. git add . -> git commit -m "massage" -> git push
  3. 从某个 commit 中复制某个文件到暂存区:

    git reset [commit-hash] file.txt

    git add file.txt 的逆操作,可用来取消暂存:

    git reset (HEAD) file.txt

  4. git 的三棵树:

    用途
    HEAD 上一次提交的快照,下一次提交的父结点
    Index 预期的下一次提交的快照(也就是暂存区)
    Working Directory 沙盒
  5. git reset 命令会以特定的顺序重写这三棵树,在你指定以下选项时停止:

    1. 移动 HEAD 分支的指向 (若指定了 --soft,则到此停止)
    2. 使索引看起来像 HEAD (若未指定 --hard,则到此停止;即指定 --mixed 或默认情况)
    3. 使工作目录看起来像索引 (指定--hard,危险操作)
  6. 查看 git 远程仓库 URL:

    git remote -v

  7. git 查看某个文件的修改历史

    1. git log -p [filename] 查看文件的每一个详细的历史修改

    2. git log --pretty=oneline [filename] 每一行显示一个提交,先显示哈希码,再显示提交说明。

    3. git blame [filename] 查看文件的每一行是哪个提交最后修改的。

  8. git reflog:

    当你 (在一个仓库下) 工作时,Git 会在你每次修改了 HEAD 时悄悄地将改动记录下来。当你提交或修改分支时,reflog 就会更新。

查看 branch 之间的关系

git log --graph --decorate --oneline --simplify-by-decoration --all

Clone this wiki locally