Simple introduction to Git operation and related commands

1 Basic concept understanding

1.1 Introduction to Git

  • Git is a distributed version control system.
  • Centralized vs distributed, SVN vs Git.
  1. The main difference between SVN and Git is where historical versions are maintained.
  2. The Git local repository contains the code repository and the historical repository. The history can be recorded in the local environment development. The historical repository of SVN resides in the central repository. Each comparison and submission of code must be connected to the central repository to proceed.
  3. The benefits of this are:
    • You can view the development version history in an offline environment.
    • If the Git repository serving as the central repository hangs during multi-person development, you can create a new central repository at any time and then synchronize to restore the central repository immediately.

1.2 Workspaces, Staging Areas, and Repositories

Workspace: A directory that can be seen on the computer.

Repository: There is a hidden directory .git in the workspace, which is the repository of Git. There are many things stored in the Git repository, the most important of which is the temporary storage area called stage (or index), the master automatically created by Git, and the pointer HEAD to the master.

git_work_theory

To explain some commands further:

  • git add actually adds files to the staging area.
  • git commit actually commits all the contents of the staging area to the current branch.

2 Git installation and remote warehouse configuration

2.1 Git installation

Generally use the Linux system, here are the Ubuntu system installation commands:

sudo apt-get install git

After the installation is complete, you need the last step of setting, enter at the command line:

$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"

Pay attention to the --global parameter of the git config command. Using this parameter means that all Git repositories on your machine will use this configuration. Of course, you can also specify different usernames and email addresses for a certain repository.

2.2 Remote warehouse configuration

First, you need to add a local SSH key to the remote repository, which is equivalent to configuring pull and upload permissions.

To create an SSH Key, enter in the console command line:

$ ssh-keygen -t rsa -C "[email protected]"

You need to replace the email address with your own email address, then press Enter all the way, and use the default value. Since this Key is not used for military purposes, there is no need to set a password.

If all goes well, you can find the .ssh directory in the user's home directory. There are two files, id_rsa and id_rsa.pub. These two are the key pair of the SSH Key. id_rsa is the private key, which cannot be leaked. id_rsa.pub is the public key and can be safely told to anyone.

Then, you only need to copy your own public key content of id_rsa.pub and add the SSH key to the personal center of the remote warehouse.

Finally, you can safely use the git clone command locally to clone the remote repository code to the local, or push the local code to the remote repository.

3 Multiplayer collaboration (important)

The working mode of multi-person collaboration is usually as follows:

1. 首先,可以试图用git push origin branch-name推送自己的修改;

2. 如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;

3. 如果合并有冲突,则解决冲突,并在本地提交;

4. 没有冲突或者解决掉冲突后,再用git push origin branch-name推送就能成功!

If git pull prompts "no tracking information", it means that the link between the local branch and the remote branch has not been created. Use the command git branch --set-upstream branch-name origin/branch-name.

This is the working mode of multi-person collaboration, and once you are familiar with it, it is very simple.

Notice:

* 查看远程库信息,使用git remote -v;
* 本地新建的分支如果不推送到远程,对其他人就是不可见的;
* 从本地推送分支,使用git push origin branch-name,如果推送失败,先用git pull抓取远程的新提交;
* 在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name,本地和远程分支的名称最好一致;
* 建立本地分支和远程分支的关联,使用git branch --set-upstream branch-name origin/branch-name;
* 从远程抓取分支,使用git pull,如果有冲突,要先处理冲突。

4 Introduction to common commands in Git

4.1 Create a repository

Initialize a repository

$ git init

Add files to repository

Include two steps:

$ git add <file>
$ git commit -m "description"

git add can be used repeatedly to add multiple files, git commit can submit many files at one time, and the input after -m is the description of this submission, and you can enter any content.

4.2 View Workspace Status

$ git status

4.3 View the modified content

$ git diff
$ git diff --cached
$ git diff HEAD -- <file>
注意:
* git diff 可以查看工作区(work dict)和暂存区(stage)的区别.
* git diff --cached 可以查看暂存区(stage)和分支(master)的区别.
* git diff HEAD -- <file> 可以查看工作区和版本库里面最新版本的区别.

4.4 View the modified content

$ git log

Simplified log output information

$ git log --pretty=oneline

4.5 View command history

$ git reflog

4.6 version rollback

$ git reset --hard HEAD^

The above command is to return to the previous version. In Git, HEAD is used to represent the current version. The previous version is HEAD^, the previous version is HEAD^^, and the previous 100 versions are written as HEAD~100.

4.7 Fall back to the specified version number

$ git reset --hard commit_id

commit_id is the version number, which is a sequence calculated with SHA1

4.8 Undo changes

Discard workspace modifications

$ git checkout -- <file>

This command refers to undoing all modifications of the file in the workspace. There are two situations:

1. One is that the file has not been placed in the temporary storage area since it was modified. Now, undo the modification and return to the same state as the version library;

2. One is that after the file has been added to the temporary storage area, it has been modified. Now, undo the modification and return to the state after it was added to the temporary storage area.

In short, it is to return this file to the state of the last git commit or git add.

Discard changes to the staging area

It is divided into two steps:
the first step is to undo the modification of the temporary storage area (unstage) and put it back into the workspace:

$ git reset HEAD <file>

The second step is to undo the modification of the workspace

$ git checkout -- <file>

summary:

  1. When you mess up the contents of a file in the workspace and want to directly discard the modifications in the workspace, use the command git checkout -- <file>.
  2. When you not only mess up the content of a file in the workspace, but also add it to the temporary storage area, and want to discard the modification, it is divided into two steps. The first step is to use the command git reset HEAD <file>, and then it returns to the first step, and the second step is the first step. operate.

  3. When inappropriate changes have been submitted to the repository, if you want to revoke this submission and perform version rollback, the premise is that it has not been pushed to the remote repository.

4.9 Deleting files

$ git rm <file>

git rm <file>is equivalent to executing

$ rm <file>
$ git add <file>

further explanation

Q: For example rm text.txt, how to recover if it is deleted by mistake?

A: Execute git checkout -- text.txtto rewrite the contents of the version library back to the workspace

Q: If we execute it, git rm text.txtwe will find that the text.txt in the workspace is also deleted. How to restore it?

A: First undo the modification of the temporary storage area, put it back into the workspace, and then write it back to the workspace from the repository

$ git reset head text.txt
$ git checkout -- text.txt

Q: What if I really want to delete files from the repository?

A: Execute git commit -m "delete text.txt", the latest version library after submission will not contain this file

4.10 Branches

create branch

$ git branch <branchname>

View branch

$ git branch

git branchThe command will list all branches, and the current branch will be marked with an *.

switch branch

$ git checkout <branchname>

Create + switch branches

$ git checkout -b <branchname>

Merge a branch into the current branch

$ git merge <branchname>

delete branch

$ git branch -d <branchname>

View the branch merge graph

$ git log --graph

When Git can't automatically merge branches, it has to resolve conflicts first. After the conflict is resolved, commit again, and the merge is complete. Use the git log --graphcommand to see the branch merge diagram.

Normal mode merge branches

$ git merge --no-ff -m "description" <branchname>

Because this merge will create a new commit, add -mparameters and write the commit description in. When merging branches, adding --no-ffparameters can be merged in normal mode. It can be seen that a merge has been done, including information such as author and timestamp, while fast forward merge can not see that a merge has been done.

Save the job site

$ git stash

View the job site

$ git stash list

Restoring the job site

$ git stash pop

discard an unmerged branch

$ git branch -D <branchname>

View remote library information

$ git remote -v

Create a local branch corresponding to the remote branch

$ git checkout -b branch-name origin/branch-name,

The names of the local and remote branches should be the same;

Establish the association between the local branch and the remote branch

$ git branch --set-upstream branch-name origin/branch-name;

push branch from local

$ git push origin branch-name

If the push fails, use git pull to grab the new commit from the remote first.

Grab branch from remote

$ git pull

If there is a conflict, resolve the conflict first.

4.11 Labels

A tag is an easy-to-remember meaningful name that is tied to a commit.

create a new label

$ git tag <tagname>

command git tag Used to create a new label, the default is HEAD, or a commit id can be specified.

Specify label information

$ git tag -a <tagname> -m <description> <branchname> or commit_id

git tag -a -m "blablabla..." can specify label information.

PGP signature tags

$ git tag -s <tagname> -m <description> <branchname> or commit_id

git tag -s -m "blablabla..." can sign tags with PGP.

view all tags

$ git tag

push a local tag

$ git push origin <tagname>

Push all unpushed local tags

$ git push origin --tags

delete a local tag

$ git tag -d <tagname>

delete a remote tag

$ git push origin :refs/tags/<tagname>

Reference link

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325260226&siteId=291194637