git命令
Git 全局設(shè)置
git config --global user.name "焦微"
git config --global user.email "19723689@qq.com"
快速拉取
進(jìn)入新文件夾中
git init
git remote add origin [你的git地址]
git pull origin master
之后就用最后一條命令來拉取
git pull不可以,具體原因不知,可能是沒有指定倉庫
創(chuàng)建新版本庫
git clone http://45.40.195.127/h5/airwar.git
cd airwar
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
已存在的文件夾
cd existing_folder
git init
git remote add origin http://45.40.195.127/h5/airwar.git
git add .
git commit -m "Initial commit"
git push -u origin master
可能遇到的問題
- 如果提示
Authentication failed或git remote: HTTP Basic: Access denied錯誤
解決辦法:
git config --system --unset credential.helper
- 在
.git/config文件中將上傳的url地址中http://后添加上username:password@可以免輸入賬號密碼
已存在的 Git 版本庫
cd existing_repo
git remote rename origin old-origin
git remote add origin http://45.40.195.127/h5/airwar.git
git push -u origin --all
git push -u origin --tags
GitHub上管理項目
新建repository
本地目錄下,在命令行里新建一個代碼倉庫(repository)
- 里面只有一個
README.md - 初始化repository
↓命令如下:
touch README.md
git init
↓將README.md加入到緩存區(qū)
git add README.md
↓將所有改動提交到緩存注意是兩個杠
git add --a
↓提交改變,并且附上提交信息"first commit"
git commit -m "first commit"
Push
git remote add origin https://github.com/XXX(username)/YYYY(projectname).git
加上一個remote的地址,名叫origin,地址是github上的地址(Create a new repo就會有)
因為Git是分布式的,所以可以有多個remote.
↓將本地內(nèi)容push到github上的那個地址上去。
git push -u origin master
參數(shù)-u:用了參數(shù)-u之后,以后就可以直接用不帶參數(shù)的git pull從之前push到的分支來pull。
此時如果origin的master分支上有一些本地沒有的提交,push會失敗.
所以解決的辦法是, 首先設(shè)定本地master的上游分支:
git branch --set-upstream-to=origin/master
↓然后pull:
git pull --rebase
↓最后再push:
git push
分支
新建好的代碼庫有且僅有一個主分支(master),它是自動建立的。
↓可以新建分支用于開發(fā),新建一個叫develop的分支,基于master分支:
git branch develop master
↓切換到這個分支:
git checkout develop
現(xiàn)在可以在這個develop分支上做一些改動,并且提交。
注意:切換分支的時候可以發(fā)現(xiàn),在Windows中的repository文件夾中的文件內(nèi)容也會實時相應(yīng)改變,變成當(dāng)前分支的內(nèi)容。
-
push方法1:
現(xiàn)在如果想直接Push這個develop分支上的內(nèi)容到github
git push -u origin如果是新建分支第一次push,會提示:
fatal: The current branch develop has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin develop輸入這行命令,然后輸入用戶名和密碼,就push成功了。
git push --set-upstream origin develop以后的push就只需要輸入
git push origin
-
push方法2:
比如新建了一個叫dev的分支,而github網(wǎng)站上還沒有,可以直接:
git push -u origin dev這樣一個新分支就創(chuàng)建好了。
-
push方法3:
提交到github的分支有多個,提交時可以用這樣的格式:
git push -u origin local:remote比如:
git push -u origin master:master
表明將本地的master分支(冒號前)push到github的master分支(冒號后)。
如果左邊不寫為空,將會刪除遠(yuǎn)程的右邊分支。
創(chuàng)建分支的另一種方法
可以新建一個分支develop2,同時切換到這個分支
git checkout -b develop2 develop
刪除分支
↓可以查看所有的分支
git branch
↓將develop2分支刪除
git branch -d develop2
Clone
使用git clone+github地址的方法,項目默認(rèn)只有master分支。也只有master
git branch
↓要看所有的分支
git branch -a
或者是
git branch -r
↓這時候要新建一個分支,叫做dev,基于遠(yuǎn)程的dev分支:
git checkout -b dev origin/dev
加Tag
↓git tag中的兩個參數(shù),一個是標(biāo)簽名稱,另一個是希望打標(biāo)簽的點develop分支的末梢。
git tag tagname develop
合并分支
↓先轉(zhuǎn)到主分支:
git checkout master
↓然后把develop分支merge過來:
git merge --no-ff develop
參數(shù)意義:
不用參數(shù)的默認(rèn)情況下,是執(zhí)行快進(jìn)式合并。
使用參數(shù)--no-ff,會執(zhí)行正常合并,在master分支上生成一個新節(jié)點。
merge的時候如果遇到?jīng)_突,就手動解決,然后重新add,commit即可。