本文主要介绍Git的安装及如何与GitHub的绑定等基本配置操作。
Git安装
本文主要简要介绍在MacOS和Windows下的Git安装。
在MacOS下安装Git
在Mac下安装Git,主要通过Homebrew来进行。
具体命令如下:
$ brew install git
$ brew install git-gui
另外,如果没有安装Homebrew,也可以通过MacPorts来安装。(MacPorts的下载链接: The MacPorts Project -- Download & Installation, 根据MacOS版本下载合适的安装包。)
$ sudo port install git
$ sudo port install git-gui
安装成功后,输入git version
来检查当前安装的Git版本。
$ git version
git version 2.48.1
在Windows下安装Git
在浏览器中,登陆Git官网的下载页面(https://git-scm.com/downloads),下载最新Git。
建议选择”64-bit Git for Windows Setup”版本,本文安装的Git版本是: Git 2.47.1.2.
在安装过程中,不同的版本会有些选择的差异,需要注意以下主要选项:
- 在”Select Components”,建议确保选择”Windows Explorer integration”下的”Git Bash Here”和”Git GUI Here”,这样可以方便在鼠标右键中加入快捷操作命令。
- 在”Choosing the default editor used by Git”,根据实际情况选择。需要在安装好选择的软件后,系统检查到相应的应用,才会可以进行Next操作。如果使用VSCode,建议提前安装好。
- 在”Adjusting the name of the initial branch in new repositories”,根据实际情况选择。
- 在”Adjusting your PATH environment”,建议选择”Use Git from the command line and also from 3rd-party software”选项。
- 在”Choosing the SSH executable”,建议选择”Use bundled OpenSSL”。
- 在”Choosing HTTPS transport backend”, 建议选择”Use the OpenSSL library”。
- 在”Configuring the line ending conversations”,建议选择”Checkout Windows-style, commit Unix-style line endings”。
- 在”Configuring the terminal emulator to use with Git Bash”,建议选择”Use MinTTY (the default terminal of MSYS2)”。
- 在”Choose the default behavior of ‘git pull’”, 建议选择”Fast-forward or merge”。
- 在”Choose a credential helper”,建议选择”Git Credential Manager”。
- 在”Configuring extra options”,选择”Enable file system caching”。
- 在”Replacing in-use files”,打开系统的任务管理器(Task Manager)中的详细(Details),将列表中的进程根据PID关闭(End task),然后点击Refresh确保列表中没有进程,然后点击Install进行安装。
在安装完成后,在应用程序中打开”Git → Git Bash”运行。
设置Git中的username和commit邮件地址
在完成Git的安装后,需要在本地设置Git username和用于commit的邮件地址等信息。
如果需要设置在当前Git下,则使用--global
参数。如果只是使用在单个的Repository中,则不需要该参数。
$ git config --global user.name "Jerry Wen"
$ git config --global user.email "xxxx@email.com"
利用SSH完成Git与GitHub的绑定
在GitHub上,一般都是通过SSH来授权的,而且大多数的Git服务器也会选择使用SSH公钥来进行授权,所以想要向GitHub提供代码,首先就得在GitHub上添加SSH key配置。
第1步: 生成SSH key
一般在Linux和Mac上都是默认安装SSH的,而对于Windows系统默认是不安装SSH的,在安装Git Bash时正常会自带SSH。
在Windows中通过Git Bash中输入ssh
命令,查看本机是否安装SSH。
$ ssh
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface] [-b bind_address]
[-c cipher_spec] [-D [bind_address:]port] [-E log_file]
[-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file]
[-J destination] [-L address] [-l login_name] [-m mac_spec]
[-O ctl_cmd] [-o option] [-P tag] [-p port] [-R address]
[-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
destination [command [argument ...]]
ssh [-Q query_option]
在Windows、Linux或Mac中,如果输入ssh
后有如上类似的输入,则表明已安装SSH成功。
生成RSA密钥,在命令行中输入ssh-keygen -t rsa
命令。
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/jerry/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/jerry/.ssh/id_rsa
Your public key has been saved in /Users/jerry/.ssh/id_rsa.pub
%
在生成密钥期间,直接输入3次回车键,即不需要输入密码,之后会在用户的主目录下的.ssh
中生成密钥id_rsa
和公钥id_rsa.pub
。
Linux: ~/.ssh
Mac: ~/.ssh
Windows: C:\Documents and Settings\username\\.ssh
在密钥和公钥生成后,就是将公钥的id_rsa.pub
中的内容添加到GitHub中,这样本地的密钥id_rsa
和GitHub上的公钥id_rsa.pub
才可以进行匹配。授权成功后,就可以向GitHub提供代码。
第2步: 添加SSH key
进入GitHub的个人账号下的”Settings → SSH and GPG keys”。
在”SSH keys”中选择”New SSH key”,将本地文件id_rsa.pub
中的内容粘贴到Key输入框中,并输入一个有意义的Title以方便记忆,然后点击”Add SSH key
”保存。
第3步: 验证绑定是否成功
在添加id_rsa.pub到GitHub上成功后,可通过在本地输入ssh -T git@github.com
命令进行验证。
$ ssh -T git@github.com
The authenticity of host 'github.com (140.82.112.4)' 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 jerrywengit! You've successfully authenticated, but GitHub does not provide shell access.
在首次连接时,会有以上类似输入,需要输入”yes”来确认。在已经连接成功过,则会有以下类似输出:
% ssh -T git@github.com
Hi jerrywengit! You've successfully authenticated, but GitHub does not provide shell access.
%
如有以上类似的输出结果,则说明Git与GitHub绑定成功。
GitHub上的Repository克隆(clone)到本地Git
本案例实现GitHub上的Repository克隆到本地Git上。
备注:本例操作在MacOS执行。
第1步: 查询GitHub的信息
在GitHub上选择待克隆的Repository,点击”Code”界面选择”Code → Local”,然后复制或记录该Repository的链接。
比如本案例中的链接为: https://github.com/jerrywengit/learning.git
第2步: 在本地Git创建相应的目录,并进入该目录。
比如本案例创建的目录为: *Workspace/git/jerrywengit/GitRepo*
第3步: 克隆GitHub上的Repository
在本地创建的目录下执行以下操作,将GitHub上的Repository克隆到本地:
$ git clone <https://github.com/jerrywengit/learning.git>
Cloning into 'learning'...
remote: Enumerating objects: 39, done.
remote: Counting objects: 100% (39/39), done.
remote: Compressing objects: 100% (34/34), done.
remote: Total 39 (delta 12), reused 8 (delta 1), pack-reused 0 (from 0)
Receiving objects: 100% (39/39), 174.07 KiB | 232.00 KiB/s, done.
Resolving deltas: 100% (12/12), done.
GitRepo %
第4步: 检查本地Git和GitHub中的内容是否一致。
在本地Git中检查文件与GitHub中对应的Repository中的内容。
第5步: 在本地Git中增加文件,同步到GitHub上
- 本地Git中创建文件夹和添加新文件
比如在Repository目录中做如下操作:
$ pwd
/Users/jerry/Workspace/git/jerrywengit/GitRepo/learning
$ ls -a
. .git README.md src
.. LICENSE aptiv
(base) jerry@macpro learning %
$ mkdir pic
$ cd pic
$ touch test02.txt
$ cd ..
$ ls ./pic
test02.txt
- 执行git status查看本地仓库状态
在本地仓库的home目录执行,本案例在/Users/jerry/Workspace/git/jerrywengit/GitRepo/learning.
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
pic/
nothing added to commit but untracked files present (use "git add" to track)
$
说明刚刚创建的文件夹没有被追踪(Untracked),也就是没有提交。
- 提交更新
在代码提交前给执行git add
,然后执行git commit
命令。
$ git add pic/
$ git commit -m "commit pic file"
[main 4c602cb] commit pic file
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 pic/test02.txt
(base) jerry@macpro learning $
- 将本地仓库的内容push到远程GitHub仓库中
主要是先检查本地仓库的状态,然后拉取(pull)远程GitHub仓库中的最新内容,最后推送(push)本地仓库中内容到GitHub远程Repository中。
% git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
(base) jerry@macpro learning % git pull origin main
From <https://github.com/jerrywengit/learning>
* branch main -> FETCH_HEAD
Already up to date.
(base) jerry@macpro learning % git push origin main
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 311 bytes | 311.00 KiB/s, done.
Total 4 (delta 1), reused 1 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To <https://github.com/jerrywengit/learning.git>
24bd82c..4c602cb main -> main
(base) jerry@macpro learning %
- 检查远程GitHub中Repository内容是否更新
登陆GitHub,并刷新本案例中的learning仓库是否按预期增加新的内容。
将本地Git仓库克隆到GitHub上
本案例实现本地Git上的仓库增加到远程GitHub上的Repository上。
备注:本例操作在MacOS执行。
第1步: 在本地创建仓库,并初始化
本例中新创建的本地仓库名称为demo
$ cd /Users/jerry/Workspace/git/jerrywengit/GitRepo
$ mkdir demo
$ cd demo
$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in /Users/jerry/Workspace/git/jerrywengit/GitRepo/demo/.git/
(base) jerry@macpro demo % git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
(base) jerry@macpro demo $
第2步: 在GitHub中创建Repository
本案例中的GitHub中Repository的名称为: demo,地址为:GitHub - jerrywengit/demo
第3步: 关联GitHub上的远程仓库
执行git remote add
来关联Git本地仓库与GitHub中远程Repository。其中origin为远程仓库的名字。
$ git remote add origin <https://github.com/jerrywengit/demo.git>
$ git remote -v
origin <https://github.com/jerrywengit/demo.git> (fetch)
origin <https://github.com/jerrywengit/demo.git> (push)
第4步: 同步本地Git仓库与GitHub远程仓库
执行git pull origin master来同步仓库。
如果本地或远程的brach不是master,可以在本地Git初始化时修改或在GitHub修改repository即可。
$ git pull origin master
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (3/3), 859 bytes | 143.00 KiB/s, done.
From <https://github.com/jerrywengit/demo>
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
(base) jerry@macpro demo $
查看本地仓库目录上,是否将里面的README.md文件同步到了本地仓库。
$ ls
README.md
第5步: 本地仓库新增文件测试
在本地仓库新增文件test.txt测试文件,并push到GitHub远程仓库中。
$ cd /Users/jerry/Workspace/git/jerrywengit/GitRepo/demo
$ touch test.txt
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt
nothing added to commit but untracked files present (use "git add" to track)
$ git add test.txt
$ git commit -m "add test file"
[master ae59809] add test file
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
$ git push origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 278 bytes | 278.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To <https://github.com/jerrywengit/demo.git>
29d5d8e..ae59809 master -> master
$
登陆GitHub,检查本例中的demo仓库中是否已增加新的test.txt文件。
<aside> 💡
重点提示: 在我们向远程仓库提交代码的时候,一定要先进行pull操作,再进行push操作,防止本地仓库与远程仓库不同步导致冲突的问题,
</aside>
Branch更新操作
有时由于某些原因,需要在GitHub上修改branch。这时需要在本地Git上执行以下操作,以确保完成branch的更新。
案例: 在GitHub上的仓库由master修改为main,需要在本地Git仓库(Clone仓库)上执行以下操作。
$ git branch -m master main
$ git fetch origin
$ git branch -u origin/main main
$ git remote set-head origin -a