饥人谷技术博客

3-git本地仓库及远程仓库GitHub

2020-12-26  本文已影响0人  茜Akane

Get Started

git 六行配置

配置并建立一个git本地仓库。

λ git config --global user.name 名字

λ git config --global user.email 邮箱

λ git config --global push.default simple

λ git config --global core.quotepath false

λ git config --global core.editor "code --wait"

λ git config --global core.autocrlf input

λ git config --global --list
5user.name=名字
user.email=邮箱
push.default=simple
core.quotepath=false
core.editor=code --wait
core.autocrlf=input

本地仓库的操作命令

git就是一个命令。
git是一个分布式版本控制系统。与之前使用过的集中式版本控制svn不同的是git是把代码仓库完整地镜像下来,包括完整的历史记录。
git是对文件的快照,而不是差分。
在用过之后就会发现git的分支更加简便,灵活。

之前没用过git,有点好奇,然后再网上发现这里的git说明十分详细:
https://git-scm.com/book/zh/v2

git指令

解决冲突的办法

远程仓库GitHub

SSH key验证身份

代码需要储存在云端,而GitHub可以储存你的代码。那么,如何将本地的代码连接到GitHub呢,首先要验证身份。

  1. 先要有一个GitHb账号+密码
  2. 然后运行申请公钥的命令,过程之中一直回车就好了
    ssh-keygen -t ed25519 -C "your_email@example.com"
  3. 查看公钥,并粘贴到GitHub上(settings->SSH keys)
    cat id_xxx.pub
  4. 输入命令测试是否配对成功,问是否要继续连接的时候yes
    ssh -T git@github.com
    提示设置成功就可以上传代码啦

上传代码

  1. 新建一个GitHub Repository,复制SSH地址和下面的代码
  2. 在本地添加远程仓库地址。origin是远程仓库的默认名字,可以换但是不建议。
    建议不要使用https://地址,每次都需要密码,麻烦。
    git remote add origin git@github.com:仓库地址
  3. 推送本地master分支到远程origin的master分支,如果过程中提示你应该git pull … ,就git pull。(如果远程分支没更新过,才可以省略)
    git push -u origin master
    -u origin master的意思是设置上游分支,之后就不用再设置上有分支,直接git pull;git push。
  4. 网站上给的命令是这样的,但是直接用3.的命令也可以
git remote add origin git@github.com:username/repo-name.git
git branch -M main
git push -u origin main

※ 关于分支的提交,pull哪个分支就上传哪个,在分支上只有commit后的文件才可以上传,add和modify状态的文件不会上传;还有.ignore文件里添加的文件不会上传。
※ modify文件需要再add之后commit。
※ 上传一个新的分支,就要使用-u origin 分支名

上传其他分支:
一、x:x左边是源头,右边是目标。从本地x到远程x
git push origin x:x
二、切换到目标分支,然后上传
git checkout x
git push -u origin x

🔶总结:解决冲突
实际操作了一下,在GitHub上修改了一个文件,然后我本地修改了代码,在不知道远程仓库中这个文件已经被修改的情况下git push了。

λ git push
    Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
    To github.com:AkaneTang/git-demo-1.git
     ! [rejected]        master -> master (fetch first)
    error: failed to push some refs to 'git@github.com:AkaneTang/git-demo-1.git'
    hint: Updates were rejected because the remote contains work that you do
    hint: not have locally. This is usually caused by another repository pushing
    hint: to the same ref. You may want to first integrate the remote changes
    hint: (e.g., 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
可以看到别人修改的文件和我的冲突了,按照提示git pull,拉取远程代码。
λ git pull
    remote: Enumerating objects: 5, done.
    remote: Counting objects: 100% (5/5), done.
    remote: Compressing objects: 100% (3/3), done.
    remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0   
    Unpacking objects: 100% (3/3), done.
    From github.com:AkaneTang/git-demo-1
       3054bd5..f454902  master     -> origin/master
    Auto-merging index.html
    CONFLICT (content): Merge conflict in index.html
    Automatic merge failed; fix conflicts and then commit the result.

自动弹出merge该文件,选择好需要保留的地方保存。再次上传需要git add+git commit然后再git push。
在此过程中可以通过git status来查看文件状态。modify文件时红色的,add之后变绿,就可以commit了。

如何下载别人的代码

git高级操作

使用bash alias简化命令

    echo 'alias ga="git add"'>> ~/.bashrc
    echo 'alias gc="git commit -v"'>> ~/.bashrc
    echo 'alias gl="git pull"'>> ~/.bashrc
    echo 'alias gp="git push"'>> ~/.bashrc
    echo 'alias gco="git checkout"'>> ~/.bashrc
    echo 'alias gst="git status -sb"'>> ~/.bashrc
上一篇 下一篇

猜你喜欢

热点阅读