《Git创建本地仓库、提交和查看文件》

本文详细介绍如何使用Git进行本地仓库的创建、文件管理、版本控制等基础操作,包括配置账户、添加文件、提交修改、查看状态及历史记录等关键步骤。

按win键进入开始菜单,选择Git Bash,打开:

1、创建本地仓库 

$ mkdir /E/Git

2、进入仓库

$ cd /E/Git

此时,命令行的前一行会改变,由 ~/Git更改为/E/Git

//更改前的标题头
dell@DESKTOP-BJG7JVO MINGW64 ~/Git
//更改后的标题头
dell@DESKTOP-BJG7JVO MINGW64 /E/Git

3、显示当前路径

$ pwd

显示结果:

/E/Git

4、初始化空Git存储库

$ git init

显示结果:

Initialized empty Git repository in E:/Git/.git/

5、添加文件到Git存储库

(1)在Git存储库路径(/E/Git)下新建一个txt文本文档,注意必须是在Git存储库路径创建,不然Git找不到,可以自己试试,添加内容如下:

Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.

(2)使用git add将文件添加到仓库

$ git add readme.txt

(3)使用git commit将文件提交到仓库

$ git commit -m "wrote a readme file"

因为我没有之前没有配置Git账户用于验证身份(identity),因此,需要先配置一下账户:

//弹出的配置账户提示信息
*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'dell@DESKTOP-BJG7JVO.(none)')

提示信息给出了配置账户的命令,需要设置邮箱和用户名:

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

这个是配置全局账户,如果需要配置单独账户,需要使用以下命令:

git config user.email "you@example.com"
git config user.name "Your Name"

配置全局账户和配置单独账户具体分析,请看这篇博文:https://blog.youkuaiyun.com/coco_wonderful/article/details/51822143

配置完账户之后,再次执行步骤(3),如果之前已配置账户,那可能就不需要了,显示结果如下:

$ git commit -m "wrote a readme file"
[master (root-commit) 95b4579] wrote a readme file
 1 file changed, 4 insertions(+)
 create mode 100644 readme.txt

注:git commit命令,-m后面输入的是本次提交的说明,可以输入任意内容,当然最好是有意义的,这样你就能从历史记录里方便地找到改动记录。

commit还可以一次提交多个文件:

$ git add file1.txt
$ git add file2.txt
$ git add file3.txt
$ git commit -m "add 3 files."

6、查看Git存储库当前状态

(1)修改readme.txt文件,修改内容如下:

Git is a distributed version control system.
Git is free software.

(2)使用git status命令看看结果

$ git status

显示结果:

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")

这提示的意思是,文档已被修改过但是还没有提交(Changes not staged for commit)

7、使用git diff命令查看不同

$ git diff readme.txt

我的竟然没有显示,凉凉

8、使用git log命令查看历史记录

$ git log

显示结果:

commit f4b7b4328a11dcae2e38e4e361c6bb94ef80dd11 (HEAD -> master)
Author: xxxxxx  xxxxxxxxxxxxxxxxxxxxxx
Date:   Tue Oct 16 10:45:25 2018 +0800

    wrote a readme file

9、使用git reset命令回退到上一个版本

$ git reset --hard HEAD^

显示结果:

HEAD is now at 95b4579 wrote a readme file

10、使用cat命令查看文件

$ cat readme.txt

因为我回退了一次,所以内容变成了未修改的:

Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.

11、使用git reflog查看使用过的命令

$ git reflog

显示结果:

95b4579 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
f4b7b43 HEAD@{1}: commit: wrote a readme file
95b4579 (HEAD -> master) HEAD@{2}: commit (initial): wrote a readme file

前面的数字是commit id。知道commit id可以回退上一次执行的命令,回退命令为git reset --hard <commit id>

参考博文1:https://www.cnblogs.com/lixiaolun/p/4360732.html

参考博文2:https://blog.youkuaiyun.com/coco_wonderful/article/details/51822143

 

GitLab and SSH keys Git is a distributed version control system, which means you can work locally, then share or "push" your changes to a server. In this case, the server is GitLab. GitLab uses the SSH protocol to securely communicate with Git. When you use SSH keys to authenticate to the GitLab remote server, you don't need to supply your username and password each time. Prerequisites To use SSH to communicate with GitLab, you need: The OpenSSH client, which comes pre-installed on GNU/Linux, macOS, and Windows 10. SSH version 6.5 or later. Earlier versions used an MD5 signature, which is not secure. To view the version of SSH installed on your system, run ssh -V. Supported SSH key types To communicate with GitLab, you can use the following SSH key types: ED25519 RSA DSA (Deprecated in GitLab 11.0.) ECDSA (As noted in Practical Cryptography With Go, the security issues related to DSA also apply to ECDSA.) Administrators can restrict which keys are permitted and their minimum lengths. ED25519 SSH keys The book Practical Cryptography With Go suggests that ED25519 keys are more secure and performant than RSA keys. OpenSSH 6.5 introduced ED25519 SSH keys in 2014 and they should be available on most operating systems. RSA SSH keys Available documentation suggests that ED25519 is more secure than RSA. If you use an RSA key, the US National Institute of Science and Technology in Publication 800-57 Part 3 (PDF) recommends a key size of at least 2048 bits. The default key size depends on your version of ssh-keygen. Review the man page for your installed ssh-keygen command for details. See if you have an existing SSH key pair Before you create a key pair, see if a key pair already exists. On Windows, Linux, or macOS, go to your home directory. Go to the .ssh/ subdirectory. If the .ssh/ subdirectory doesn't exist, you are either not in the home directory, or you haven't used ssh before. In the latter case, you need to generate an SSH key pair. See if a file with one of the following formats exists: Algorithm Public key Private key ED25519 (preferred) id_ed25519.pub id_ed25519 RSA (at least 2048-bit key size) id_rsa.pub id_rsa DSA (deprecated) id_dsa.pub id_dsa ECDSA id_ecdsa.pub id_ecdsa Generate an SSH key pair If you do not have an existing SSH key pair, generate a new one. Open a terminal. Type ssh-keygen -t followed by the key type and an optional comment. This comment is included in the .pub file that's created. You may want to use an email address for the comment. For example, for ED25519: ssh-keygen -t ed25519 -C "<comment>" For 2048-bit RSA: ssh-keygen -t rsa -b 2048 -C "<comment>" Press Enter. Output similar to the following is displayed: Generating public/private ed25519 key pair. Enter file in which to save the key (/home/user/.ssh/id_ed25519): Accept the suggested filename and directory, unless you are generating a deploy key or want to save in a specific directory where you store other keys. You can also dedicate the SSH key pair to a specific host. Specify a passphrase: Enter passphrase (empty for no passphrase): Enter same passphrase again: A confirmation is displayed, including information about where your files are stored. A public and private key are generated. Add the public SSH key to your GitLab account and keep the private key secure. Configure SSH to point to a different directory If you did not save your SSH key pair in the default directory, configure your SSH client to point to the directory where the private key is stored. Open a terminal and run this command: eval $(ssh-agent -s) ssh-add <directory to private SSH key> Save these settings in the ~/.ssh/config file. For example: # GitLab.com Host gitlab.com PreferredAuthentications publickey IdentityFile ~/.ssh/gitlab_com_rsa # Private GitLab instance Host gitlab.company.com PreferredAuthentications publickey IdentityFile ~/.ssh/example_com_rsa For more information on these settings, see the man ssh_config page in the SSH configuration manual. Public SSH keys must be unique to GitLab because they bind to your account. Your SSH key is the only identifier you have when you push code with SSH. It must uniquely map to a single user. Update your SSH key passphrase You can update the passphrase for your SSH key. Open a terminal and run this command: ssh-keygen -p -f /path/to/ssh_key At the prompts, type the passphrase and press Enter. Upgrade your RSA key pair to a more secure format If your version of OpenSSH is between 6.5 and 7.8, you can save your private RSA SSH keys in a more secure OpenSSH format. Open a terminal and run this command: ssh-keygen -o -f ~/.ssh/id_rsa Alternatively, you can generate a new RSA key with the more secure encryption format with the following command: ssh-keygen -o -t rsa -b 4096 -C "<comment>" Add an SSH key to your GitLab account To use SSH with GitLab, copy your public key to your GitLab account. Copy the contents of your public key file. You can do this manually or use a script. For example, to copy an ED25519 key to the clipboard: macOS: tr -d '\n' < ~/.ssh/id_ed25519.pub | pbcopy Linux (requires the xclip package): xclip -sel clip < ~/.ssh/id_ed25519.pub Git Bash on Windows: cat ~/.ssh/id_ed25519.pub | clip Replace id_ed25519.pub with your filename. For example, use id_rsa.pub for RSA. Sign in to GitLab. In the top right corner, select your avatar. Select Preferences. From the left sidebar, select SSH Keys. In the Key box, paste the contents of your public key. If you manually copied the key, make sure you copy the entire key, which starts with ssh-ed25519 or ssh-rsa, and may end with a comment. In the Title text box, type a description, like Work Laptop or Home Workstation. Optional. In the Expires at box, select an expiration date. (Introduced in GitLab 12.9.) The expiration date is informational only, and does not prevent you from using the key. However, administrators can view expiration dates and use them for guidance when deleting keys. GitLab checks all SSH keys at 02:00 AM UTC every day. It emails an expiration notice for all SSH keys that expire on the current date. (Introduced in GitLab 13.11.) GitLab checks all SSH keys at 01:00 AM UTC every day. It emails an expiration notice for all SSH keys that are scheduled to expire seven days from now. (Introduced in GitLab 13.11.) Select Add key. 翻译一下
07-04
### GitLab 与 SSH 密钥的配置 GitLab 是一个流行的 DevOps 平台,支持使用 SSH 密钥进行安全的身份验证。SSH(Secure Shell)是一种加密协议,允许用户通过不安全的网络连接到远程服务器并执行命令。为了在 GitLab 中使用 SSH 密钥,需要生成一对密钥:公钥私钥,并将公钥添加到 GitLab 账户中。 #### 生成 SSH 密钥对 如果尚未生成 SSH 密钥对,可以通过以下命令在本地计算机上生成: ```bash ssh-keygen -t rsa -b 4096 -C "your_email@example.com" ``` 此命令将生成默认文件名为 `id_rsa` 的密钥对,其中 `-C` 参数用于指定注释,通常是用户的电子邮件地址[^2]。 系统会提示输入保存密钥的路径,默认为 `~/.ssh/id_rsa`,可以直接按回车键接受默认值。接着,会要求设置一个密码(passphrase),建议设置以增加安全性。 #### 添加 SSH 密钥到 GitLab 生成密钥后,下一步是将公钥添加到 GitLab 账户中。可以通过以下命令查看公钥内容: ```bash cat ~/.ssh/id_rsa.pub ``` 复制输出的内容,然后登录到 GitLab,依次执行以下操作: 1. 点击右上角的用户头像,选择 **Settings**。 2. 在左侧菜单中选择 **SSH Keys**。 3. 在文本框中粘贴之前复制的公钥。 4. 输入一个标题来标识该密钥(例如“Personal Laptop”)。 5. 点击 **Add key** 按钮。 完成上述步骤后,GitLab 将识别该密钥,并允许使用它进行身份验证[^2]。 #### 配置 SSH 客户端 为了让 SSH 客户端知道要使用哪个密钥,可以编辑 `~/.ssh/config` 文件。以下是一个典型的配置示例: ```bash Host gitlab.com IdentityFile ~/.ssh/id_rsa User git ``` 此配置确保当访问 `gitlab.com` 时,SSH 客户端将使用 `~/.ssh/id_rsa` 私钥,并以 `git` 用户身份进行连接。此外,还可以禁用严格的主机密钥检查(不推荐用于生产环境),以便自动接受新的主机密钥: ```bash echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config ``` 此命令会覆盖现有的 `~/.ssh/config` 文件,并添加一行配置以禁用严格主机密钥检查。 #### 使用 SSH 密钥进行 Git 操作 一旦 SSH 密钥被正确配置,就可以使用 Git 命令与 GitLab 进行交互,而无需每次都输入用户名密码。例如,克隆仓库的命令如下: ```bash git clone git@gitlab.com:username/project.git ``` 此命令将以 SSH 协议克隆指定的 GitLab 项目。如果一切配置正确,系统将不会提示输入用户名或密码。 #### 自动化部署中的 SSH 密钥管理 在 CI/CD 流水线中使用 SSH 密钥时,通常需要将私钥作为环境变量注入到构建环境中。以下是一个 `.gitlab-ci.yml` 示例片段,展示了如何在部署阶段使用 SSH 密钥: ```yaml deploy: image: ubuntu:latest stage: deploy before_script: - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )' - eval $(ssh-agent -s) - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - - mkdir -p ~/.ssh - chmod 700 ~/.ssh - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config script: - ssh root@$SERVER "docker stop $APP_NAME; docker system prune -a -f; docker pull $DOCKER_REPO; docker container run -d --name $APP_NAME -p $PORT:8080 -e SPRING_PROFILES_ACTIVE=$SPRING_ACTIVE_PROFILE $DOCKER_REPO" ``` 在此配置中,首先安装 `openssh-client` 工具包,并启动 `ssh-agent` 来管理私钥。随后,将 `$SSH_PRIVATE_KEY` 环境变量中的私钥加载到 `ssh-agent` 中,并创建必要的 SSH 配置文件。最后,在 `script` 部分使用 `ssh` 命令连接到远程服务器并执行部署任务[^2]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值