【MAC】mac上Git和GitHub的使用
一、查看mac上是否安装git
首先查看电脑是否安装Git,终端输入:
git
如果安装则出现:
usage: git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
[--super-prefix=<path>] [--config-env=<name>=<envvar>]
<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
restore Restore working tree files
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
diff Show changes between commits, commit and working tree, etc
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
commit Record changes to the repository
merge Join two or more development histories together
rebase Reapply commits on top of another base tip
reset Reset current HEAD to the specified state
switch Switch branches
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.
See 'git help git' for an overview of the system.
如果未安装,可以通过homebrew或者是Xcode安装,具体步骤可以查找相应教程
二、创建ssh key、配置git
- 1.配置git config
git config --global user.name "your user name xxx"
git config --global user.email "xxx@qq.com"
-
- 通过终端命令创建ssh key
ssh-keygen -t rsa -C "xxx@qq.com"
输出是:
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/yangxinyu/.ssh/id_rsa):
/Users/yangxinyu/.ssh/id_rsa already exists.
Overwrite (y/n)? n
因为之前已经生成过了,所以这里我选了n,如果没有生成过,会提示确认路径输入密码。
此时生成的密钥已经保存在.ssh/id_rsa这个文件里了。
- 3.使用cat 命令打开id_rsa.pub文件,复制相应的密钥
cat .ssh/id_rsa.pub
- 4.登陆GitHub 添加SSH Key
点击setting
选择SSH and GPG keys
点击 New SSH key
随便填入 title,并将相应的密钥复制进去
- 5.链接验证
ssh -T git@github.com
初始验证会出现:
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])?
可以和这个秘钥指纹进行验证:
输入yes后,出现:
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
Hi yangxinyu-april! You've successfully authenticated, but GitHub does not provide shell access.
说明已经链接成功。
三、提交本地项目到GitHub
-
创建一个新的仓库
-
输入仓库的相关信息
-
成功创建仓库
-
复制ssh地址,可以clone工程到本地,在本地可以添加修改代码后再推送到远端,实现本地和远程仓库的同步。
git clone git@github.com:yangxinyu-april/Leetcode-exercise.git
Cloning into 'Leetcode-exercise'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
- 克隆后,我用vscode打开,并在这个文件夹内,创建新的文件夹,添加代码
- 保存文件后,提交修改。输入:
//文件添加到仓库
git add /Users/yangxinyu/01yxyfile/01self/code/leetcode/Leetcode-exercise/leandemo/test.py
//文件提交到仓库
git commit -m "first commit"
输出是:
main 88080ba] first commit
1 file changed, 1 insertion(+)
create mode 100644 leandemo/test.py
- 推送到远端
git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 346 bytes | 346.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:yangxinyu-april/Leetcode-exercise.git
c79d835..88080ba main -> main
- 查看GitHub上已经同步了:
四、切换不同分支上传代码
上一小节是在本地的main分支修改,提交到远端的main分支,这一小节来说明一下如何切换分支,并且提交pr,最终合并到主分支上。
- 创建并切换到新的分支
git branch try1
git checkout try1
- 添加代码
- 保存并提交修改到仓库
git add /Users/yangxinyu/01yxyfile/01self/code/leetcode/Leetcode-exercise/leandemo/test.py
git commit -m "second commit"
输出:
[try1 135a452] second commit
1 file changed, 3 insertions(+), 1 deletion(-)
- 将新的分支,推送到远端
git push origin try1
输出:
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 380 bytes | 380.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'try1' on GitHub by visiting:
remote: https://github.com/yangxinyu-april/Leetcode-exercise/pull/new/try1
remote:
To github.com:yangxinyu-april/Leetcode-exercise.git
* [new branch] try1 -> try1
- 可以看到GitHub上有新的pr
-
添加相应的描述信息
-
可以进行合并
-
合并成功
-
可以查看所有的分支