日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

git安装、使用及常见报错

發(fā)布時間:2024/4/15 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 git安装、使用及常见报错 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

目錄

一、安裝Git

二、創(chuàng)建ssh key、配置git

三、Git本地倉庫與遠程倉庫關聯(lián)

四、Git本地分支和遠程分支關聯(lián)

五、合并分支到master上

?六、Git新建分支出現(xiàn)fatal: Not a valid object name: ‘master‘錯誤


一、安裝Git

MAC 上安裝Git主要有兩種方式:通過homebrew安裝(這種方法比較好)或者通過xcode安裝

首先查看電腦是否安裝Git,終端輸入:

git

安裝過則會輸出:

WMBdeMacBook-Pro:~ WENBO$ git usage: git [--version] [--help] [-C <path>] [-c name=value][--exec-path[=<path>]] [--html-path] [--man-path] [--info-path][-p | --paginate | --no-pager] [--no-replace-objects] [--bare][--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]<command> [<args>]These are common Git commands used in various situations:start a working area (see also: git help tutorial)clone Clone a repository into a new directoryinit Create an empty Git repository or reinitialize an existing onework on the current change (see also: git help everyday)add Add file contents to the indexmv Move or rename a file, a directory, or a symlinkreset Reset current HEAD to the specified staterm Remove files from the working tree and from the indexexamine the history and state (see also: git help revisions)bisect Use binary search to find the commit that introduced a buggrep Print lines matching a patternlog Show commit logsshow Show various types of objectsstatus Show the working tree statusgrow, mark and tweak your common historybranch List, create, or delete branchescheckout Switch branches or restore working tree filescommit Record changes to the repositorydiff Show changes between commits, commit and working tree, etcmerge Join two or more development histories togetherrebase Reapply commits on top of another base tiptag Create, list, delete or verify a tag object signed with GPGcollaborate (see also: git help workflows)fetch Download objects and refs from another repositorypull Fetch from and integrate with another repository or a local branchpush Update remote refs along with associated objects'git help -a' and 'git help -g' list available subcommands and some concept guides. See 'git help <command>' or 'git help <concept>' to read about a specific subcommand or concept.

1、通過homebrew安裝Git

  • 未安裝homebrew,需安裝homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • 安裝git
brew install git

2、通過Xcode安裝

直接從AppStore安裝Xcode,Xcode集成了Git,不過默認沒有安裝,你需要運行Xcode,選擇菜單“Xcode”->“Preferences”,在彈出窗口中找到“Downloads”,選擇“Command Line Tools”,點“Install”就可以完成安裝了。

二、創(chuàng)建ssh key、配置git

  • 設置username和email(github每次commit都會記錄他們)
git config --global user.name "abbey_lian" git config --global user.email "abbey_lian@icloud.com"
  • 通過終端命令創(chuàng)建ssh key
ssh-keygen -t rsa -C "abbey_lian@icloud.com"

abbey_lian@icloud.com是我的郵件名,回車會有以下輸出

由于這里我原來已經(jīng)創(chuàng)建過,這里我選n,沒有創(chuàng)建過的,會要求確認路徑和輸入密碼,我們這使用默認的一路回車就行。成功的話會在~/下生成.ssh文件夾,進去,打開id_rsa.pub,復制里面的key。終端查看.ssh/id_rsa.pub文件

cat .ssh/id_rsa.pub
  • 3、登錄GitHub(默認你已經(jīng)注冊了GitHub賬號),添加ssh key,點擊Settings-New SSH key,如圖

?

添加key,如圖

  • 4、鏈接驗證
ssh -T git@github.com

第一場登陸會出現(xiàn)如下彈框,要求輸入密碼?

終端輸出結(jié)果,說明已經(jīng)鏈接成功。

三、Git本地倉庫與遠程倉庫關聯(lián)

1、前提條件

1)本地一個倉庫:本地倉庫已經(jīng)經(jīng)過git init 初始化

2)遠程一個倉庫:已經(jīng)存在了一個遠程的倉庫

3)GitHub已經(jīng)添加了SSH Keys

2、建立遠程與本地倉庫的關聯(lián)

本地倉庫與遠程倉庫關聯(lián)有兩種方法,一種是通過IDE(比如idea、pycharm等)關聯(lián)遠程倉庫;一種是通過git bash 添加;

1)通過IDE添加(以idea為例)

添加遠程倉庫的ssh地址?

輸入ssh密碼?

2)通過git bash添加

使用 git remote add 在本地添加遠程倉庫的關聯(lián),git remote add [遠程分支名] [遠程倉庫ssh地址]

git remote add origin git@github.com:Barry-Manilow/myblog.git

使用git remote -v查看能使用的遠程命令 ,如圖,則表示已經(jīng)關聯(lián)好了。

四、Git本地分支和遠程分支關聯(lián)

功能命令備注
創(chuàng)建分支(需要先進入倉庫目錄)

git branch develop

?
查看本地分支git branch

注:名稱前面加* 號的是當前的分支

查看遠程分支git branch -a
切換分支git checkout branch_name?
刪除本地分支git branch -d branch_name?
刪除遠程分支git branch -r -d origin/branch-name ???
如果遠程新建了一個分支,本地沒有該分支git checkout?--track?origin/branch_name這時本地會新建一個分支名叫?branch_name?,會自動跟蹤遠程的同名分支?branch_name
如果本地新建了一個分支,遠程沒有該分支git push --set-upstream origin branch_name這樣就可以自動在遠程創(chuàng)建一個?branch_name?分支,然后本地分支會?track?該分支。后面再對該分支使用 push 和 pull 就自動同步。
如果只想與遠程分支關聯(lián),而不想新創(chuàng)建遠程分支git branch --set-upstream-to=origin/遠程分支名 本地分支名如果使用這個命令時,提示沒有遠程分支(實際有),可以先使用git fetch命令更新本地分支緩存,然后再執(zhí)行該命令

五、合并分支到master上

首先切換到master分支上

git checkout master

如果是多人開發(fā)的話 需要把遠程master上的代碼pull下來

git pull origin master

然后我們把dev分支的代碼合并到master上

git merge dev

然后查看狀態(tài)

git status

?六、Git新建分支出現(xiàn)fatal: Not a valid object name: ‘master‘錯誤

今天在idea中將本地倉庫與遠程倉庫關聯(lián)起來后發(fā)現(xiàn)創(chuàng)建新分支功能是灰色的在git bash中用branch創(chuàng)建分支時,出現(xiàn)以下錯誤:

?

原因是沒有提交一個文件,要先add一個文件并commit之后才會真正建立master分支,此時才可以建立其它分支。

再次打開idea創(chuàng)建分支,已經(jīng)可以創(chuàng)建

總結(jié)

以上是生活随笔為你收集整理的git安装、使用及常见报错的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。