Writing in the front

Have you ever used Git? Maybe you haven’t touched Git yet, or maybe you’ve been using it for a while, but it may still confuse you. This article mainly explains how to learn Git and solve various Git problems encountered by yourself. First of all, I strongly recommend the nugget Git principle detailed and practical guide. (Not 😂)

  • If you’re new to Git, read this brochure and follow the practice to get a general idea of Git.
  • If you’ve been using Git for a while, this little book will give you an Epiphany.
  • The booklet suggests reading it over and over again (at least three times, I’d say), and you’ll learn something different each time you read it.
  • If you want to learn more about Git, you can visit git-book.

Git Common Commands

Git quadruple

Git commit -m "describe" the commit with a profileCopy the code

Other Common Commands

Git log View detailed history git log --stat View brief statistics git Status View workspace status Git branch name Create a branch Checkout -b name to create and switch to new branch git branch -d name to delete branch Git branch -d name delete the branch (you can delete branches that are not merged into the master) git commit --amend git reset --hard Git Reset --soft HEAD^ Discard the latest commit (uncommitted content will not be erased) git Revert HEAD^ Return to a commit git rebase Git merge Name Merge branches to the branch pointed to by head. Git push Origin Localbranch Push code to the specified branch in the remote repository. Git push -d Origin branchName Delete the remote branch git stash Git Stash Pop Pops the stash codeCopy the code

Configure an alias

This section describes how to configure aliases for common commands to improve work efficiency

git config --global alias.st status                 git status ==> git st
git config --global alias.ci commit                 git commit ==> git ci
git config --global alias.co checkout               git checkout ==> git co
git config --global alias.br branch                 git branch ==> git br
git config --global alias.sh stash                  git stash ==> git sh
git config --global alias.pop "stash pop"           git stash pop ==> git pop
Copy the code

Common problems and solutions

Git clone failure

An error prompt

Could not read from remote repository.
Please make sure you have the correct access rights
Copy the code

Error reason

The SSH key is invalid or you have no permission. Procedure

Solution (1) — 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 忛┈/

3.C:\Users\ hao 忛┈/. SSH /id_rsa already exists (y/n)? Y enter y

4.Enter same passphrase again

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

6. Find the public key according to the path and add it to Git

Solution (2) – Clone using HTTP address

Use this way of words need to input their own account and password, a little trouble, not recommended to use

Git pull failure

An error prompt

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

Error reason

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

Solution – Store changes

Perform the following operations: Git Stash Restore the workspace to the contents of the last commit and back up what you have done locally. Git pull git Stash pop up your latest saved contents and view the corresponding files to resolve the conflict

Git then commits its code three times in a row

Git pull failure

An error prompt

Pulling is not possible because you have unmerged files.    
Copy the code

Error reason

The modified file is not submitted

The error looks like this — I’ve pulled code before, and there’s a conflict, and I want to pull the error again after resolving the conflict, and then I realize that after resolving the conflict, I need to commit again

Solution – Commit locally

Git pull -m git pull -m git pull -m git pull -m git pull

Git push failure

An error prompt

fatal: Could not read from remote repository.
Please make sure you have the correct access rights
Copy the code

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

To explain why this happens, github and GitLab share the same public key and private key. When I was working on the company project, I reconfigured the public key and private key when the clone failed (the first error above), so my latest public key was not available on Github, so I could not push

Reason two solution – connect to the Internet

  1. Cable is loose
  2. The user logs out of the Intranet and needs to log in again
  3. WiFi didn’t network

Undo the changes to the file

describe

I modified a complex index.vue file and felt like I was writing in a mess, but the file was uncommitted and I wanted to go back to the way it started.

The solution

Run the git status command to get the path to our file

Git checkout — complete file path

Then close the file and open it before reminding you to save your changes

This is a dangerous command, and git will overwrite the entire file with the latest commit.

Do not use this command unless you know for sure that you do not want to make local changes to that file.

Your own code is overwritten by the pulled code

describe

My own code was just submitted, and 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 for the contract).

Solution (1)

git logFind the commit code of the last commit



Git reset -- Commit code for hard replicationThen close the file and open it to remind you to save your changesDon’t save

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 was stuck and restarted the editor…..).

You just want to pull the remote code instead of commit

describe

I just wrote a little bit of my code, and my colleague said he submitted it and asked me to pull it, because I didn’t write anything, so I didn’t want to commit it

A whimsical attempt

At that time, I thought, can you pull directly, but of course no, git will report the following error to you

our local changes to the following files would be overwritten by merge:
Copy the code

The solution

Git Stash Pop Deposit your own stash code

Want to go back to the state before pull

Problem description

After commit, pull the code, have a lot of conflicts, then want to go back to the state before pull, format the code and then pull

The solution

Git merge — Abort returns to the state before the conflict

Git merge -- Abort will discard the merge process and attempt to recreate the state before the merge. However, if there are uncommitted files when the merge begins, Git merge --abort will not be able to reproduce the state before the merge in some cases. (especially if the uncommitted files will be modified during the merge)Copy the code

View your own COMMIT records

describe

Companies require journaling, and if they want to do that by looking at their own commit records,

The solution

Git log this method is flawed. It only shows the commit record for the last push, and only the history on GitLab

The last

It is not recommended that you use third-party Git visualization tools, first of all, some written tests or interviews are to examine Git, and then feel that after using Git visualization tools, there is no flavor.

This article continues to be updated, interested can pay attention to, we can also discuss the problem together.