初始化倉庫并推送到遠端
- 初始化
echo "# MyProject" >> README.md
git init
- 然后可以愉快的進行開發(fā)了。。。
add .
commit 'lalala'
- 進行推送(如果本身已經是 git 倉庫了,直接走到第 3 步驟)
# 將本地倉庫關聯(lián)一個遠程庫
git remote add origin git@github.com:someone/MyProject.git
# 加上了 -u 參數(shù),Git 不但會把本地的master分支內容推送的遠程新的master分支,還會把本地的 master 分支和遠程的 master 分支關聯(lián)起來,在以后的推送或者拉取時就可以簡化命令
git push -u origin master
Git 代碼庫遷移
# 從原地址克隆一份裸版本庫
git clone --bare https://git.example.com/your/project.git your_path
cd your_path
git remote set-url origin https://codeup.aliyun.com/5eacd74338076f00011bc59e/hexo-src.git
git push --mirror
或者不 set-url origin, 而是最后兩條命令變成一條命令, 進行推送
git push --mirror https://codeup.aliyun.com/5eacd74338076f00011bc59e/hexo-src.git
其中 git clone --bare 創(chuàng)建的克隆版本庫都不包含工作區(qū),直接就是版本庫的內容,這樣的版本庫稱為裸版本庫。
其中 git clone --mirror 遠程跟蹤設置,所以如果你運行 git 遠程更新所有參考將被覆蓋從原點,如果你剛剛刪除鏡像并重新登記。正如文檔最初說的,它是一面鏡子。它應該是一個功能相同的副本,可以與原件互換。
git push --mirror
Instead of naming each ref to push, specifies that all refs under
refs/ (which includes but is not limited to refs/heads/,
refs/remotes/, and refs/tags/) be mirrored to the remote
repository. Newly created local refs will be pushed to the remote
end, locally updated refs will be force updated on the remote end,
and deleted refs will be removed from the remote end. This is the
default if the configuration option remote.<remote>.mirror is set.
簡而言之 -- mirror 強制推送 all refs under refs/ 下的所有. 保持絕對的同步.
撤銷修改
場景1:當你改亂了工作區(qū)某個文件的內容,還未提交到工作區(qū), 且想直接丟棄工作區(qū)的修改時,用命令git checkout -- file。用干凈暫存區(qū)內容(同版本庫)去覆蓋工作區(qū)的內容。
場景2:當你不但改亂了工作區(qū)某個文件的內容,還添加到了暫存區(qū)時,想丟棄修改,分兩步,第一步用命令git reset HEAD file,就回到了場景1,第二步按場景1操作。
reset 和 revert
本地分支可以 reset, 回退分支
revert 回歸分支會生成新的提交記錄, 遠程分支建議如此操作.
如何修改老舊的commit信息
git rebase -i commit-id
commit-id 指的是要修改的節(jié)點的父節(jié)點.
使用 r 命令
如何多個 commit 整理合并成
git rebase -i commit-id
commit-id 指的是要修改的節(jié)點的父節(jié)點.
然后使用 s 標記進行精簡。
git 移除已提到到版本庫的文件
請使用 git rm --cached 命令
發(fā)現(xiàn) .idea 文件夾下的文件還有變更被提交,這是因為在使用 gitignore 之前,此文件就以及被跟蹤了,這樣的話需要移除跟蹤,如下命令:
移除單個文件
git rm --cached --force ydq-api/ydq-api.iml
移除指定文件夾即文件夾下所有文件:
git rm --cached --force -r .idea/
git 如何修改遠程 URL
# 先查看remote的名字
git remote -v
origin http://aaa.bbb.ccc.ddd/be/preser-image.git (fetch)
origin http://aaa.bbb.ccc.ddd/be/preser-image.git (push)
# 使用`git remote set-url` 修改 remote_url地址, 這里假設你的remote是origin
git remote set-url origin http://117.50.94.8/be/preser-image.git
刪除遠程分支
方法一
git push 主機名 :遠程分支
如果一次性刪除多個,可以寫多個
git push 主機名 :遠程分支名 :遠程分支名 :遠程分支名
方法二
另外一個刪除分支的命令是
git push 主機名 --delete 遠程分支名
方法三
git branch -r -d origin/branch-name
自動補全
在輸入 Git 命令的時候可以敲兩次跳格鍵(Tab),就會看到列出所有匹配的可用命令建議
設置 http 代理
# 開啟 http 代理
git config --global http.proxy http://10.5.3.9:80
# 開啟 https 代理
git config --global https.proxy https://10.5.3.9:80
# 取消 對應 http 代理
git config --global --unset http.proxy
# 取消 對應 https 代理
git config --global --unset https.proxy
參考
很好的參考文章:
https://fle.github.io/git-tip-keep-your-branch-clean-with-fixup-and-autosquash.html