git基本用法備忘以及idea,pycharm連接github

1.GIT基本使用方法備忘(摘自廖雪峰)

一、幾個專用名詞的譯名如下

Workspace:工作區(qū)
Index / Stage:暫存區(qū)
Repository:倉庫區(qū)(或本地倉庫)
Remote:遠(yuǎn)程倉庫
給電腦授權(quán)
ssh-keygen -t rsa -b 4096 -C my_email_address
vim /c/Users/wangyao/.ssh/id_rsa.pub

二、新建代碼庫

在當(dāng)前目錄新建一個Git代碼庫
$ git init

新建一個目錄,將其初始化為Git代碼庫
$ git init [project-name]

下載一個項目和它的整個代碼歷史
$ git clone [url]

三、配置

Git的設(shè)置文件為.gitconfig,它可以在用戶主目錄下(全局配置),也可以在項目目錄下(項目配置)。

顯示當(dāng)前的Git配置
$ git config --list

編輯Git配置文件
$ git config -e [--global]

設(shè)置提交代碼時的用戶信息

$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"

四、增加/刪除文件

添加指定文件到暫存區(qū)
$ git add [file1] [file2] ...

添加指定目錄到暫存區(qū),包括子目錄
$ git add [dir]

添加當(dāng)前目錄的所有文件到暫存區(qū)
$ git add .

刪除工作區(qū)文件,并且將這次刪除放入暫存區(qū)
$ git rm [file1] [file2] ...

停止追蹤指定文件,但該文件會保留在工作區(qū)
$ git rm --cached [file]

改名文件,并且將這個改名放入暫存區(qū)
$ git mv [file-original] [file-renamed]

五、代碼提交

提交暫存區(qū)到倉庫區(qū)
$ git commit -m [message]

提交暫存區(qū)的指定文件到倉庫區(qū)
$ git commit [file1] [file2] ... -m [message]

提交工作區(qū)自上次commit之后的變化,直接到倉庫區(qū)
$ git commit -a

提交時顯示所有diff信息
$ git commit -v

使用一次新的commit,替代上一次提交
如果代碼沒有任何新變化,則用來改寫上一次commit的提交信息
$ git commit --amend -m [message]

重做上一次commit,并包括指定文件的新變化
$ git commit --amend [file1] [file2] ...

六、分支

列出所有本地分支
$ git branch

列出所有遠(yuǎn)程分支
$ git branch -r

列出所有本地分支和遠(yuǎn)程分支
$ git branch -a

新建一個分支,但依然停留在當(dāng)前分支
$ git branch [branch-name]

新建一個分支,并切換到該分支
$ git checkout -b [branch]

新建一個分支,指向指定commit
$ git branch [branch] [commit]

新建一個分支,與指定的遠(yuǎn)程分支建立追蹤關(guān)系
$ git branch --track [branch] [remote-branch]

切換到指定分支,并更新工作區(qū)
$ git checkout [branch-name]

建立追蹤關(guān)系,在現(xiàn)有分支與指定的遠(yuǎn)程分支之間
$ git branch --set-upstream [branch] [remote-branch]

合并指定分支到當(dāng)前分支
$ git merge [branch]

選擇一個commit,合并進(jìn)當(dāng)前分支
$ git cherry-pick [commit]

刪除分支
$ git branch -d [branch-name]

刪除遠(yuǎn)程分支
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

七、標(biāo)簽

列出所有tag
$ git tag

新建一個tag在當(dāng)前commit
$ git tag [tag]

新建一個tag在指定commit
$ git tag [tag] [commit]

查看tag信息
$ git show [tag]

提交指定tag
$ git push [remote] [tag]

提交所有tag
$ git push [remote] --tags

新建一個分支,指向某個tag
$ git checkout -b [branch] [tag]

八、查看信息

顯示有變更的文件
$ git status

顯示當(dāng)前分支的版本歷史
$ git log

顯示commit歷史,以及每次commit發(fā)生變更的文件
$ git log --stat

顯示某個文件的版本歷史,包括文件改名
$ git log --follow [file]
$ git whatchanged [file]

顯示指定文件相關(guān)的每一次diff
$ git log -p [file]

顯示指定文件是什么人在什么時間修改過
$ git blame [file]

顯示暫存區(qū)和工作區(qū)的差異
$ git diff

顯示暫存區(qū)和上一個commit的差異
$ git diff --cached [file]

顯示工作區(qū)與當(dāng)前分支最新commit之間的差異
$ git diff HEAD

顯示兩次提交之間的差異
$ git diff [first-branch]...[second-branch]

顯示某次提交的元數(shù)據(jù)和內(nèi)容變化
$ git show [commit]

顯示某次提交發(fā)生變化的文件
$ git show --name-only [commit]

顯示某次提交時,某個文件的內(nèi)容
$ git show [commit]:[filename]

顯示當(dāng)前分支的最近幾次提交
$ git reflog

九、遠(yuǎn)程同步

下載遠(yuǎn)程倉庫的所有變動
$ git fetch [remote]

顯示所有遠(yuǎn)程倉庫
$ git remote -v

顯示某個遠(yuǎn)程倉庫的信息
$ git remote show [remote]

增加一個新的遠(yuǎn)程倉庫,并命名
$ git remote add [shortname] [url]

取回遠(yuǎn)程倉庫的變化,并與本地分支合并
$ git pull [remote] [branch]

上傳本地指定分支到遠(yuǎn)程倉庫
$ git push [remote] [branch]

強(qiáng)行推送當(dāng)前分支到遠(yuǎn)程倉庫,即使有沖突
$ git push [remote] --force

推送所有分支到遠(yuǎn)程倉庫
$ git push [remote] --all

刪除已指定的遠(yuǎn)程倉庫
git remote rm origin

十、撤銷

恢復(fù)暫存區(qū)的指定文件到工作區(qū)
$ git checkout [file]

恢復(fù)某個commit的指定文件到工作區(qū)
$ git checkout [commit] [file]

恢復(fù)上一個commit的所有文件到工作區(qū)
$ git checkout .

重置暫存區(qū)的指定文件,與上一次commit保持一致,但工作區(qū)不變
$ git reset [file]

重置暫存區(qū)與工作區(qū),與上一次commit保持一致
$ git reset --hard

重置當(dāng)前分支的指針為指定commit,同時重置暫存區(qū),但工作區(qū)不變
$ git reset [commit]

重置當(dāng)前分支的HEAD為指定commit,同時重置暫存區(qū)和工作區(qū),與指定commit一致
$ git reset --hard [commit]

重置當(dāng)前HEAD為指定commit,但保持暫存區(qū)和工作區(qū)不變
$ git reset --keep [commit]

新建一個commit,用來撤銷指定commit
后者的所有變化都將被前者抵消,并且應(yīng)用到當(dāng)前分支
$ git revert [commit]

十一、其他

生成一個可供發(fā)布的壓縮包
$ git archive

十二、一般流程:

cd 到準(zhǔn)備放置代碼的文件夾下
git clone [url] 把代碼弄到本地
cd 到clone下的文件夾中
git status 查看狀態(tài)
git log 顯示當(dāng)前分支的版本歷史

修改代碼以后
git status 應(yīng)該能看到 紅色的modified: XXXX.py 被修改的代碼
git add . add所有內(nèi)容
git status 應(yīng)該能看到,綠色的modified: XXXX.py
git commit -m "修改的內(nèi)容" 用于注釋做了什么修改
git push origin master 或者 git push origin develop 正式推上線

回退

如果不小心改了一些不需要改的地方,還未add/commit提交,想要把代碼恢復(fù)到之前的狀態(tài)。
git checkout . #撤銷全部文件的修改
git checkout [filename] # 撤銷對指定文件的修改

使用 git reset 回退項目版本
可以回退到任意已經(jīng)提交過的版本。已 add / commit 但未 push 的文件也適用。
git reset --hard [commit-hashcode]
一般用法是先用 git log 查看具體commit的哈希值,然后 reset 到那個版本。

合并develop分支到master

1.查看本地和遠(yuǎn)程分支

$ git branch -a

  • developer
    master
    remotes/origin/developer
    remotes/origin/master

2.切換到本的developer分支

$ git checkout -b developer origin/developer

如果developer分支已經(jīng)存在,執(zhí)行下面這步

$ git checkout developer

如查當(dāng)前使用的就是developer分支,則這步不用執(zhí)行。

3.把遠(yuǎn)程的developer分支拉取到本地,保證是最新的developer分支

$ git pull

4.切換到master分支

$ git checkout master

5.確保存master分支也是最新的

$ git pull

6.執(zhí)行合并的關(guān)鍵代碼,此時執(zhí)行結(jié)果時將本地的developer合并到本地master分支

$ git merge developer

7.將合并的本地master分支推送到遠(yuǎn)程master

$ git push origin master

偶爾出現(xiàn)異??梢杂眠h(yuǎn)程倉庫強(qiáng)制覆蓋本地
git fetch --all && git reset --hard origin/master && git pull

十三、其他需要注意的:

如果其他人某些地方做了修改,
git pull 把其他人修改過的部分更新一下
git branch -a 先看一下有哪些分支
然后選擇分支push 比如更新到develop分支:git push origin develop
push的時候如果代碼有更新,直接git pull更新代碼再push

git commit -a 相當(dāng)于 git add . 加上git commit兩步操作,通常還是建議分開操作。

上傳代碼如果報錯
error: src refspec master does not match any.
error: failed to push some refs to
git commit -m "xxx" 時候需要用戶的郵箱和用戶名,而新裝的git還沒有提交用戶郵箱和用戶名就會報錯。

*** Please tell me who you are.

Run

 git config --global user.email "you@example.com"

 git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

git pull時出現(xiàn)Automatic merge failed; fix conflicts and then commit the result.
說明和別人同時修改了某行,起了沖突。
可以執(zhí)行
git reset --hard HEAD
廢棄這次修改。
然后用git checkout退回到?jīng)]改過的樣子。
git pull。

有時候git pull時出現(xiàn)Your local changes to the following files would be overwritten by merge.
可能是有人在本地直接修改了代碼文件(沒有通過git提交),
最簡單的方法,如果本地修改不重要
放棄本地修改,直接覆蓋

git reset --hard
git pull

解決方法二:git checkout還原然后再pull(就是覆蓋更新的意思)
解決方法三:先add 再commit 最后pull 就會在本地合并你的代碼,最后檢查沒問題再push

提示:您有偏離的分支,需要指定如何調(diào)和它們。您可以在執(zhí)行下一次
提示:pull 操作之前執(zhí)行下面一條命令來抑制本消息:
提示:
提示:  git config pull.rebase false  # 合并(缺省策略)
提示:  git config pull.rebase true   # 變基
提示:  git config pull.ff only       # 僅快進(jìn)
提示:
提示:您可以將 "git config" 替換為 "git config --global" 以便為所有倉庫設(shè)置
提示:缺省的配置項。您也可以在每次執(zhí)行 pull 命令時添加 --rebase、--no-rebase,
提示:或者 --ff-only 參數(shù)覆蓋缺省設(shè)置。

2.IDEA連接github

2.1 IDEA連接github
https://www.cnblogs.com/jinjiyese153/p/6796668.html
2.2 IDEA導(dǎo)入項目
File->New


復(fù)制http地址



填入地址導(dǎo)入項目


3.pycharm 連接github

https://www.cnblogs.com/lidyan/p/6538877.html

References:

git 中文文檔: https://docs.gitlab.com.cn/
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432712108300322c61f256c74803b43bfd65c6f8d0d0000

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容