1. 程式人生 > >Git本地init與push到遠端倉庫

Git本地init與push到遠端倉庫

1、先進行git配置項檢視與修改初始化

.git刪除:rm -rf .git

檢視資訊:git config --list

$ git config --list
core.symlinks=false
core.autocrlf=true
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
http.sslcainfo=C:/APP/Git/mingw64/ssl/certs/ca-bundle.crt
diff.astextplain.textconv=astextplain
rebase.autosquash=true
credential.helper=manager
...


修改資訊,例如使用者名稱與郵箱:

git config --global user.name "name"

git config --global user.email "email"

賬號、密碼修改

git config –system –unset credential.helper  #重置認證資訊

git config –global http.emptyAuth true  #認證清除

(先進入專案資料夾)通過命令 gitinit 把這個目錄變成git可以管理的倉庫

git init

2、把檔案新增到本地版本庫中,使用命令git add 檔案;新增到暫存區裡面去,如果後面接小數點“.”,意為新增資料夾下的所有檔案

git add .

3、用命令 git commit告訴Git,把檔案提交到倉庫。引號內為提交說明

git commit -m 'first commit'

4、關聯到遠端庫

git remote add origin 你的遠端庫地址

如:

git remote add origin [email protected]:/srv/sample.git

如果上面步驟寫錯了:則      

git remote rm origin   #刪除origin

git remote add origin [email protected]:yourname/demo.git   #重新新增origin

5、獲取遠端庫與本地同步合併(如果遠端庫不為空必須做這一步,否則後面的提交會失敗)

git pull --rebase origin master

6、將最新的修改推送到遠端倉庫

    備註:origin:遠端倉庫名字;master:分支

注意:我們第一次push的時候,加上-u引數,Git就會把本地的master分支和遠端的master分支進行關聯起來,我們以後的push操作就不再需要加上-u引數了

如果出現類似下面內容:

Username for 'https://github.com': shiren1118

Password for 'https://[email protected]':

To https://github.com/shiren1118/iOS_code_agile.git

 ![rejected]        master -> master(non-fast-forward)

error: failed to push some refs to'https://github.com/shiren1118/iOS_code_agile.git'

hint: Updates were rejected because the tip of yourcurrent branch is behind

hint: its remote counterpart. Merge the remote changes(e.g. 'git pull')

hint: before pushing again.

hint: See the 'Note about fast-forwards' in 'git push--help' for details.

則輸入命令:git push -u origin master –f即可搞定問題

git push -u origin master

如果從在git伺服器所在主機上的其他賬戶獲取git伺服器上面檔案,則直接用

gitclone + git倉庫的路徑,即:

git clone /srv/sample.git/

7、複製分支並推送到遠端倉庫

# 從master切分出dev分支,並推送到遠端
git checkout -b develop    

# 切換到功能開發分支
git checkout -b feature-[name_of_feature]  
 
# 進行功能開發,在階段性完成之後,將程式碼合併回本地的dev分支
git checkout develop
 
# 確認分支程式碼為最新
git pull origin develop
 
# 注意,一定要新增--no-ff 標記
git merge --no-ff feature-[name_of_feature]
 
# 如果確認feature分支已經不需要了可以刪除
# git branch -d feature-[name_of_feature]
 
# 程式碼提交到遠端分支
git push origin develop

8、儲存進度的列表

git stash  # git stash save 'message...'可以新增一些註釋
git stash pop  #恢復最新的進度到工作區。git預設會把工作區和暫存區的改動都恢復到工作區,並且會刪除當前進度。
git stash pop --index  #恢復最新的進度到工作區和暫存區。(嘗試將原來暫存區的改動還恢復到暫存區)
git stash pop [email protected]{1} #恢復指定的進度到工作區。stash_id是通過git stash list命令得到的