更新项目
当自己的本地项目与 远程的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:
-
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:
- Update your remote repository URL to use SSH instead of HTTPS:
git remote set-url origin git@github.com:xxxx_name/respitory.git
- Now you can push without entering a username or password:
git push origin main
- Update your remote repository URL to use SSH instead of HTTPS:
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