Install git

Linux

sudo apt-get install git
Copy the code

Mac

First install Homebrew and then install Git through Homebrew. For details, refer to the Homebrew documentation: brew.sh/.

brew install 
Copy the code

Windows

Download it from git’s official website

Essential knowledge

Git process

warehouse

  1. Remote: Remote main warehouse;
  2. Repository/History: local Repository;
  3. Stage/Index: Git trace tree, temporary storage;
  4. Workspace: your local workspace (that is, your editor’s code)

Related terms

Repository Branch Branch Summary Track Track modify modify stage temporary save commit commit push pull clone Clone amend merge merge Conflict Origin Source Upstream Upstream Downstream verbose reflog Reference logCopy the code

Git basis

Modified project (or project that has been modified but not committed to a local database) No Git Add staged project (or project that has been modified but not committed) Git commit has been committed and saved to a local databaseCopy the code

Git basic workflow

Write the code

Take a snapshot of the modified file and save it to the staging area

Submit the code to upload a snapshot of the files in the staging area to Git

The basic configuration

Git configuration:

Git config --list git config user.name git config user.email git config -hCopy the code

Modify the current configuration (it usually only needs to be configured once, but we need to reconfigure it if we change the computer or change the system) :

Git config --global user.name"ty"Git config --global user.email"[email protected]"
Copy the code

Project configuration. Gitignore

When submitting a project to Git, there are many files that you don’t need to submit, such as node_modules,.vscode,.idea, etc

This is where we need to create a.gitignore file in the root directory of the project

node_modules/
/*/node_modules/
.idea
.vscode
Copy the code

Configure SSH

When we pull and push code from github remote servers, how do we verify who is submitting and pulling code? In order to avoid the trouble of entering the user name and password each time, we can configure SSH to solve the problem.

Check SSH on this machine:

In SSH, // open the command linecdPub cat id_rsa.pub or vim id_rsa.pub // View the file contentsCopy the code

If not, we can use the following command to generate

ssh-keygen
Copy the code

Next, we copy the contents of the public key and log into Github

Click New SSH Key from the profile picture drop-down Settings -> SSH and GPG Keys in the upper right corner to add it.

Run the SSH -t [email protected] command to check whether the SSH configuration is successful.

Git Basic Operations

Initialize the new repository

git init
Copy the code

Viewing file Status

git status
Copy the code

File tracking

Git addCopy the code

If the file is successfully added, we can check it again through git status

Files are submitted to the local repository

Git commit // Commit to a local repository git commit -m'xxx'Git commit-am'xxx'// Temporarily save all traced files and commit git commit-amend-m'xxx'// if -m fails, write a noteCopy the code

Push remote warehouse

Git push origin XXX // you can specify a default source with -u, so that you do not need to add origin git pushCopy the code

Pull remote warehouse

Git pull origin XXX // if no tracking information is displayed, you can create a trace using the following method: Git branch --set-upstream branch-name origin/branch-nameCopy the code

diff

Git reflog git reflog git reflog git reflog git reflog git reflog git reflog git reflogCopy the code

A normal code submission process

git pull
git add .
git commit -m 'xxx'
git push
Copy the code

Switching git versions (This operation is risky and must be performed with caution)

Sometimes business scenarios, or changing requirements, and other factors require us to switch back to a previous version.

To make a version switch, we need to know which version we are in:

git log --oneline
Copy the code

Git reset –hard id git reset –hard id Id can be:

- HEAD^ : last version - HEAD^^: last version - HEAD~10: last 10 versionsCopy the code

When you switch back to a previous version and you can’t see the latest version through git log, you can use git reflog, which will be sorted by all the previous commit paths.

Git Branch Management

We usually separate dev or other development branches from the master branch and use them for development, so that if the development goes wrong, the main branch will not be affected.

Create a new branch using Git branch XXX.

Use Git Checkout XXX to switch to the new branch

Branch, checkout

Git branch -d XXX // Git branch -d XXX git branch -d XXX git branch -d XXXCopy the code

Branch merge

Note: When merging branches, you must make sure that you are merging to the target branch of the branch

Git merge XXX to merge the XXX branch into your current branch.

Normal merge branches is very simple, that is ok, but the branch to merge, if in different branches of the modified the same part of the same file, the git is unable to determine which branch of the code, the use that will generate conflicts, although the git merged, but did not submit, we need to resolve the conflict and resubmit.

Git status can be used to check which files are in conflict, and then resolve them one by one. After fixing the conflicting code according to the correct code, git add, git commit, git push are needed again.

Operating remote warehouse

Git Clone url is used to clone remote repository

Such as:

/ / the default path in your pull down to create a new blog - mason folder git clone https://github.com/MasonEast/blog-mason.git / / folder if you don't want a blog - mason, You can like, behind the url, the blank space to add new name git clone newName / / https://github.com/MasonEast/blog-mason.git if you want to be in the current path down the project file, not the folder in the outside, You can use git clone https://github.com/MasonEast/blog-mason.git.Copy the code

Go to git remote

The repository we cloned will see a remote repository named Origin through Git Remote, which is the original repository identified by Git default

Git remote -v or git remote –verbose you can view more detailed information, i.e. the corresponding project address. Normally there are two, but if you do not have push permission, you can only see the address of one fetch.

git remote -v

origin  [email protected]:MasonEast/blog-mason.git (fetch)
origin  [email protected]:MasonEast/blog-mason.git (push)
Copy the code

Deleting a Remote Branch

Git push origin: XXX // git push origin --delete XXXCopy the code

Deleting a remote repository

git remote rm xxx
Copy the code

Rename the remote repository

git remote rename oldName newName
Copy the code

About Misoperation

Git is mainly used for version control and collaborative development. Misoperations can be reversed, but some of the retractions are irreversible. We must treat them with caution, otherwise some code may be lost.

Modify last commit

Scenario: After one commit, we find that the file is undercommitted. We need to undo the previous commit and resubmit.

Git add XXX // Add the undercommitted files to the hold git commit --amend // Add the undercommitted files to the last commit, this commit will not generate a recordCopy the code

Remove files from the local repository

Scenario: We commit the file to the local repository via Git commit and then remember to add the file we didn’t want to commit.

git rm xxx
Copy the code

Removes files from the staging area

Scenario: Sometimes we get used to git add., but there are files that we shouldn’t commit, and then we need to remove them from the staging area.

Git reset HEAD XXX // Remove XXX file from temporary storageCopy the code

Git in the source

Scenario: When a remote warehouse is relocated, the local address needs to be located to the new warehouse

# remove source
git remote remove origin
# add a new sourcegit remote add origin git@git.. gitxxx.com:fei/stic.git git pull git branch --set-upstream master origin/master git pull origin masterNone of this is easy to read, and all of this is easy to read
git pull origin master --allow-unrelated-histories
Copy the code

Push multiple Git repositories

Scenario: Some open source projects that want to commit to multiple repositories (rarely used)

View the remote repository
git remote -v

Add a new warehouse addressgit remote add origin git git@git.. gitxxx.com:fei/stic.gitCopy the code

The latter

This is the first time to publish git nuggets. The above are just the basic operations you might use in your daily work. Most of them are from the reference “Some Things you Use in your daily work with Git”. This git article has been of great help to me since I worked for one year. I can basically solve all the problems I have encountered. Welcome to exchange and learn from each other

reference

  • A few things about Git that you use in your daily work
  • A summary of the most common Git problems and action lists