1.簡歷github倉庫https://github.com/fulq1234/a.git
2.建立本地倉庫。我在本地新建一個文件夾gitgit,打開文件夾,右鍵選擇git bash here
git init
3.在gitgit下面新建一個文件夾adddd,
git add .
4. git倉庫遷移,git remote更改源
- git remote #不帶參數,列出已經存在的遠程分支
- git remote -v #(-v是–verbose 的簡寫,取首字母)列出詳細信息,在每一個名字後面列出其遠程url
- git remote add [shortname] [url] #添加遠程倉庫
- git remote rm [shorname] #刪除git
$ git remote -v
$ git remote add origin https://github.com/fulq1234/a.git
$ git remote -v
origin https://github.com/fulq1234/a.git (fetch)
origin https://github.com/fulq1234/a.git (push)
$ git remote add origin2 https://github.com/fulq1234/a.git
$ git remote -v
origin https://github.com/fulq1234/a.git (fetch)
origin https://github.com/fulq1234/a.git (push)
origin2 https://github.com/fulq1234/a.git (fetch)
origin2 https://github.com/fulq1234/a.git (push)
$ git remote add origin2 https://github.com/fulq1234/a.git
fatal: remote origin2 already exists.
$ git remote rm origin2
$ git remote add origin2 https://github.com/fulq1234/a.git
$ git remote -v
origin https://github.com/fulq1234/a.git (fetch)
origin https://github.com/fulq1234/a.git (push)
origin2 https://github.com/fulq1234/a.git (fetch)
origin2 https://github.com/fulq1234/a.git (push)
5. 拉取遠程倉庫的代碼。
git pull origin master
6.提交本地代碼
git add .
git commit -m ""
git push origin master
7.怎麼解決衝突。如果遠程的一個文件改變了,本地的也改變了。那麼提交代碼會衝突
$ git add .
$ git commit -m "bbb"
[master 908e9a5] bbb
1 file changed, 2 insertions(+), 1 deletion(-)
$ git push origin master
To https://github.com/fulq1234/a.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/fulq1234/a.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
方法1:
更新成最新代碼
git pull origin master
再看衝突的那個文件,本地代碼和遠程倉庫的代碼都有。需要修改後提交。
方法2:git stash
$ git stash save "1"
Saved working directory and index state On master: 1
$ git stash list
stash@{0}: On master: 1
$ git stash show
a.txt | 1 +
1 file changed, 1 insertion(+)
$ git pull origin master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/fulq1234/a
* branch master -> FETCH_HEAD
f01172d..55fa00a master -> origin/master
Updating f01172d..55fa00a
Fast-forward
a.txt | 1 +
1 file changed, 1 insertion(+)
再看衝突的那個文件,本地代碼和遠程倉庫的代碼都有。需要修改後提交。
$ git stash pop
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
方法3:把衝突文件新建分支,把文件再合併,刪除新建的那個分支
8.多個版本切換
git log 查看歷史記錄
--hard當前版本,--hard^上一個版本。
git reset --hard HEAD
版本切換:
git reset --hard c17177259e63a6f964b27be0a0b1ee6ad4882abd
9.分支
git branch iss53 #建一個分支名字叫iss53
git checkout iss53 #版本切換到分支iss53上面
如果想把master合併到iss53上。
第一步,切換到master 。
git checkout master
第二步,合併iss53
git merge iss53
10.刪除分支
git branch -d hotfix #刪除分支hotfix