工作区(working dir)
暂存区(Index / Stage)
本地仓库 (HEAD)
远程仓库 (Remote)
git config --global color.ui true 开启git output的颜色显示
git init 跟踪当前文件夹(dirctory), 并创建 .git的隐藏文件夹, 包含与git相关文件
git status 检查当前文件夹git状态
git add filename 跟踪文件夹中的文件(file)
git reset filename 放弃跟踪文件夹中的文件(file)
git checkout -- <file> 命令来撤销工作区的代码修改
git reset HEAD 将暂存区的代码撤销
git reset --hard HEAD~1 从本地仓库回滚上一次操作,保留回滚记录
git reset --hard HEAD^^ 从本地仓库向前回退两个版本,
git reset --hard 695ce1fe 从本地仓库命令回到回退之前注释695ce1fe的状态:
git rm file 删除文件夹中的文件
git commit 进入提交更改的注解vi模式
git commit -m "message" 提交更改简短注解
git commit --amend 修改已提交的变更的注释
git commit -a 自动把所有已经跟踪过的文件暂存起来一并提交,从而跳过 git add 步骤
git log 倒序排列修改的when, who and message (最上为最近一次)
git log --oneline 只显示第一行简短注释和短序列码
git log --pretty=short 只显示简短的注释
git log -p 看提交时文件的变化,加上-p参数即可
git reflog 显示整个本地仓库的commit, 包括branch
git diff 查看工作区和暂存区的区别
git diff HEAD 查看本地仓库区和暂存区的区别
git diff --staged 同上
git show 检查每个修改的明细(默认为最近的一次修改)
git rebase -i 提交历史的压缩
git remote add origin url 绑定远程仓库地址到本地
git fetch [remote-name] 从远程仓库拉
git push [remote-name] 向远程仓库推
git --amend 添加/修改上次git commit的内容
git rebase master 在branch分支修改,commit前先 同步master branch
.gitignore 来忽略不被git同步的文件/文件夹
# 此为注释 – 将被 Git 忽略
*.a # 忽略所有 .a 结尾的文件
!lib.a # 但 lib.a 除外
/TODO # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
build/ # 忽略 build/ 目录下的所有文件
doc/*.txt # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt
git rm -rf --cached . # 跟新 .gitignore untracked 的文件
1.工作区的代码想撤销
git checkout -- filename
2.add到暂存区(Stage)的代码想撤销
git reset HEAD
git checkout -- filename
3.提交到本地仓库(HEAD)的代码想撤销
git reset --hard HEAD^^ 或者 git reset --hard HEAD~3 或者 git reset --hard HEAD 哈希码
git checkout -- filename
1. 可以使用HEAD^来描述版本,一个^表示前一个版本,两个^^表示前两个版本,以此类推。
2.也可以使用数字来代替^,比如说前100个版本可以写作HEAD~100。
3.也可以直接写版本号,表示跳转到某一个版本处。我们每次提交成功后,都会生成一个哈希码
# Rollback with history:
git revert HEAD
git commit -m "roll back with new commit"
git push origin main
# Rollback with no history:
git reset HEAD~1 (保留staging work)
git reset --hard HEAD~1 (不保留staging work)
git push -f origin main
What If I Already Have It Checked In?
Git will not ignore the file if you've already committed it. You'll have to untrack the file first, then it will start ignoring it. You can untrack the file with this command:
git rm --cached FILENAME
默认Branch Master
- git checkout DEV
- git checkout UAT
要合并其他分支到你的当前分支(例如 master),执行:
git merge <branch>
分支是用来将特性开发绝缘开来的。在你创建仓库(git init)的时候,master 是"默认的"分支。在其他分支上进行开发,完成后再将它们合并到主分支上。
我们也可以使用 git checkout -b (branchname) 命令来创建新分支并立即切换到该分支下,从而在该分支中操作。
创建一个叫做"feature_x"的分支,并切换过去:
git checkout -b feature_x
当你切换分支的时候,Git 会用该分支的最后提交的快照替换你的工作目录的内容, 所以多个分支不需要多个目录
切换回主分支:
git checkout master
分支合并 (git merge) 另一个方向的git pull
一旦某分支有了独立内容,你终究会希望将它合并回到你的主分支。 你可以使用以下命令将任何分支合并到当前分支中去:
$ git:(master) # git branch
* master
branch-no-TF
$ git:(master) # ls
hello-git.txt terraform.txt
$ $ git:(master) # git merge branch-no-TF
Updating 3e326eb..a9e2f96
Fast-forward
runoob.php | 0
terraform.txt | 1 -
2 files changed, 1 deletion(-)
create mode 100644 runoob.php
delete mode 100644 terraform.txt
$ ls
hello-git.txt runoob.php
以上实例中我们将 branch-no-TF 分支合并到主分支去,terraform.txt 文件被删除。
合并完后就可以删除分支:
$ git branch -d branch-no-TF
Deleted branch branch-no-TF (was a9e2f96).
删除后, 就只剩下 master 分支了:
$ git branch
* master
再把新建的分支删掉:
git branch -d feature_x
git branch -m | -M oldbranch newbranch重命名分支,如果newbranch名字分支已经存在,则需要使用-M强制重命名,否则,使用-m进行重命名
git branch -d local_branch 删除本地分支
合并并不仅仅是简单的文件添加、移除的操作,Git 也会合并修改。
除非你将分支推送到远端仓库,不然该分支就是 不为他人所见的:
git push origin <branch>
git push -u origin master 将本地仓库文件提交到远程仓库主干上
git push origin :remote_branch 删除远程分支(本地分支还在保留)
git push origin branch_name 推送本地分支到远程
git clone
从远端克隆
git pull
从远端repo下拉更新
git pull is really equivalent to running git fetch and then git merge
So, suppose you've got a remote called origin that refers to your GitHub repository, you would do:
git fetch origin
... and then do:
git diff master origin/master
in order to see the difference between your master, and the one on GitHub. If you're happy with those differences, you can merge them in with
git merge origin/master, assuming master is your current branch.
git log
在使用 Git 提交了若干更新之后,又或者克隆了某个项目,想回顾下提交历史,我们可以使用 git log 命令查看。
针对我们前一章节的操作,使用 git log 命令列出历史提交记录如下:
git log - 查看历史提交记录
git blame <file> - 以列表形式查看指定文件的历史修改记录。
我们可以用 --oneline 选项来查看历史记录的简洁的版本
$ git log --oneline
从最初开始查询
git log --reverse --oneline
指查询部分用户
git log --author=Linus --oneline -5
查询指定时间段内的更改
git log --oneline --before={3.weeks.ago} --after={2010-04-18} --no-merges
conor_wu@conor-wus-MacBook-Pro:~/Desktop/Python$ git remote -v
origin git@github.airbnb.biz:conor-wu/Coolmine.git (fetch)
origin git@github.airbnb.biz:conor-wu/Coolmine.git (push)
conor_wu@conor-wus-MacBook-Pro:~/Desktop/Python$ git remote -v
origin git@github.airbnb.biz:conor-wu/Coolmine.git (fetch)
origin git@github.airbnb.biz:conor-wu/Coolmine.git (push)
conor_wu@conor-wus-MacBook-Pro:~/Desktop/Python$ git remote rm origin
conor_wu@conor-wus-MacBook-Pro:~/Desktop/Python$ git remote -v
conor_wu@conor-wus-MacBook-Pro:~/Desktop/Python$ git remote add origin git@github.airbnb.biz:conor-wu/Coolmine.git
conor_wu@conor-wus-MacBook-Pro:~/Desktop/Python$ git push -u origin
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
conor_wu@conor-wus-MacBook-Pro:~/Desktop/Python$ git push -u origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 249 bytes | 249.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.airbnb.biz:conor-wu/Coolmine.git
314b75f..9bc55a6 master -> master
conor_wu@conor-wus-MacBook-Pro:~/Desktop/Python$ git push -u origin master
Branch 'master' set up to track remote branch 'master' from 'origin'.
Everything up-to-date
git diff master origin/master
git push --force