Git基础-获取仓库、提交、查看历史、撤销
2019獨角獸企業重金招聘Python工程師標準>>>
1 獲取git倉庫
有兩種取得git倉庫的方法:
- 在現有目錄中初始化倉庫
- 另一種是克隆現有倉庫
第二種會在本地建一個mylibgit的倉庫名
2 記錄每次更新到倉庫
文件暫存
git add,意思是“添加內容到下一次提交中”,這是一個多功能命令
- 開始跟蹤新文件
- 將已跟蹤的文件放到暫存區
- 合并時,把有沖突的文件標記為已解決狀態
狀態簡覽
git status -s:查看文件狀態
$ git status -sM README MM Rakefile A lib/git.rb M lib/simplegit.rb ?? LICENSE.txtM靠右:表示文件修改,但未放到暫存區
M靠左:表示文件已修改,并且已放到暫存區
?
忽略文件
.gitignore 文件記錄忽略的文件
# 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 diff:比較工作目錄中當前文件和暫存區快照之間的差異,也就是修改之后還沒有暫存的變化內容
git diff --staged:查看已暫存的,將要添加到下次提交里的
?
提交更新
git commit:提交記錄的是放在暫存區的快照,以后可以回到這個狀態,或比較
?
跳過使用暫存區
git commit -a:自動將已跟蹤的文件暫存起來一并提交,從而跳過git add 步驟
?
移除文件
git rm: 從跟蹤文件清單(確切說是暫存區)中移除,并連帶從工作目錄中刪除
git rm -f:強制刪除,如果文件修改已經放到暫存區
git rm --cached fileName:文件保留在磁盤,git不再跟蹤
?
移動文件
git mv file_from file_to:重命名
?
3 查看提交歷史
git log
git log -p -2:顯示最近兩次提交的內容差異
git log --stat:顯示每次提交的簡略統計信息
git log --pretty=oneline:oneline表示每個提交放在一行顯示,另外還有full, fuller, short
git log --pretty=format:"%h - %an, %ar : %s":定制要顯示的記錄格式
下面的例子顯示了提交對象的短哈希,作者名字,修訂日期,提交說明
ca82a6d - Scott Chacon, 6 years ago : changed the version number 085bb3b - Scott Chacon, 6 years ago : removed unnecessary test a11bef0 - Scott Chacon, 6 years ago : first commit添加ASCII字串形象展示分支、合并歷史:
$ git log --pretty=format:"%h %s" --graph * 2d3acf9 ignore errors from SIGCHLD on trap * 5e3ee11 Merge branch 'master' of git://github.com/dustin/grit |\ | * 420eac9 Added a method for getting the current branch. * | 30e367c timeout code and tests * | 5a09431 add timeout protection to grit * | e1193f8 support for heads with slashes in them |/ * d6016bc require time for xmlschema * 11d191e Merge branch 'defunkt' into local查看最近兩周的提交, 2008年10月1日后的提交
$ git log --since=2.weeks $ git log --since="2008-10-01"?
4 切分支
git checkout -b [分支名] [遠程名]/[分支名]
git checkout --track origin/serverfix (git 1.6.2以上)
?
本地分支名為sf,不同于遠程分支名
git checkout -b sf origin/serverfix?
?
5 push到遠程倉庫
git push (遠程倉庫名) (分支名)
比如:git push origin serverfix
Git 自動把?serverfix?分支名擴展為 refs/heads/serverfix:refs/heads/serverfix ,意為“取出我在本地的 serverfix 分支,推送到遠程倉庫的 serverfix 分支中去”。
也可以運行:git push origin serverfix:serverfix
若想把遠程分支叫作 awesomebranch,可以用 git push origin serverfix:awesomebranch?來推送數據?
?
6 撤銷操作
修改提交(比如,有漏提交的文件)
git commit --amend:提交暫存區的文件,如果文件未改變,那么修改的只是提交信息,最終,第二次的提交會替代第一次的提交
$ git commit -m 'initial commit' $ git add forgotten_file $ git commit -m 'initial commit' --amend?
取消暫存的文件(取消git add 的文件)
git reset HEAD <file>
$ git add * $ git status On branch master Changes to be committed:(use "git reset HEAD <file>..." to unstage)renamed: README.md -> READMEmodified: CONTRIBUTING.md?
撤銷對文件的修改
git checkout -- <file>
Changes not staged for commit:(use "git add <file>..." to update what will be committed)(use "git checkout -- <file>..." to discard changes in working directory)modified: CONTRIBUTING.md $ git checkout -- CONTRIBUTING.md $ git status On branch master Changes to be committed:(use "git reset HEAD <file>..." to unstage)renamed: README.md -> README?
?
?
參考資料:https://git-scm.com/book/zh/v2
轉載于:https://my.oschina.net/u/2510955/blog/832199
總結
以上是生活随笔為你收集整理的Git基础-获取仓库、提交、查看历史、撤销的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS------给字体添加边框时,边框
- 下一篇: jetty权威指南