preface

In this paper, I have recorded all kinds of git problems I have encountered since I worked. Now I look back at these problems and feel that I used to be a good cook. But I’ve also seen my own growth.

Git Clone using SSH failed. Procedure

An error prompt

Could not read from remote repository.

Please make sure you have the correct access rights

Error reason

The SSH key is invalid or you do not have permission

Solution – Add the SSH key again

  1. Ssh-keygen -t rsa -c “username”
  2. Generating public/private rsa key pair.

Enter file in which to save the key (C:\Users\ hao 忛┈/. SSH /id_rsa)

  1. C:\Users\ hao 忛┈/. SSH /id_rsa already exists.

Overwrite (y/n)? Y enter y

  1. Enter same passphrase again: Press Enter
  2. Your identification has been saved in C:\Users\ hao 忛┈/. SSH /id_rsa. Address for saving the private key

Your public key has been saved in C:\Users\ hao 忛┈/. SSH /id_rsa.pub. Public Key Storage Address

  1. Follow the path to find the public key and add it to Git

Git pull failed

An error prompt

Your local changes to the following files would be overwritten by merge:

Error reason

Someone else modified the file to commit to the repository, and I modified the file locally, causing a conflict when pulling the code

The solution – store changes

Perform the following operations in sequence

Git Stash restores the workspace to the last commit while backing up changes made locally

The git pull pull

Git Stash pop pops up your most recently saved content

View corresponding files to resolve conflicts



Git then commits its code in threes

Git Pull failed (2)

An error prompt

Pulling is not possible because you have unmerged files.    

Error reason

The modified file is not submitted

This error actually looks like this — I had already pulled the code before, and there was a conflict. After resolving the conflict, I wanted to pull the code again, and later I realized that after resolving the conflict, I needed to commit again

Solution – Submit it locally

Git commit -m “get new code” git pull

Git push failure

An error prompt

fatal: Could not read from remote repository.

Please make sure you have the correct access rights

Error reason

Cause one: The latest public key is not added to Github. Cause two: The network is disconnected

Cause one solution: Configure the public key

1. Find the public key, open it, and copy its contents



2. Add the public key to Github

Please explain why this happened. Github and Gitlab share the same public key and private key. When I failed to clone (the first error above), I reconfigured the public key and private key, so there is no latest public key on my Github at this time, so I cannot push

Cause two Solution — Connect to the network

1. The network cable is loose. 2

Undo changes to files

describe

I modified a complex index.vue file and felt that I was writing in a mess and had no clue after the modification, but the modified file was not submitted and I wanted to restore it to its original appearance.

The solution

Run git status to get the path to our file



Git checkout — complete path to the file



And then close this file and just open it and remind you whether or not to save your changesDon’t save it

This is a dangerous command, and git overwrites the entire file with the most recent commit. Do not use this command unless you really know that you do not want to make local changes to that file.

Their own code is overwritten by code pulled down

describe

My code was just submitted, and then my colleague pulled my code down and submitted it. Then I pulled the code down again, and found that all the code I just wrote was missing (note: I wrote the same file in the contract).

Solution (1)

Git log finds the last commit code



Git reset –hard Commit code for replication

And then close this file and just open it and remind you whether or not to save your changesDon’t save it

Solution (2)

CTRL + Z To use this method, you need to know which files you have changed and the editor has not been closed (I had the editor stuck and restarted the editor…..).

Just want to pull remote code and not commit

describe

My own code is only a little bit written, the colleague next to me said he submitted, asked me to pull, because nothing written, so do not want to commit

A whimsical attempt

Our local changes to the following files would be overwritten by merge our local changes to the following files would be overwritten by merge

The solution

Git pull git Stash pop Git stash

You want to go back to where you were before pull

Problem description

After commit, pull the code down, a lot of conflicts occur, and then you want to go back to the state before pull, format the code and pull it again

The solution

Git merge –abort returns to the state before the conflict

Git merge — Abort will abort the merge process and attempt to rebuild the state before the merge. However, git merge –abort cannot reproduce the pre-merge state in some cases if there are uncommitted files at the start of the merge. (Especially if these uncommitted files will be modified during the merge)

View your commit record

describe

Companies want to keep track of what they do every day and want to keep a log by looking at their commit records,

The solution

Git log This method has a bug that only shows the commit record of the most recent push and finally, only the history on GitLab

Git is secret-free

Problem description

When I first took over the code at the new company, THE first thing I did was to configure SSH. After a few operations, I found that it didn’t work. I later learned that there was no SSH at all. Then I have been using HTTP which way to code, but using this way every time when dealing with a remote warehouse need to enter a password, then enter the password was too really annoying, went to baidu check for solution, check also has a real (before the thought way of HTTP is must enter the password, so not the first time to go to baidu)

The solution

git config --global credential.helper store
Copy the code

After this command is executed, you need to enter the command again. After this command, you do not need to enter the password any more

Cancel the merge

Problem description

When merging branches, the branch that will be merged was accidentally merged into the master branch. Originally, the master branch was merged into the 1124 production package, and now the master branch is merged back to its original form.

The solution

Git co master 2. Check the historical version number to find the version you want to restore git reflog

Git reset –hard 60a4a3f

In addition, the record under reflog will also disappear after the rollback. It was only when I recorded by myself that I found that I could not find the version number I just used

Push branch code

Problem description

If you want to push your code to a remote location, you can’t just use git push

The solution

Git doesn’t know which branch to push our code to when we use this command. This is the same with Git pull

Plan a

Git push origin

originBranch is a remote branch. This method can be used to push an originBranch to a specified branch. If the remote branch does not exist, the branch is automatically created

Scheme 2

Git push –set-upstream origin

Git push –set-upstream origin

Git push can then be used directly in this branch, which will be pushed by default to the remote branch with which we have established a relationship.

Plan 3

Git branch — set-upgrade-to =origin/


If the remote does not exist, the branch is automatically created

git commit –amend

Problem description

I forgot to delete the console in the last commit, but I do not want to add a new commit after deleting the console, but I do not know how to use the command….

The solution

  • The following interface is displayed after the command is executed

  • Since we’re just fixing our commit, there’s nothing else going on here, so let’s just press Esc to exit edit mode, and then type, okay:wqSave and exit
  • Finally you can typegit logTake a look at our commit record

Wrong branch!!

Problem description

Based on colleagues of the branch, some of the operation of the buried point, lu code, forgot to cut the branch, will write their own code in colleagues of the branch, at this time he did not submit, how to do?

The solution

  1. Let’s hold the code temporarilygit stash
  2. Move out a branch git checkout -b
  3. Pop up your own temporary code git Stash pop

Changing the branch Name

Problem description

You want to change the name of the branch that you moved out of

The solution

Git branch -m oldName newName oldName Original branch name newName Current branch name

How do I put a local repository on top of Git

Problem description

I first created a local Git repository and did a series of learning. Now I want to upload my local repository to Github and save my own submission records. Why?

The solution

  1. Now create a new remote repository on Github
  2. Use the following command under the local repository to associate the two repositories

Git remote add origin


git remote add origin

  1. Use the following command to push all the contents of the local repository to the remote repository

git push -u origin main

If the command in step 3 is used, the following error occurs



This is because the README file in the new repository is not in the local repository directory. In this case, we can use the following command to merge the contents:

git pull --rebase origin main

And then push