Git~Master installation configuration, workflow, basic command operation, branch management and withdrawal

Installation configuration

The installation is not much to say, it is very simple and there are many articles on the Internet, and just go to the official website to download and install
https://git-scm.com/download
Insert picture description here

In short, input git version to get the version and it means that the installation is complete.

Configure account password

Configuration I will simply record the commands here, such as configuring your name and email, Note that the name and email are yours!

git config --global user.name "Listen-Y"
git config --global user.email "[email protected]"

Then check your configuration

git config --list

Configure key

When Git associates with a remote warehouse, it needs to provide a public key and save the private key locally. Each time it interacts with a remote warehouse, the remote warehouse uses the public key to verify the identity of the interactor. Use the following command to generate the key.The mailbox is your own!

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

After the key is generated, two files id_rsa and id_rsa.pub will be generated in the local /Users/current computer user/.ssh directory. The id_rsa file saves the private key and is saved locally. The id_rsa.pub file saves the public key. Key, you need to upload the contents to the remote warehouse.
The specific operation of obtaining the public key string is as shown in the figure below.
Insert picture description here

Be sure to
enter the ls -ainstruction in the directory where you entered the ssh command to view all files in the current user directory, including hidden files.
Enter the cd .sshinstruction, enter the .ssh directory,
enter the lsinstruction, view the files in the .ssh directory,
enter the cat id_rsa.pubinstruction, view id_rsa.pub Content in the file

Then in your remote repository, whether it is GitHub or GitLab, find the place to add the key. For example, I am GitLab
Insert picture description here
and add the key just cated to the webpage.

work process

The general workflow is as follows:

  • Clone the Git resource as a working directory.
  • Add or modify files on the cloned resource.
  • If other people make changes, you can update the resources.
  • Review the changes before submitting.
  • Submit changes.
  • After the modification is completed, if an error is found, you can withdraw the submission and modify and submit again.

basic concepts

Let's first understand the concepts of Git workspace, staging area, version library and remote library:

  • Workspace: The workspace is where you usually store the project code

  • Index / Stage: Temporary storage area, used to temporarily store your changes. In fact, it is just a file to save the information that will be submitted to the file list

  • Repository: The warehouse area (or local warehouse) is a place where data is safely stored, and there are data that you submit to all versions. Where HEAD points to the latest version put into the warehouse

  • Remote: A remote warehouse, a server hosting the code, which can be simply regarded as a computer in your project team for remote data exchange
    Insert picture description here

The four states of the file

Version control is the version control of files. To modify and submit files, you must first know the current status of the files, otherwise you may submit files you don't want to submit, or you may not submit the files you want to submit.

  • Modified: The file has been modified, only modified, and no other operations have been performed. This file also has two places. You can enter the temporary staged state through git add, and use git checkout to discard the modified state and return to the unmodify state. This git checkout That is, take out the file from the library and overwrite the current modification!
  • Untracked: Untracked, this file is in the folder, but it is not added to the git repository, and does not participate in version control. The status changes to Staged by git add.
  • Staged: Temporary status. Execute git commit to synchronize the changes to the library. At this time, the files in the library and the local files become consistent again.
  • Unmodify: The file has been stored in the library and has not been modified, that is, the content of the file snapshot in the version library is exactly the same as that in the folder. This type of file has two places, if it is modified, it becomes Modified. If you use git rm to move it out Repository, then becomes Untracked file
#查看指定文件状态
git status [filename]
 
#查看所有文件状态
git status

Basic command operation

Git commonly uses the following 6 commands: git clone, git push, git add, git commit, git checkout, git pull
Insert picture description here

Establish a connection with a remote warehouse to upload code

git clone https://gitlab.com/testgitlablisten/test.git

Then the remote project will be downloaded in your current directory.
At this point you cdenter the current project

Then upload your code

git add README.md
git commit -m "add README"
git push -u origin master

If it is to upload other code of this project, you can directly cd to the directory you need, and then execute the above command to upload the code.

Branch management

Create branch command:

git branch (branchname)

Switch branch command:

git checkout (branchname)

List basic branch commands:

git branch

Without parameters, git branch will list your local branches.

$ git branch
* master

Delete branch:

$ git branch -d branch1
Deleted branch branch1 (was c1501a2).

Branch upload

Assuming the new branch name is branch1:

git checkout -b branch1

This command first locates the branch1 branch locally.
Then:

git add .
git commit -m “你想说的注释”
git push -u origin branch1

git add. Means that everything in this directory is add

Branch merge

Back to main branch

git checkout master

merge

git merge branch1

Then go back to the master branch and proceed

git push -u origin master

View and withdraw

View historical submission records.

git log

In order to obtain the version number that needs to be rolled back to:

For example, the version number to be rolled back to is: aa909cff2239536df14820fe086d96305b24e9f1.

by:

git reset –soft <版本号>

Reset the submission to the specified version to achieve the purpose of canceling the submission, such as:

git reset --soft aa909cff2239536df14820fe086d96305b24e9f1

Then, confirm whether the revocation is successful through git log:

git log

Guess you like

Origin blog.csdn.net/Shangxingya/article/details/114896125