Git的一些操作

Git的一些操作

流程例子

  • 將本地代碼上傳到github的操作流程

新建說明文件

touch README.md

在當(dāng)前項(xiàng)目目錄中生成本地git管理,并建立一個(gè)隱藏的.git目錄

git init

添加當(dāng)前目錄中的所有文件到索引

git add .

提交到本地源碼庫,并附加描述

git commit -m 'commit description'

添加到遠(yuǎn)程目錄

git remote add origin https://github.com/username/
project.git

把本地源碼庫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)目到本地倉庫

git clone htts:github.com/username/project.git
  • 同步github最新代碼到本地倉庫

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è)置要上傳的倉庫目錄
git remote add origin https://github.com/username/project.git
  • 如果這句報(bào)錯(cuò):

fatal: remote origin already exists

  • 則輸入下面這句:
git remote rm origin
  • 上傳到倉庫
git push -u origin master
  • 刷新后如果只有README.md文件,進(jìn)行以下操作:(這個(gè)我主要在上傳到開源中國的碼云的時(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倉庫的代碼到本地(注意,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的倉庫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 .

2016.8.23 初次接觸github做的一些隨筆

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 1.git的安裝 1.1 在Windows上安裝Git msysgit是Windows版的Git,從https:/...
    落魂灬閱讀 12,713評(píng)論 4 54
  • 無論從產(chǎn)生青銅器的遠(yuǎn)古,還是退出歷史舞臺(tái)的春秋戰(zhàn)國后期,青銅器它作為一種器具,是社會(huì)地位的分現(xiàn),是當(dāng)時(shí)社會(huì)科技發(fā)展...
    阿建w閱讀 225評(píng)論 5 2
  • 題目來源: 牛客網(wǎng)--程序員面試金典 題目描述 請(qǐng)編寫一個(gè)函數(shù),檢查鏈表是否為回文。給定一個(gè)鏈表ListNode*...
    努力努力再努力_姜姜閱讀 243評(píng)論 0 0