git相關操作

github相關概念

image.png

這里要區分git和github
git操作是屬于左半部分,github屬于上面部分
git是自己本地倉庫和自己遠程倉庫的操作
github是自己倉庫和別人倉庫(fork和被fork倉庫)之間操作
https://github.com/812865052/learngit.git是倉庫的https地址
git@github.com:812865052/learngit.git是倉庫的ssh地址

Joe和其它貢獻者已經對這個項目做了一些修改,而你將在他們的修改的基礎上,還要再做一些修改。在你開始之前,你最好"同步你的fork",以確保在最新的復制版本里工作。下面是你要做的:


image.png

git安裝

Linux:sudo apt-get install git
其他系統安裝

本地git配置

首先要在某個文件夾(空的或者已經有文件)內執行:
git init
創建了一個git倉庫
然后設置git屬性
git config --global user.name "yefeimac" #可以不加--global
git config --global user.email "your email address" #可以不加--global
git config --global core.sshCommand "ssh -i /Users/yefei/.ssh/id_rsa" #這里設置git使用哪個密鑰,非常好用
git配置文件地址: macOS and Linux: /Users/<username>/.gitconfig or ~/.gitconfig

配置git的ssh

在用ssh之前,你需要
先生成公鑰私鑰,然后將公鑰加入到github的setting中,按照教程配置~/yefei/.ssh/config
也可以參考中文教程
它的大概過程:
1.設置本地的ssh key,打開git bash,輸入命令:
ssh-keygen -t rsa -C "XXXXXX@XXXX.com" 其中雙引號中是你注冊github時用的郵箱。
一直回車,選擇默認路徑,和空密碼。最后會在默認路徑下生成.ssh文件夾,打開.ssh里面有兩個文件,打開id_rsa.pub復制里面的密鑰。
2.打開github,選擇settings
點擊ssh and gpg keys,選擇ssh keys 右邊的new ssh key。出現下面綠色框的內容,填寫標題,并將自己剛才復制的密鑰粘貼到key中。最后點擊add ssh key.
titile隨便取名字,一般讓你自己明白是哪個電腦,比如yefeiMBP
3.查看是否成功。在git bash中輸入命令:(注意是git bash,不是win自帶的cmd中輸入命令)
ssh -T git@github.com
會提示,是否continue,輸入yes。后就會看到:
Warning:Permanently added 'github.com,207.97.227.239' (RSA) to the list of known hosts.
  Hi zhangsiyao11! You've successfully authenticated, but GitHub does not provide shell access.
這樣就成功了,不用理會warning。
同理,你的這個ssh既可以加到github上,也可以加到公司的gitlab里

新建遠程倉庫流程

  • 配置GitHub倉庫
    在github上新建一個倉庫具體見下圖


    image.png

本地代碼和遠程倉庫關聯

1、首先執行類似與git remote add orgin https://github.com/812865052/newstock.git
2、執行git pull --rebase origin master (解決failed to push some refs to git的辦法)
3、git push origin master

已有github倉庫

  • 已有github倉庫,從遠程倉庫下載代碼
    執行前面的本地新倉庫配置步驟
  • 常規的本地下載、提交和push
    從遠程倉庫下載新的倉庫,可以使用
    https git clone:
    git clone https://github.com/project/repo.git

ssh git clone:
git clone git@github.com:project/repo.git

可以直接clone特定分支的代碼
git clone 分支名 地址

本地倉庫修改后,如何同步:
1、本地修改上傳:
git add 文件名
git commit -m "comment"
或者刪除文件
git rm 文件名/文件夾(前提是該文件或者文件夾在git所在目錄里面)
然后提交 git commit -m "comment"
git push origin(reposity name) master
2、本地同步遠程倉庫代碼:
git pull origin master(好像還可以用fetch,更安全,可以后面研究下)

倉庫

查看遠程倉庫
git remote -v

新建倉庫
git remote add gitname https://github.com/812865052/newstock.git

本地倉庫改名
如果不想使用origin這個名字,可以用 git remote rename origin newname改名。
提交的時候,就變成了git push newname master

若大批量 增、刪、改文件,顯然一個個添加或刪除是不可取的,以下命令可快捷操作暫存區(建議練習使用,加深對以下幾個命令的理解):

git add -A 暫存區與工作區保持一致(stages All)

git add . 暫存區新建文件及更改文件(stages new and modified, without deleted)

git add -u 暫存區刪除文件及更改文件(stages modified and deleted, without new)

單個文件的修改記錄

  1. git log filename
    可以看到fileName相關的commit記錄
  2. git log -p filenam
    可以顯示每次提交的diff
  3. 只看某次提交中的某個文件變化,可以直接加上fileName
    git show c5e69804bbd9725b5dece57f8cbece4a96b9f80b filename

diff查看

diff各個版本區別
工作目錄 vs 暫存區
git diff filename
意義:查看文件在工作目錄與暫存區的差別。如果還沒 add 進暫存區,則查看文件自身修改前后的差別。
也可查看和另一分支的區別。$ git diff branch filename

暫存區 vs Git倉庫
git diff --cached filename
意義:表示查看已經 add 進暫存區但是尚未 commit 的內容同最新一次 commit 時的內容的差異。
也可以指定倉庫版本:git diff --cached commit filename

工作目錄 vs Git倉庫
git diff commit filename
意義:查看工作目錄同Git倉庫指定 commit 的內容的差異。
commit=HEAD 時:查看工作目錄同最近一次 commit 的內容的差異。

Git倉庫 vs Git倉庫
git diff commit commit
意義:Git倉庫任意兩次 commit 之間的差別。

本地分支版本回退的方法

首先,使用 git log 命令查看提交歷史,找到你想要回退的版本的提交哈希值(commit hash)或者提交消息。

運行以下命令回退到指定版本:

git reset --hard <commit>
將 <commit> 替換為你要回退到的版本的提交哈希值或者提交消息。

例如,如果要回退到提交哈希值為 abc123 的版本,命令將如下所示:

git reset --hard abc123
注意:--hard 參數將會刪除回退版本后的所有更改,包括未提交的更改,請確保在執行此命令之前備份和保存你的工作。

執行回退命令后,Git 將會將當前分支指向指定的版本,并且丟棄指定版本后的所有提交。

自己的遠程分支版本回退的方法

在回退到指定版本后,使用 git log 命令確認你的本地倉庫已經回退到了正確的版本。

如果你之前已經將本地倉庫與遠程倉庫關聯,可以直接使用以下命令將回退推送到遠程倉庫:

git push -f origin <branch_name>
將 <branch_name> 替換為你要推送的分支的名稱。

注意:由于回退操作改變了 Git 倉庫的歷史記錄,使用 -f 參數強制推送是必要的。請謹慎使用此命令,并確保你具有足夠的權限來推送更改。

注意:本地分支回滾后,版本將落后遠程分支,必須使用強制推送覆蓋遠程分支,否則無法推送到遠程分支
如果提示GitLab: You are not allowed to force push code to a protected branch on this project.則需要在gitlab setting里面的repository里面找到protect branch,要打開允許force操作


image.png

本地同步遠程倉庫修改

想保留本地修改,同時把遠程代碼更新到本地的話,最好的命令是git pull --rebase,只用git pull會多出很多無用的commit信息。詳細信息參考git pull --rebase這篇
git pull = git fetch + git merge
git pull --rebase = git fetch + git rebase
或者是用git fetch upstream 然后git merge upstream/master 參考git pull 和 git fetch的區別?

如何同步不同名字的分支
The upstream branch of your current branch does not match
the name of your current branch. To push to the upstream branch
on the remote, use

git push gerrit HEAD:jacoco

To push to the branch of the same name on the remote, use

git push gerrit v4.7.0

分支

查看本地和遠程分支
git branch -a

查看本地分支
git branch

查看所有遠程分支
git branch -r

創建分支
git branch test

切換分支
git checkout test

創建并且切換分支
git checkout -b test
git checkout -b 本地分支名x origin/遠程分支名x
使用該方式會在本地新建分支x,并自動切換到該本地分支x。
采用此種方法建立的本地分支會和遠程分支建立映射關系。
然后直接git push test(新建的本地分支)

git fetch origin 遠程分支名x:本地分支名x
使用該方式會在本地新建分支x,但是不會自動切換到該本地分支x,需要手動checkout。
采用此種方法建立的本地分支不會和遠程分支建立映射關系。

重命名分支
在當前分支執行git branch -m new-branch-name,則把當前分支重新命名為new-branch-name

必須是相同名字的分支才能push,否則會報error: src refspec xxx does not match any / error: failed to push some refs to

把master代碼合到當前分支
1.切換到主分支
git checkout master
2.使用git pull把master代碼拉到本地
git pull upstream master
3.切換到自己的分支——>(XXX)
git checkout XXX
4.使用merge把主分支的代碼合并到自己的分支
git merge master
注意:合并時有可能會有沖突,解決完沖突才能push
5.最后push,你分支的代碼就和主分支的一樣了
git push origin xxx

刪除本地分支
git branch -d xxxxx

查看本地分支與遠程分支的映射關系
git branch -vv (注意是兩個v)


映射關系

第一行,本地的jacoco分支,沒有和遠程分支關聯
第二行,本地的master分支,和遠程的origin/master分支關聯
第三行,本地的v4.7.0分支,和遠程的gerrit/jacoco分支關聯
其中,遠程名字和ahead和behind的意思:
Ahead is the number of commits on this branch that do not exist on the base branch. Behind is the number of commits on the base branch that do not exist on this branch.

當前分支與取消和遠程分支關聯
git branch --unset-upstream

當前分支與和遠程分支關聯
git branch -u origin/addFile
或者使用命令:
git branch --set-upstream-to origin/addFile

fork別人項目后,先同步更新別人的提交,然后把自己的代碼merge到原項目

git remote -v
git remote add upstream git@github.com:xxx/xxx.git(這里還可以填https的地址) 把原項目加入到upstream
如果填錯地址,想刪除upstream,用git remote rm upstream
git fetch upstream 從原項目拉取最新代碼
git merge upstream/master 將源代碼最新代碼合到fork分支
或者直接拉推特定分支代碼到當前分支
git pull upstream Financial-User
git pull origin Financial-User
git push origin Financial-User

如果上一步出錯,提示:
error: Your local changes to the following files would be overwritten by merge:
Please, commit your changes or stash them before you can merge.
有兩種解決方法,第一, 先commit本地修改,然后合并在push到遠程自己的項目。
第二,先執行git stash,然后在執行git merge upstream/master,最后執行git stash pop,把自己的修改又展示出來。

git push 把代碼上傳到fork項目
最后就是在網頁發起pull request請求
另外一種方法就是github網頁操作,具體步驟,參考這篇文章

同步master代碼到feature分支

#1 創建功能分支 
(master) git checkout -b feature

#2 功能迭代 
(feature) git commit ...

#3 合并最新主干代碼 
(feature) git checkout master 
(master) git pull orgin master
(master) git checkout feature  ####把feature合并到master
(feature) git merge master

gitlab常見操作

git pull upstream master  本地分支拉取最新master代碼
git branch -a
git checkout xxx
git remote -v
git pull upstream Financial-User

忽略文件設置
.gitignore
文件位置就在項目根目錄

exclude
git 還提供了另一種 exclude 的方式來完成同樣的忽略
不同的是 .gitignore 這個文件本身會push到庫中去。保存的是公共的需要排除的文件。
而exclude 是自己本地忽略的設置,不會影響到其他人,也不會提交到庫中去。

exclude 文件所在位置
項目根目錄/.git/info/exclude

場景1-切到新分支看開發代碼

git fetch -這里會把所有新分支拉下來 否則在 git branch -r看不到開發新建的分支
git check out 分支名 就自動切到新分支了 代碼也是分支代碼
如果報錯

error: The following untracked working tree files would be overwritten by checkout:
        remit-manager.iml
Please move or remove them before you switch branches.
Aborting

就執行git clean -df remit-manager.iml
然后再執行checkout

查看沖突文件列表
git diff --name-only --diff-filter=U

【Git】pull 分支報錯 fatal: Need to specify how to reconcile divergent branches

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容