Git的一些操作
流程例子
-
將本地代碼上傳到github的操作流程
新建說明文件
touch README.md
在當(dāng)前項(xiàng)目目錄中生成本地git管理,并建立一個(gè)隱藏的.git目錄
git init
添加當(dāng)前目錄中的所有文件到索引
git add .
提交到本地源碼庫(kù)食铐,并附加描述
git commit -m 'commit description'
添加到遠(yuǎn)程目錄
git remote add origin https://github.com/username/
project.git
把本地源碼庫(kù)push到github名為branchname的分支中
git push -u origin branch_name
-
更新代碼到github
git add .
git commit -m 'update description'
git push -u origin branch_name
-
復(fù)制github的項(xiàng)目到本地倉(cāng)庫(kù)
git clone htts:github.com/username/project.git
-
同步github最新代碼到本地倉(cāng)庫(kù)
git checkout branch_name
git pull
-
在branch_name分支上與master保持同步最新代碼
在branch_name分支上執(zhí)行以下代碼
git rebase master
git checkout master
git merge branch_name
git push
常用命令
- 配置用戶名
git config --global user.name 'username'
- 配置用戶郵箱
git config --global user.email 'usereamil'
- 初始化項(xiàng)目目錄
git init
- 創(chuàng)建README.md文件
touch README.md
git add READEME.md
- 添加README.md內(nèi)容
git commit -m 'first commit'
- 設(shè)置要上傳的倉(cāng)庫(kù)目錄
git remote add origin https://github.com/username/project.git
- 如果這句報(bào)錯(cuò):
fatal: remote origin already exists
- 則輸入下面這句:
git remote rm origin
- 上傳到倉(cāng)庫(kù)
git push -u origin master
- 刷新后如果只有README.md文件畴椰,進(jìn)行以下操作:(這個(gè)我主要在上傳到開源中國(guó)的碼云的時(shí)候遇到過)
git add
git commit -m 'first commit'
git push -u origin master
- 如果上傳到github出現(xiàn)
error: failed to push some refs to...
- 原因是github中的README.md文件不在本地代碼目錄中窜护,解決方案如下:
git pull --rebase origin master
Tips:
pull = fetch + merge
- 執(zhí)行完上面那句再執(zhí)行
git push -u origin master
即可將代碼上傳到github
git生成ssh驗(yàn)證碼
ssh-keygen -C 'email@address.com' -t rsa
- git倉(cāng)庫(kù)的代碼到本地(注意酷窥,clone整個(gè)項(xiàng)目的文件夾)
git clone https://github.com/username/project.git
- 在本地添加一個(gè)分支
git branch branch_name
- 切換到新建分支
git checkout branch_name
- 將新建分支發(fā)布到github
git push origin branch_name
- 在本地刪除一個(gè)分支
git branch -d branch_name
- github遠(yuǎn)程刪除一個(gè)分支
git push origin :branch_name
- 遠(yuǎn)程創(chuàng)建branch_name分支并把master的倉(cāng)庫(kù)clone一份
git push origin master:branch_name
- 查看遠(yuǎn)程分支
git branch -r
- 添加例外文件
添加
.gitignore
文件贫橙,并在文件中添加例外規(guī)則即可捍歪,規(guī)則說明如下:
忽略所有目錄中的
.idea
目錄的全部?jī)?nèi)容矾踱,不管是在根目錄還是子目錄下的idea
都會(huì)被忽略
.idea/*
忽略根目錄中的
idea
/.idea/*
跟蹤
.gitignore
文件
忽略全部?jī)?nèi)容恨狈,但是不忽略.gitignore
文件
!.gitignore
清空暫存區(qū)
git rm -r --cached .