Git使用及安裝

Git常用命令

  1. git init #使用當前目錄作爲Git倉庫
    git init newrepo #使用指定目錄作爲Git倉庫
  2. git clone git.example.com:repositories/hello.git #獲取
  3. git checkout -b branchname #創建並切換到分支
    git checkout -f $filename #還原被修改文件
    git checkout -f && git clean -df #清除當前所有修改
  4. git branch -D branchname #刪除分支
    git branch -a #查看所有分支
  5. git add/rm #從本地倉庫增刪,結果將會保存到本機的緩存裏面
    git add -A #提交全部修改
  6. git status #查看狀態
  7. git commit -m “註釋” #提交,把本機緩存中的內容提交到本機的 HEAD 裏面
  8. git pull origin master #從遠程倉庫中下載新的改動
    git push origin master #把本地的 commit(提交) push 到遠程服務器上
  9. git log #查看提交信息
    git log -n -2 #查看最近兩次的提交
  10. git whatchhange #查看每個提交修改的文件
  11. git reset HEAD read.md #還原已經提交的文件
  12. git remote -v #查看下載地址
  13. git commit --amend -m #“修改 提交 說明” 修改最後一次提交註釋
  14. git reset --hard “commit ID號” #回退到指定版本
  15. 標籤操作
  • git tag -a 0.1.3 -m “Release version 0.1.3” #詳解:git tag 是命令, -a 0.1.3是增加 名爲0.1.3的標籤, -m 後面跟着的是標籤的註釋
  • git push origin --tags #提交標籤到遠程服務器上 –tags參數表示提交所有tag至服務器端,普通的git push origin master操作不會推送標籤到服務器端。
  • git tag -d 0.1.3 #刪除標籤的命令
  • git push origin :refs/tags/0.1.3 #刪除遠端服務器的標籤
  • git show v1.0.0 #查看 tag 詳情
  1. git cherry-pick #合併某次提交到另一個分支(必須同一個倉庫)
  2. git fetch <遠程主機名> #將某個遠程主機的更新,全部取回本地
  3. git rebase #從父線上拉下來最新的代碼到自己的分支
  4. git merge branch_name #從子分支的修改合併到父線上
  5. git submodule #開發過程中,經常會有一些通用的部分希望抽取出來做成一個公共庫來提供給別的工程來使用,而此命令可以管理公共代碼庫的版本
  • git submodule add 倉庫地址 路徑 #爲當前工程添
    其中,倉庫地址是指子模塊倉庫地址,路徑指將子模塊放置在當前工程下的路徑。注意:路徑不能以 / 結尾(會造成修改不生效)、不能是現有工程已有的目錄(不能順利 Clone)
  • git rm –cached #刪除
    submodule的刪除稍微麻煩點:首先,要在“.gitmodules”文件中刪除相應配置信息。然後,執行“git rm –cached ”命令將子模塊所在的文件從git中刪除
  • git submodule update --init --recursive #下載的工程帶有submodule
    當使用git clone下來的工程中帶有submodule時,初始的時候,submodule的內容並不會自動下載下來的,此時,只需執行此命令
  1. 直接Clone連接時,設置記住密碼
  • git config –global credential.helper cache #設置記住密碼(默認15分鐘)
  • git config credential.helper ‘cache –timeout=3600’ #自己設置時間
  • git config –global credential.helper store #長期記住密碼
  1. 忽略提交某個指定的文件(不從版本庫中刪除)
  • git update-index --assume-unchanged test.c (忽略提交)
  • git update-index --no-assume-unchanged test.c (取消忽略指令)
  1. 待續 …

Git安裝

  1. 安裝git
    ubuntu: sudo apt-get install git
    windows: 下載git安裝

  2. 打開終端
    ubuntu: 直接打開終端
    windows: 在git工作目錄下—右鍵—Git Base Here

  3. 配置本機git的兩個重要信息,user.name和user.email,中終端輸入如下命令即可設置
    git config --global user.name “hanbo” #用戶名
    git config --global user.email “[email protected]” #用戶郵箱
    git config --list #查看是否設置成功。

  4. 配置ssh key上傳代碼時使用這個 sshkey 來確認是否有上傳權限
    ssh-keygen -t rsa -C “[email protected]” #創建ssh
    會生成.ssh目錄中兩個文件id_rsa和id_rsa.pub。id_rsa:私鑰;id_rsa.pub:公鑰,打開id_rsa.pub賦值裏面的內容;
    ubuntu路徑/home/username/.ssh/id_rsa.pub
    windows路徑C:\Documents and Settings\Administrator.ssh\id_rsa.pub

  5. 進入你的github,進入Profile Settings->SSH keys->Add SSH Keys,然後在Key那欄下面將公鑰拷貝的內容粘貼進去,最後點擊 Add key按鈕添加。

  6. 驗證:ssh -T [email protected]
    出現 Hi xxx! You’ve successfully authenticated, but GitHub does not provide shell access. 說明配置成功

  7. 如果使用局域網域名,則需要添加本地域名解析
    ubuntu: vi /etc/hosts
    windows: 管理員打開C:\Windows\System32\drivers\etc\hosts

    在最後添加: 192.168.1.11 git.example.com #IP 域名

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章