Git基本使用

一、基本命令

1、初始化倉(cāng)庫(kù)
git init
2、配置倉(cāng)庫(kù)
git config user.name xxx
git config user.email xxx
git config --global user.name xxx
git config --global user.email xxx

前兩種是配置在項(xiàng)目文件夾下,后兩個(gè)是配置全局的
3、指南使用
git help 子命令
退出       Q
下一頁(yè)     空格
上一頁(yè)     control+B
搜索      / 然后輸入要搜索的內(nèi)容后回車(chē)

二、常用指令

git status               查看文件狀態(tài)
git add                  添加文件的暫存區(qū)
git commit 文件名稱(chēng)       添加到本地倉(cāng)庫(kù)
    注意:如果沒(méi)有在commit后面加上 -m說(shuō)明修改了什么,
    會(huì)自動(dòng)進(jìn)入vim界面,要求我們輸入修改信息
    輸入修改信息步驟:
       按鍵盤(pán)上的i
       輸入信息
       按鍵盤(pán)上的esc
       輸入:wq然后回車(chē)
       
git config alias.別名 指令名
    git config alias.st status 這種方法設(shè)置的別名是局部的
    git config --global alias.st status 這樣設(shè)置是全局的
    當(dāng)然了,不建議去起別名
    
git log           查看所有版本庫(kù)日志
git log 文件名     查看指定文件名的版本庫(kù)日志
    git中的版本號(hào)是一個(gè)哈希值

git reflog        查看版本庫(kù)所有修改(包括提交和回退)

回退到相應(yīng)版本號(hào)(--hard表示強(qiáng)制)
git reset --hard HEAD   回到當(dāng)前版本,放棄所有沒(méi)有提交的修改(git checkout 文件名 具有相同功能)
git reset --hard HEAD^  回到上一個(gè)版本
git reset --hard HEAD~(3) 回到之前第三個(gè)修訂版本
get reset --head 版本號(hào)    回到相應(yīng)的版本號(hào)
    (版本號(hào)是取前7位,可以通過(guò)git reflog指令獲得)
    
git diff 文件名    查看文件修改了什么地方

三、遠(yuǎn)程倉(cāng)庫(kù)配置

這里的遠(yuǎn)程倉(cāng)庫(kù)可以是在本地的,也可以是在公司服務(wù)器上的。

1、git init --bare

注意:這個(gè)倉(cāng)庫(kù)是用于管理代碼的,和git init生成的是不一樣的


Paste_Image.png
2、先克隆一份空的倉(cāng)庫(kù)到本地
git clone /Users/username/Desktop/StudyCode/遠(yuǎn)程倉(cāng)庫(kù)
3、忽略不需要加入版本控制的文件以及文件夾(這里的配置方法是在github上搜索的,里面的內(nèi)容請(qǐng)搜索.gitignore)
.gitignore
echo -e "# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## Build generated
build/
DerivedData/

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata/

## Other    
*.moved-aside
*.xccheckout
*.xcscmblueprint

## Obj-C/Swift specific
*.hmap
*.ipa

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://github.com/fastlane/fastlane/blob/master/docs/Gitignore.md

fastlane/report.xml
fastlane/screenshots > .gitignore

注意:生成好的.gitignore文件一定要和.git隱藏文件夾在同一級(jí)目錄

4、將.gitignore添加到版本控制
git add .gitignore
git commit .gitignore -m"配置忽略文件"
5、新建項(xiàng)目

新建項(xiàng)目之后,可以直接使用Xcode中的工具提交

source control -> commit    將代碼提交到本地倉(cāng)庫(kù)
source control -> push      將代碼提交到遠(yuǎn)程倉(cāng)庫(kù)
注意:git中默認(rèn)會(huì)創(chuàng)建一個(gè)分支,這個(gè)分支叫做origin/master
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容