Version control

A system that records changes in the contents of a document for future reference of revisions to a specified version

Centralized version control system

The biggest disadvantage is that if the services of the shared warehouse (the central server) fail in a single point of failure, they cannot commit and cannot work together. The typical representative of this system is SVN.

Distributed version control system

It can solve the single point failure of central server in centralized control system. It will clone a remote warehouse to the local, some remote warehouse failure, also will not affect the local warehouse files. The typical representative of this system is GIT.

GIT features

Record snapshots directly, not differences

Each version of the SVN records the difference between the file and the initial version

Each GIT release records a snapshot of the file. At each submission, a snapshot is created for all files at that time and the index of the snapshot is saved. If the file is not modified, the snapshot is not created and only a link to the previously stored file is kept

Almost all operations are performed locally

Most operations only require access to local resources and do not require information from other computers on the network. This is why Git is so fast to use.

GIT area

Workspace: is the directory that can be seen on the computer

Git: a hidden directory in your workspace

Git /index:.git/index

Files from the workspace are added to the staging area and then from the staging area to the repository

GIT GIT

The warehouse operation

Access to the warehouse

Git clone <url> < local folder name > Clone an existing Git repositoryCopy the code

Look at the warehouse

Git remote show origin git remote show originCopy the code

Viewing file Status

Git status Basic changes to the status of the file, as well as the committed statusCopy the code

The temporary change

Git add < directory/filename > For new files, you need to trace them, that is, add them to the staging area; For files that have been traced, changes have occurred, and the modified files are temporarily savedCopy the code

View staged and unstaged changes

What is not temporarily stored at present? Which ones are provisioned but not yet committed? Git status allows you to see which files are involved, but not how files have been modified, so you need to use git diff.

Git diff view differences between workspace files and staging snapshots git diff --stage view differences between staging and the last file committedCopy the code

Submit the updates

Git commit <-m commit description > commit a snapshot of the hold area to the local repository. Git commit -- Amend creates a commit record every time you commit. If the first commit is made, the commit record is made, and the same file is modified later, However, you do not want to generate a record of submission, but want to merge with the previous record into a record of submission, you can use the following command, enter the shell and edit the description to save and exit. Amend doesn't have to be modified to work, and can be used to modify the commit description without any changes. This command causes the last committed record to never exist in the repository.Copy the code

Remove the file

Git rm -- Cache removes only snapshots from the staging area and keeps the files in the workspace. This is very useful when unwanted files are accidentally committed. You can use this command to preserve workspace files, delete staging snapshots, and delete files from the local repository for subsequent commitsCopy the code

View historical submission records

Git log --pretty=oneline Git log --pretty=oneline --graph --graph allows the log to show branch and merge historyCopy the code

undo

Undo changes to files

Git checkout -- File path This method removes changes that have been modified but have not been temporarily storedCopy the code

Undo the temporary file

Git reset deletes files from the staging area but keeps workspace files. Git reset --hard deletes files from the staging area and from the workspace. With caution.Copy the code

The label

Check the label

Git tag Lists all tags. Git show Tag name Lists details about a specified tagCopy the code

tagging

Labels are classified into lightweight labels and note labels. Lightweight labels cannot use the -A / -M options

Git tag -a tag name [version number] -m Tag Description Git tag a lightweight tag with the version number. If the version number is not specified, git tag a lightweight tag with the latest version number. -A indicates that a label is added, and -m indicates that a label description can be added. Note labels are usually used.Copy the code

Push to remote repository

Git push origin -- Tags push all tags that are not in the remote repository to the remote repositoryCopy the code

Remove the label

Git tag -d git push origin :refs/tags/ tag name Delete a local tag and push it to a remote git push --delete the tag name directly delete the remote tagCopy the code

See the branch

Git branch View the list of local branches that are checked out. Git branch -r View the list of remote branchesCopy the code

The new branch

Git checkout -b < branch name > create a branch and switch the branchCopy the code

Push & trace branch

Git push --set-upstream origin < branch name > Git push --set-upstream origin < branch nameCopy the code

Pull remote warehouse data

Git fetch only fetches data that does not exist locally, but does not modify the contents of the working directory. You need to merge git pull by yourself, which is equivalent to git fetch+ Git mergeCopy the code

Merging branches

There are two modes for merging: fast-forward and no fast-forward

Fast-forward: directly moves the pointer to the latest submitted version

No fast-forward: Three versions generate a new commit, based on the last version of both branches and the most recent common ancestor version of both branches

Git merge < branch name > merges the "branch name" branch changes into the current branchCopy the code

Check whether the branch has been merged as master

Git branch --no-merged < < branch name > lists branches merged into < < branch name >. If no branch is specified, the current branch can be viewed. If the branch has been merged, the branch can be deleted if it is not in useCopy the code

Delete the branch

Git branch -d < branch name > Delete the local branch. Check the merge status before deleting the local branch. If the branch is not merged, an error is reported. Git push origin --delete < branch name > delete remote branchCopy the code

Rename branches

Git branch -m oldName newName Git push origin --detele oldName Git push --set-upstream Origin backup git push --set-upstream origin newNameCopy the code

rebase

There are two ways to merge content from different branches: merge and rebase

Git rebase < branch name > rebase the current branch to the "branch name" branch git checkout < branch nameCopy the code

The principle of rebasing: find the common ancestor of two branches, then change the current branch from the common ancestor to a temporary file, then point the current branch pointer to the target base of the branch named “branch name”, and finally apply the temporary file to “branch name” and move the pointer of the current branch.

The golden rule: If commits exist outside of your repository and someone else might be developing on them, don’t base them.

Storage and cleaning

If you have been working in one branch for a while, and some changes have not been committed, and you want to switch to another branch, store and then switch branches to work

Git stash list See the list of stash items stored in the stack. If the stash name is not specified, the latest stash item is applied. This command will show that the previously staged content is not recapitulated after application, but in the workspace. Git stash apply < stash name > --index This is done by adding --index to the git stash apply. Git stash drop removes the specified stash from the stack and removes it from the application stackCopy the code

The reset revelation

Reset to a version

Git reset [--mixed] < version number > restore the repository to the specified version number. --mixed can omit git reset --hard < version number > restores version libraries, staging areas, and workspaces to the specified version numberCopy the code

Reset by path

Git reset <file> Deletes a file from the staging area and keeps it in the workspaceCopy the code

Undo an operation

Git revert < version number > keeps the commit and history before and after this operation, and treats this undo as a recent commitCopy the code

Git repository is migrated to another Git repository

Migrating repositories not only preserves code, but also branches, historical commit records, and so on

Clone the bare library and mirror it

Git repository test_admin_new

Create it by yourselfCopy the code

2. Clone a bare warehouse, i.e. one without a workspace

git clone --bare git-server:/mm/test_admin
$ ls
test_admin.git/
Copy the code

3. Access the bare warehouse

$ cd test_admin.git/
Copy the code

3. Push the code to test_admin_new as a mirror

git push --mirror git-server:/mm/test_admin_new
Copy the code

Set the remote URL and push it

Git repository test_admin_new

Create it by yourselfCopy the code

2. View the remote repository

git remote -v
Copy the code

3. Set the remote URL in the local file

Git remote set-url origin git-server:/mm/test_admin_new This command changes the url configuration of the. Git /config file. Origin is based on the result obtained in step 2Copy the code

4. Push all branches to remote

git push --all
Copy the code