git安装、使用及常见报错

目录

一、安装Git

二、创建ssh key、配置git

三、Git本地仓库与远程仓库关联

四、Git本地分支和远程分支关联

五、合并分支到master上

 六、Git新建分支出现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 directory
   init       Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add        Add file contents to the index
   mv         Move or rename a file, a directory, or a symlink
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect     Use binary search to find the commit that introduced a bug
   grep       Print lines matching a pattern
   log        Show commit logs
   show       Show various types of objects
   status     Show the working tree status

grow, mark and tweak your common history
   branch     List, create, or delete branches
   checkout   Switch branches or restore working tree files
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   merge      Join two or more development histories together
   rebase     Reapply commits on top of another base tip
   tag        Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch      Download objects and refs from another repository
   pull       Fetch from and integrate with another repository or a local branch
   push       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”就可以完成安装了。

二、创建ssh key、配置git

  • 设置username和email(github每次commit都会记录他们)
git config --global user.name "abbey_lian"
git config --global user.email "abbey_lian@icloud.com"
  • 通过终端命令创建ssh key
ssh-keygen -t rsa -C "abbey_lian@icloud.com"

abbey_lian@icloud.com是我的邮件名,回车会有以下输出

由于这里我原来已经创建过,这里我选n,没有创建过的,会要求确认路径和输入密码,我们这使用默认的一路回车就行。成功的话会在~/下生成.ssh文件夹,进去,打开id_rsa.pub,复制里面的key。终端查看.ssh/id_rsa.pub文件

cat .ssh/id_rsa.pub 
  • 3、登录GitHub(默认你已经注册了GitHub账号),添加ssh key,点击Settings-New SSH key,如图

 

添加key,如图

  • 4、链接验证
ssh -T git@github.com 

第一场登陆会出现如下弹框,要求输入密码 

终端输出结果,说明已经链接成功。

三、Git本地仓库与远程仓库关联

1、前提条件

1)本地一个仓库:本地仓库已经经过git init 初始化

2)远程一个仓库:已经存在了一个远程的仓库

3)GitHub已经添加了SSH Keys

2、建立远程与本地仓库的关联

本地仓库与远程仓库关联有两种方法,一种是通过IDE(比如idea、pycharm等)关联远程仓库;一种是通过git bash 添加;

1)通过IDE添加(以idea为例)

添加远程仓库的ssh地址 

输入ssh密码 

2)通过git bash添加

使用 git remote add 在本地添加远程仓库的关联,git remote add [远程分支名] [远程仓库ssh地址]

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

使用git remote -v查看能使用的远程命令 ,如图,则表示已经关联好了。

四、Git本地分支和远程分支关联

功能命令备注
创建分支(需要先进入仓库目录)

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这样就可以自动在远程创建一个 branch_name 分支,然后本地分支会 track 该分支。后面再对该分支使用 push 和 pull 就自动同步。
如果只想与远程分支关联,而不想新创建远程分支git branch --set-upstream-to=origin/远程分支名 本地分支名如果使用这个命令时,提示没有远程分支(实际有),可以先使用git fetch命令更新本地分支缓存,然后再执行该命令

五、合并分支到master上

首先切换到master分支上

git  checkout master

如果是多人开发的话 需要把远程master上的代码pull下来

git pull origin master

然后我们把dev分支的代码合并到master上

git merge dev

然后查看状态

git status

 六、Git新建分支出现fatal: Not a valid object name: ‘master‘错误

今天在idea中将本地仓库与远程仓库关联起来后发现创建新分支功能是灰色的在git bash中用branch创建分支时,出现以下错误:

 

原因是没有提交一个文件,要先add一个文件并commit之后才会真正建立master分支,此时才可以建立其它分支。

再次打开idea创建分支,已经可以创建

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值