This is the 15th day of my participation in the August More Text Challenge

Understand Git’s four working areas correctly

Workspace: a Workspace, that is, the folder where the project resides after the project is cloned to the local PC.

Index/Stage: staging area where files that are changed in the workspace are stored. Using Git add while operating commits all local changes to the staging area

Git commit-m ‘Commit description’. This contains the data you have committed to all versions, with HEAD pointing to the latest version in the Repository

Remote Git repository (Git repository) Remote Git repository (Git repository) Remote Git repository (Git repository) Git push Origin specifies the remote branch name.

Git’s general workflow

Git works like this:

  1. Add and modify files in the working directory
  2. Add or modify files in the working directory to the staging area
  3. Commit the files for the staging area to your local Git repository
  4. Upload the code for the local repository to the remote repository

Four states of a file

Version control is the version control of a file. To modify or submit a file, you must first know the current state of the file. Otherwise, you may submit a file that you do not want to submit now, or the file to be submitted is not submitted.

GIT does not care about the specific differences between two versions of a file, but whether the file as a whole has changed. If the file has changed, a snapshot of the new version will be generated when the file is added and committed. The way to determine whether the file as a whole has changed is to use the SHA-1 algorithm to calculate the file’s checksum.

  • Untracked:Not traced, this file is in the folder, but not added to git repository, does not participate in version control. throughgit addThe state went into passage.
  • Unmodify:The file has been stored and has not been modified. That is, the snapshot content of the file in the version library is the same as that in the folder. A file of this type can be Modified in two ways. If it is Modified, it becomes Modified. If you are usinggit rmMove the library, then becomes an Untracked file
  • Modified:The file has been modified, just modified, and nothing else has been done. This file also has two places to go, throughgit addPassage Ten can be entered into temporary state and usedgit checkoutIt discards the modification and returns to the Unmodify state, this onegit checkoutThat is, take the file out of the library and overwrite the current change
  • Staged:Temporary status. performgit commitThen, the changes are synchronized to the library. In this case, the files in the library and the local file become the same and the file is in the Unmodify state. performgit reset HEAD filenameCancel temporary storage with the file status Modified.

Flow chart of file state transitions

The flow chart of these four states is as follows:

operation Change the file status to XXX
The new file Untracked
Use the add command to add the new file to the staging area Staged
Use the commit command to commit the files in the staging area to the local repository Unmodified
If a file in the Unmodified state is modified Modified
Remove a file in the Unmodified state Untracked

Therefore, Git manages files in three states: Modified, staged, and Committed

The new warehouse

  1. Create remote libraries (preferably empty)

  2. Create a new folder locally (preferably with the same name as the remote repository)

mkdir testgit
cd testgit
Copy the code
  1. Git is the repository that git uses to keep track of repositories
git init  
Copy the code

Note:

Create a new directory and initialize it as a Git code base (Git init [project-name]).

  1. Associated remote library

Git remote add origin(default origin, modifiable) branch_Name(default master) URL

git remote add origin [email protected]/xxx.git
The two locations do not need to have the same repository name, because by executing this in the local repository directory
The # command (which contains the name of the remote library) already links the two
Copy the code
  1. The new file
touch README.md
Copy the code
  1. Add files to the local library
git add .
Copy the code
  1. Commit files to local libraries
git commit -m "MSG (submit log)"
Copy the code

Note:

Git commit -am “MSG”

  1. Push all content from the local library to the remote library
git push -u origin master 
With the -u parameter (push and associate) Git will not only push the local master branch to the new remote master branch,
It also associates the local master branch with the remote master branch
Git push origin master git push origin master
Copy the code

Clone remote repository

git clone [email protected]:xxx/xxx.git 
Copy the code

Common View Commands

Check the current status of the warehouse
git status 

Every time you run a command, start with the first seven digits of the version number after the command was executed
git reflog
Copy the code

Compare different files -diff

What changes are made to the file, compare workspace and stagingGit diff file nameCompare the staging area with the latest local version library
git diff --cached  filename

Compare workspaces with the latest version
git diff HEAD filename

# compare the workspace with the specified commit - ID
git diff commit-id  filename

# compare the difference between the staging area and the specified commit-id
git diff --cached commit-id  filename

See a list of the differences between the two versions of the file, including the number of lines changed and the images added and deletedGit diff version 1 version 2 --stat
# git diff bf326a16 276b4a14 --stat
# docs/v2.8.76 / README. Md | 10 -- -- -- -- -- -- -- -- -- --
# docs/v2.8.76 / mysql_ddl. SQL | 6 -- -- -- -- -- -
# 2 files changed, 16 deletions(-)
Copy the code

Common Modification Commands

Add, but do not commit
git add readme.txt

Commit only after add.
# "change file ->add file -> change again -> Commit", the second change is invalid, will not be committed, only the first change will be successfully committed.
git commit -m "Submit description"
Copy the code

Undo changes and version rollback


Go back to the previous version
The previous version is HEAD^, the previous version is HEAD^^,
Of course, it's easier to write 100 ^ in the next 100 versions, so write HEAD~100
git reset --hard HEAD^

Fall back to the specific COMMIT ID
git reset --hard 0b3a6dbf02c8d03969577cb7fe0e200cf8303c63


# change the status of the workspace to "unsaved"Git reset HEAD File name# Kill the unprovisioned (i.e., no add), or discard the workspace and return to the provisioned stateGit checkout -- file name# git checkout -- pom.xml


Copy the code

Git checkout — git checkout — git checkout — git checkout — git checkout Git checkout — git checkout — git checkout — git checkout

From a distant warehouse to the local


# pull code from remote branch,
# Disadvantage: Pulls all branches of the current project commit. It's not necessary. If there are a lot of people working on the current project,
# Then there will be many branches, and other branch submissions will be pulled down
git fetch	


# pull merges code directly, causing conflicts and so on
git pull
# git fetch + git merge == git pull
Copy the code

Operation of remote warehouse -remote

Delete the remote libraryGit remote remove Specifies the name of the remote repositoryGit remote remove origin

Add another remote libraryGit remote add Specifies the name of the remote repositoryGit remote add origion https://..

Change the name of the remote libraryGit remote rename git remote renameGit git rename origin origin1

Check the remote library information
git remote 

Select * from remote library; select * from remote library
git remote -v

# change remote address
git remote set-url origin [email protected]:username/repository-name.git
Copy the code