更新项目
当自己的本地项目与 远程的github 的仓库已经建立远程连接时, 则直接按照下面的步骤,
将本地的项目代码更新到远程仓库。
# Stage the resolved file
git add README.md <file1> <file2>
# To stage all changes:
git add .
# Commit the merge
git commit -m "Resolved merge conflict in README.md"
# Push the changes
git push origin main
如果在此之前, 自己的本地仓库并没有与远程的仓库建立连接时, 则先需要按照下面的步骤进行连接建立。
1. 建立连接
由于github在 2021 年开始, 取消使用密码登录的方式, 因此 这里推荐使用第二种 通过ssh 连接的方式,进行登录。
The error occurs because GitHub no longer supports password-based authentication for HTTPS URLs (as of August 13, 2021). Instead, you need to use one of the following authentication methods:
Option 1: Use a Personal Access Token (PAT)
GitHub now requires a Personal Access Token (PAT) instead of a password for HTTPS authentication.
Steps:
-
Generate a PAT:
- Go to your GitHub account settings: GitHub Tokens.
- Click Generate new token.
- Select the appropriate scopes (e.g.,
repo
for full control of private repositories). - Generate the token and copy it (you won’t be able to see it again).
-
Use the PAT for Authentication:
- When prompted for a password, paste the PAT instead of your GitHub password.
Example:
Username for 'https://github.com': your_account Password for 'https://xxxxx@github.com': <paste-your-PAT-here>
Option 2: Use SSH Authentication
SSH is a more secure and convenient way to authenticate with GitHub.
Steps:
- Generate an SSH Key (if you don’t already have one):
- Run the following command in your terminal:
ssh-keygen -t ed25519 -C "your_email@example.com"
- Press Enter to accept the default file location and passphrase (optional).
- Run the following command in your terminal:
The command ssh-keygen -t ed25519 -C "your_email@example.com"
is used to generate a new SSH key pair for secure authentication with remote servers, such as GitHub. Let’s break down the command and its components:
Command Breakdown
-
ssh-keygen
:- This is the command-line tool used to generate, manage, and convert SSH keys.
-
-t ed25519
:- The
-t
flag specifies the type of key to generate. ed25519
is a cryptographic algorithm used to create the key pair. It is a modern, secure, and efficient algorithm based on elliptic curve cryptography.- You can replace
ed25519
with other algorithms, such as:rsa
: Older but widely supported algorithm (e.g.,-t rsa -b 4096
generates a 4096-bit RSA key).ecdsa
: Elliptic Curve Digital Signature Algorithm (e.g.,-t ecdsa -b 521
generates a 521-bit ECDSA key).dsa
: Older and less secure (not recommended).
- The
-
-C "your_email@example.com"
:- The
-C
flag adds a comment to the key, which is typically your email address or a label to help identify the key. - This comment is embedded in the public key file and is useful for keeping track of which key is associated with which account or purpose.
- The
Why Use Different Algorithms (-t
Options)?
You can use different algorithms (ed25519
, rsa
, ecdsa
, etc.) for different repositories or purposes, but this is not common practice. Instead, people usually generate one key per machine or purpose and reuse it across multiple repositories. However, here are some reasons you might use different algorithms:
-
Compatibility:
- Some older systems or services may not support modern algorithms like
ed25519
. In such cases, you might need to usersa
orecdsa
.
- Some older systems or services may not support modern algorithms like
-
Security Requirements:
- If you have specific security requirements, you might choose an algorithm based on its strength or performance characteristics.
-
Organizational Policies:
- Some organizations enforce specific key types for compliance or standardization.
How to Use Different Keys for Different Repositories
If you want to use different SSH keys for different repositories, you don’t need to generate keys with different algorithms. Instead, you can:
- Generate multiple SSH key pairs (e.g., one for work and one for personal use).
- Add the keys to your SSH agent.
- Configure your SSH client to use the appropriate key for each repository using the
~/.ssh/config
file.
Summary
- The command
ssh-keygen -t ed25519 -C "your_email@example.com"
generates a secure SSH key pair using theed25519
algorithm. - You can use different algorithms (
-t
options) for compatibility or specific requirements, but it’s not necessary for managing multiple repositories. - To use different keys for different repositories, generate multiple keys and configure them in your
~/.ssh/config
file.
Let me know if you need further clarification!
-
Add the SSH Key to Your GitHub Account:
- Copy the public key to your clipboard:
cat ~/.ssh/id_ed25519.pub
- Go to your GitHub account settings: GitHub SSH Keys.
- Click New SSH key, give it a title, and paste the public key.
- Copy the public key to your clipboard:
-
Change the Remote URL to SSH:
这里注意 使用的 git@github.com
- Update your remote repository URL to use SSH instead of HTTPS:
git remote set-url origin git@github.com:xxxx_name/respitory.git
这里需要注意, 你远程的仓库 使用的是main 还是 master.
- Now you can push without entering a username or password:
git push origin main
Option 3: Use GitHub CLI
If you have the GitHub CLI installed, you can authenticate using the gh
command.
Steps:
- Install the GitHub CLI: GitHub CLI Installation.
- Authenticate with GitHub:
gh auth login
- Follow the prompts to log in and authorize the CLI.
Option 4: Use a Credential Helper
You can configure Git to remember your credentials.
Steps:
- Enable the credential helper:
git config --global credential.helper store
- Push your changes. The first time, you’ll be prompted for your username and PAT. After that, Git will remember your credentials.
Summary
- Recommended: Use a Personal Access Token (PAT) or switch to SSH for authentication.
- If you’re unsure, start with the PAT method.
2. 处理冲突
当远程仓库和本地仓库发生冲突时, 需要先解决冲突
我们正常更新时的状态是, 远程仓库的文件在本地是具有的,
而如果远程的文件在本地中不存在时, push 时就会存在冲突, 此时需要先需要拉取远程中的文件,然后,根据自己的选择,进行更新或者更改, 之后在推送。
The error indicates that the remote repository has changes that you don’t have locally, and Git is preventing you from overwriting those changes. To resolve this, you need to pull the remote changes first, merge them with your local changes, and then push your updates.
Steps to Fix the Issue:
-
Pull the Remote Changes:
Run the following command to fetch and merge the remote changes into your local branch:git pull origin main
- If you’re using SSH, it will look like this:
git pull origin main
- If you’re using HTTPS, it will prompt you for your GitHub username and Personal Access Token (PAT).
- If you’re using SSH, it will look like this:
-
Resolve Conflicts (if any):
- If there are merge conflicts, Git will notify you. Open the conflicting files, resolve the conflicts, and save the changes.
- After resolving conflicts, stage the resolved files:
git add <file1> <file2>
-
Commit the Merge:
If there were conflicts, commit the merge:git commit -m "Merge remote changes into local branch"
-
Push Your Changes:
Once the remote changes are merged with your local changes, push your updates:git push origin main
Optional: Force Push (Not Recommended)
If you’re sure you want to overwrite the remote changes (e.g., if you’re working alone and don’t care about the remote changes), you can force push:
git push --force origin main
Warning: Force pushing can overwrite remote changes, so use it with caution, especially in collaborative environments.
Summary of Commands:
git pull origin main
# Resolve conflicts if any
git add .
git commit -m "Merge remote changes"
git push origin main
3. 手动解决冲突
The git pull
command resulted in a merge conflict in the README.md
file. This happens when Git cannot automatically merge changes from the remote repository with your local changes. You’ll need to manually resolve the conflict before proceeding.
Steps to Resolve the Conflict:
- Open the Conflicted File:
Open theREADME.md
file in your text editor or IDE. You’ll see conflict markers like this:<<<<<<< HEAD Local changes ======= Remote changes >>>>>>> 7006db8
注意, 等号线上方的代表的本地仓库中的内容, 等号线下面的代表的是远程仓库中的内容,
需要根据自己的需求, 进行更改。
<<<<<<< HEAD
indicates the start of your local changes.=======
separates your local changes from the remote changes.>>>>>>> 7006db8
indicates the end of the remote changes.
-
Resolve the Conflict:
Edit the file to keep the changes you want. For example:- Keep both changes:
Local changes Remote changes
- Keep only local changes:
Local changes
- Keep only remote changes:
Remote changes
Remove the conflict markers (
<<<<<<<
,=======
, and>>>>>>>
) after resolving. - Keep both changes:
-
Stage the Resolved File:
Once you’ve resolved the conflict, stage the file:git add README.md
-
Commit the Merge:
Commit the resolved changes:git commit -m "Resolved merge conflict in README.md"
-
Push Your Changes:
Push the resolved changes to the remote repository:git push origin main
Example Workflow:
# Open README.md and resolve conflicts
nano README.md
# Stage the resolved file
git add README.md
# Commit the merge
git commit -m "Resolved merge conflict in README.md"
# Push the changes
git push origin main
Additional Notes:
- If you’re unsure how to resolve the conflict, you can use a merge tool like
meld
,kdiff3
, or the built-in tools in your IDE (e.g., VS Code). - To abort the merge and start over (if needed), run:
git merge --abort
小结
全局配置git 上的用户名和email:
git config --global user.name "username"
git config --global user.email 1234567@email.com
在本地的client 的git, 配置git初始化默认的分支由master 变为main;
git config --global init.defaultBranch main
upload
一 : 上传大量文件(适用于在github上 刚新建一个的仓库A)
1.在本地新建工程文件夹T,将所有待上传的文件拷贝到T中;
2.在T中空白处,点击git bash here; 在github 上点击copy 刚刚仓库A的地址;
3.使用 git clone “仓库A的地址” ,从github 上拉取该仓库到本地;
4.在本地T中, cd到仓库A的路径下,注意到此时在显示 (main),代表进入到该仓库中;
5.在T中将待上传的文件拷贝到 仓库A中, 输入 “git add .” ,注意空格和点号;
6.输入 git commit -m “your logs infor” ;
7.输入 git push -u origin main, 将本地仓库push 到 github 上面,完成代码上传。
note: git add . : 是将当前目录下所有文件添加到 待上传区域;
git add xx.txt : 可以指定当前目录下待上传的文件;
git push -u origin main: -u 代表首次提交, 后续更新提交时,可以不用;
remote
二:本地仓库远程连接到 github 上已经有的仓库。
1.在本地文件夹T中空白处,点击git bash here; 输入git init;
2.将本地的仓库关联到Github上:
git remote add origin https://github.com/h-WAVES/test0913
-
选中待上传的文件, 文件之间空格隔开;
git add file1 fiel2; -
备注此次操作的信息
git commit -m “logs” -
上传之前先进行Pull 确认一下;如果在github上初始化仓库时,使用第二个;
git pull origin main
git pull --rebase origin main -
待上传的文件push 到远程仓库中;
git push origin main
## delete
三 删除远程仓库中的文件夹
1.在本地文件夹T中空白处,点击git bash here; 输入git init;
2.git clone 项目地址, cd 到该对应的路径下
3.git rm -r folder/
4. git commit -m "delete folder"
5. git push origin main
完成删除
OpenSSL SSL_read: Connection was reset, errno 10054; 解除SSL 验证:
git config --global http.sslVerify "false"
当github 创建仓库时,选择了初始化.gitignore 和 README.md 时,
此时在github 上和本地的仓库由于文件的不同出现不匹配的情况:
对于error: failed to push some refsto‘远程仓库地址’
git pull --rebase origin main