1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
| // 安装git版本控制工具 $ sudo apt-get install git // apt get $ sudo apt-get install git-core // outdate $ sudo yum install git // yum
// 指定用户和Email地址 $ git config --blobal user.name "Your Name" $ git config --global user.email "email@example.com"
// Git显示颜色 $ git config --global color.ui true
// 配置命令别名 $ git config --global alias.<order_name> <previous_name> // $ git config --global alias.st status // $ git config --global alias.last 'log -l' // git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
// 查看配置信息 $ git config --list
// 初始化管理仓库 $ git init /Users/michael/learngit/.git/
// 将文件添加到暂缓区 $ git add <file>
// 将文件从暂缓区提交到git仓库 $ git commit -m <message>
// 查看当前git状态 $ git status // e.g. $ git status -s
// 查看文件是否有新改动 $ git diff <file> // e.g. $ git diff --staged // e.g. $ git diff --cached
// 查看修改日志 $ git log // e.g. git log --pretty=oneline --abbrev-commit // e.g. git log -p -2 // -p查看修改差异 // e.g. 还可以定制输出格式 $ git log --pretty=format:"%h - %an, %ar : %s" /* %H: 提交对象(commit)的完整哈希字串 %h: 提交对象的简短哈希字串 %T: 树对象(tree)的完整哈希字串 %t: 树对象的简短哈希字串 %P: 父对象(parent)的完整哈希字串 %p: 父对象的简短哈希字串 %an: 作者(author)的名字 %ae: 作者的电子邮件地址 %ad: 作者修订日期(可以用 --date= 选项定制格式) %ar: 作者修订日期,按多久以前的方式显示 %cn: 提交者(committer)的名字 %ce: 提交者的电子邮件地址 %cd: 提交日期 %cr: 提交日期,按多久以前的方式显示 %s: 提交说明 */
// 返回到上一个版本 $ git reset --hard HEAD^ // e.g. HEAD^^表示上上个版本,HEAD~100返回上100个版本
// 通过指定SHA-1编码恢复版本 $ git reset --hard <code> // e.g. git reset --hard 1094a
// 查看每一次命令的记录 $ git reflog
// 查看工作区和版本库里最新版本的区别 $ git diff HEAD -- <file> // e.g. git diff HEAD -- what.cpp
// 将暂存区的修改撤销掉,放回工作区 $ git reset HEAD <file>
// 丢弃工作区的修改 $ git checkout -- <file>
// Git中对文件改名 $ git mv <pre_name> <cur_name> /* 相当于 $ mv a.left a.right $ git rm a.left $ git add a.right */
// 删除文件 $ git rm <file> // e.g. 放到暂存区的要用 git rm -f <file>
// 克隆文件 $ git clone git@github.com:<user>/<Repository_name>.git
// 创建分支,然后切换到该分支 $ git checkout -b <branch_name> // 相当于以下两条指令 $ git branch <branch_name> // 创建分支 $ git checkout <branch_name>// 选择该分支
// 查看分支 $ git branch // e.g. git branch -v // 查看每个分支最后一次提交
// 删除分支 $ git branch -d <branch_name>
// 将分支的结果合并到master分支上 $ git merge <branch_name> // e.g. 合并分支请注意--no-ff参数,表示禁用Fast forward;以普通模式合并,合并后的历史有分支,能看出来曾经做过合并,而fast forward合并就看不出来了 // e.g. $ git merge --no-ff -m "merge with no-ff" dev
// 查看分支的合并情况 $ git log --graph --pretty=oneline --abbrev-commit
// 如果用户主目录没有.ssh目录、id\_rsa和id\_rsa.pub就创建SSH Key: $ ssh-keygen -t rsa -C "youremail@example.com"
// 远程分支它以(remote)/(branch)形式命名
// 关联origin远程库 $ git remote add <remote> git@github.com:<user>/<Repository_name>.git github仓库 $ git remote add <name> <user>@<Ip_address>:/warehouse/<Repository_name>.git // 个人服务器示例(SSH) // e.g. origin也可以改成别的,不是固定 // e.g. $ git remote add origin git@github.com:example/test.git
// 远程仓库重命名 $ git remote rename <pre_remote> <cur_remote>
// 远程仓库的移除 $ git remote rm <remote>
// 删除远程分支 $ git push origin --delete <remote-branch>
// 查看远程库的信息 $ git remote -v // e.g. master dev bug feature
// 查看远程仓库信息 $ git remote show <remote>
// 获得远程引用的完整列表 $ git ls-remote <remote>
// 将本地库的内容推送到远程库上 $ git push -u origin master // e.g. master是指其中一个branch.可以通过serverfix:awesomebranch方式来将本地的serverfix分支推送到远程仓库上的awesomebranch分支 $ git push origin master:serverfix
// 从远程仓库抓取分支(分离状态的) $ git fetch <remote> <branch> // e.g. 本地不会生成可编辑的副本.它不会有serverfix分支,只有一个不可修改的origin/serverfix指针
// 从远程仓库获取并建立<branch>分支 $ git fetch <remote> <remote-branch>:<local-branch> // e.g. git fetch origin master:test // e.g. git diff tmp // 对比分支 $ git checkout -b serverfix origin/serverfix $ git checkout --track origin/serverfix
// 从远程库获取最新版本并merge到本地 $ git pull <remote> <branch> // e.g. 需要指定本地分支与远程origin/分支的连接.以下以dev分支为例 // $ git branch --set-upstream-to=origin/dev dev
// 比较本地master分支和origin/master分支差别 $ git log -p master ..origin/master
// 将stash储藏起来,等以后恢复stash(dirty) $ git stash
// 查看stash列表 $ git stash list
// 恢复stash $ git stash apply
// 恢复指定stash $ git stash apply stash@{0}
// 删除stash $ git stash drop
// 恢复stash的同时删除stash内容 $ git stash pop
// 尝试重新应用暂存的修改 $ git stash apply --index
// 不要储藏任何已经通过git add暂存的东西 $ git status -s $ git status --keep-index
// 储藏任何创建的未跟踪的文件 $ git stash --include-untracked $ git stash -u
// 交互式提示哪些改动想要储存,哪些改动想要保存工作目录中 $ git stash --patch
// 从储藏创建一个分支 $ git stash branch <branch>
// 变基 $ git rebase // 跳过server分支,将client分支与master分支合并 $ git rabase -onto master server client // e.g. 以上命令意思是:"取出client分支,找出处于client分支和server分支的共同祖先之后的修改,然后把他们放在master分支上重放一遍." $ git rebase master server // 以上命令可以直接将特性分支变基到目标分支上,即是说,把server分支续到master分支之后,省去得先切到server分支
// 打标签 $ git tag <tag_name> [commit_id] // e.g. git tag v1.0 // e.g. 再加一个参数是对应的commit id打上标签 // e.g. git tag v1.0 f52c633 // -m 指定说明文字 -a指定标签名
// 查看所有标签 $ git tag
// 查看标签信息 $ git show <tag_name>
// 查看标签所指文件版本 $ git checkout <tag_name> // e.g. 这会使你的仓库处于分离头指针(detached HEAD)状态.也就是说任何修改标签都不会变化,不属于任何分支,也无法访问. // e.g. 因此要么在初始阶段创建新分支关联,要么在切走分支时,和提交校验码git branch关联.否则将会丢失它 $ git checkout -b version2 v2.0.0 $ git branch version2 XXXXXXX
// 删除标签 $ git tag -d <tag_name>
// 推送某个标签到远程 $ git push origin <tag_name>
// 一次性推送全部尚未推送到远程的本地标签 $ git push origin --tags
// 删除远程标签 $ git tag -d <tag_name> $ git push origin :refs/tags/<tag_name>
// 添加被.gitignore忽略的文件: $ git add -f <file_name>
// 检查.gitignore文件规则 $ git check-ignore -v <file_name>
// 删除工作目录 $ git clean -n // 提示将要删除什么文件 -d // 移除文件 -x // 不忽略.gitignore文件规则删除所有 -i // 交互式提醒
|