1. 程式人生 > >Git命令匯總

Git命令匯總

orm targe num pgp sig rar reflog pom 其他

關於GIT,推薦閱讀 http://www.liaoxuefeng.com/ ,講的非常透徹清楚。


技術分享


創建GIT版本庫

git init

vim readme.txt

git commit -m "wrote a readme file"


常用分支內命令

git add <file>

git add -u 將所有修改過的Tracked files加入Stage

git add -a 將所有修改過的文件加入Stage

git status 查看工作空間和Stage信息

git commit -m "comments"

git log 查看 commit 歷史

git log --pretty=oneline 格式化查看

git log --graph --pretty=oneline --abbrev-commit 圖形化查看

git reflog 記錄每一次命令


分支管理命令

git branch <branch name> 創建分支

git checkout <branch name> 切換分支

git checkout -b <branch name> 創建分支並切換到某個分支

git checkout -d <branch name> 刪除分支

git branch 查看本地分支

git branch -a 查看所有分支

git branch -r 查看遠程分支


分支合並

git merge <branch name>

git merge --no-off -m "comments" dev 避免快速合並


版本回退與撤銷操作

git checkout <filename> : file stage -> workspace

git checkout HEAD <filename> : file Repo -> Stage -> workspace

git checkout HEAD : all files Repo -> Stage -> workspace

git reset : all files Repo -> Stage

git reset HEAD : all files Repo -> Stage

git reset HEAD <filename> : file Repo -> Stage

git reset --hard HEAD Stage回退到上一版本, HEAD指向上一版本 Repo -> Stage -> Workspace

git reset --hard 3628164 回退到版本號為3628164, HEAD指向3628164

git stash 把工作現場儲存起來

git stash pop 恢復工作現場


與遠程git服務器交互

git remote 顯示遠程信息

git remote -v

git push origin master 推送分支

git clone url 克隆遠程git

git checkout -b dev origin/dev 創建遠程origin dev到本地dev

git pull 從遠程分支下載最新代碼 git branch --set-upstream dev origin/dev

git push origin --delete <branch name> 刪除遠程分支

多人協作的工作模式通常是這樣:

  1. 首先,可以試圖用git push origin branch-name推送自己的修改;

  2. 如果推送失敗,則因為遠程分支比你的本地更新,需要先用git pull試圖合並;

  3. 如果合並有沖突,則解決沖突,並在本地提交;

  4. 沒有沖突或者解決掉沖突後,再用git push origin branch-name推送就能成功!

如果git pull提示“no tracking information”,則說明本地分支和遠程分支的鏈接關系沒有創建,用命令git branch --set-upstream branch-name origin/branch-name

這就是多人協作的工作模式,一旦熟悉了,就非常簡單。

小結

  • 查看遠程庫信息,使用git remote -v

  • 本地新建的分支如果不推送到遠程,對其他人就是不可見的;

  • 從本地推送分支,使用git push origin branch-name,如果推送失敗,先用git pull抓取遠程的新提交;

  • 在本地創建和遠程分支對應的分支,使用git checkout -b branch-name origin/branch-name,本地和遠程分支的名稱最好一致;

  • 建立本地分支和遠程分支的關聯,使用git branch --set-upstream branch-name origin/branch-name

  • 從遠程抓取分支,使用git pull,如果有沖突,要先處理沖突。


標簽管理

命令git tag <name>用於新建一個標簽,默認為HEAD,也可以指定一個commit id;

git tag v0.9

git tag -a <tagname> -m "blablabla..."可以指定標簽信息;

git tag v0.9 6224937

git tag -s <tagname> -m "blablabla..."可以用PGP簽名標簽;

命令git tag可以查看所有標簽。

git show v0.9

命令git push origin <tagname>可以推送一個本地標簽;

命令git push origin --tags可以推送全部未推送過的本地標簽;

命令git tag -d <tagname>可以刪除一個本地標簽;

命令git push origin :refs/tags/<tagname>可以刪除一個遠程標簽

git push origin --delete tag <tagname>

git fetch -p 刪除沒有和遠程分支對應的本地分支


GitHub

小結

  • 在GitHub上,可以任意Fork開源倉庫;

  • 自己擁有Fork後的倉庫的讀寫權限;

  • 可以推送pull request給官方倉庫來貢獻代碼。


git 上傳Maven項目

只要提交

  • src

  • pom.xml

  • README.MD

  • .gitignore

其他的文件都不需要也不應該被提交上來,否則很容易造成沖突。

提供個java的gitignore 模板:

### Java template
*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea/workspace.xml
.idea/tasks.xml
.idea/dictionaries
.idea/vcs.xml
.idea/jsLibraryMappings.xml

# Sensitive or high-churn files:
.idea/dataSources.ids
.idea/dataSources.xml
.idea/dataSources.local.xml
.idea/sqlDataSources.xml
.idea/dynamic.xml
.idea/uiDesigner.xml

# Gradle:
.idea/gradle.xml
.idea/

# Mongo Explorer plugin:
.idea/mongoSettings.xml

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
### Windows template
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk
### Maven template
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# ignore eclipse files
.project
.classpath
.settings
.metadata

Git命令匯總