ArticleDirectory
- Three States of a file
- View help
- Create a git Repository
- Submit update
- Delete an object
- View submission history
- Graphical log interface gitk
- Cancel operation
- Use of remote version Library
Http://omiga.org/blog/archives/1896
What is git?
Git is a distributed version control system (DVCs ).
What is the relationship between git and SVN?
Most people may be more familiar with SVN. SVN is a centralized version control system (CVCs ), in CVCs, there will be a server that centrally manages versions. All coworking personnel connect to the server through the client,Check out the latest file or submit an update. CVCs has two major disadvantages in collaborative development:
- In case of server downtime, the entire collaboration cannot be performed because it cannot be performed at this time.CodeUpdate submission. Of course, the latest Code cannot be checkout.
- If the server data is lost, the entire version data will also be lost, unless you deliberately back up the server for Version Management
DVCs provides a better solution for these two major problems:
First, DVCs allows you to conveniently manage versions locally, just as if you have a local version management server. You can choose to push the local version to the Unified Version Management Server when appropriate.
Secondly, DVCs extracts the complete image of the entire code repository every time, which is equivalent to backing up the entire code repository. In this way, even if the version management server encounters an accident, it can be easily restored using any local repository. Combined with the local version management function, you can still work safely when the remote Version Management Server fails. When the remote server recovers, submit your local version.
Directly record snapshots, rather than compare differences
Git only cares about whether the overall file data changes, while most other systems care about the specific differences in the file content and record these differences. Git is more like a micro-File System. It stores the snapshot of the updated file and creates an index for it.
CVS, subversion, perforce, bazaar, etc.) What files are updated each time, and what lines are updated.
Git saves the file snapshot for each update
How to start?
You need to install a git client to start using git. You can use msysgit as your git client on Windows. msysgit contains a command line tool git bash and a GUI tool git GUI. For those who are used to tortoisesvn, the git gui may be too simple and it doesn't matter. After installing msysgit, you can still install tortoisegit to achieve a smooth transition from SVN to git. Tortoisegit is not introduced separately. This article mainly introduces using git bash for version management. After you are familiar with GIT Bash, I believe you will have a deeper understanding of tortoisegit.
After msysgit is installed, run git Bash. Before starting all the work, we need to make some configuration first. Now we only need to do some basic configuration. The detailed configuration will be discussed later:
# Username $ git config -- global user. Name'Oga'
# Email $ git config -- global user. Email'Omiga @ sample.com'
# Text editor. The default value is Vim $ git config -- global core. Editor Vim # differential analysis tool $ git config -- Global merge. Tool vimdiff.
Because each commit of git records committer information, after completing the above configuration, run the CD command to enter any file directory, and then use the GIT init name to initialize a git version library.
$ CD/D/ohmygod $ git init
In this way, any changes under the ohmygod directory are managed by the GIT version library. Create a ReadMe file in this directory, and then run the GIT status command. You will see the GIT prompt that the README file is in the "untracked Files" list, and provides the "(use git add <File>... To include in what will be committed. In this case, you can use git add readme name to add the README file to the temporary storage area.
$ Git add readme
Execute git status again and you will see the prompt message "New File: readme. Continue to execute git commit readme-m "create readme"
$ Git commit readme-m"Create readme"
In this way, the README file is submitted to the local version library to complete the process from file creation to submission:
- Git status-check the status of the GIT version Library (this step is not required, but it is a good habit)
- Git add <File>-submit the file to the temporary storage area (usually after the GIT status command, you can clearly see the file change information, determine which files need to be submitted to the temporary storage area)
- Git commit-submit changes (only files in the temporary storage zone will be submitted). directly use the GIT commit command to call the submit description window.
Three States of a file
Files tracked by git are in three states:
- Modified files
- Staged Files Added to the saved area by running the GIT add command
- Committed files submitted by using the GIT commit command
Working directory, temporary storage area, and local warehouse
View help
$ Git help <verb> $ git <verb> -- help $ man Git-<verb> # unavailable in Windows
For example, view the help information of the init command:
$ Git help init $ git init -- Help
Git basic operations create git version library initialize git version Library
The GIT init name will create a new version library in the current directory.
Clone git version Library
Clone the version library from your git version server to your local machine, or clone the code library of an open source project from GitHub. In this case, you need the GIT clone command:
# Git clone URL [newname] # clone to current directory $ git clone https://github.com/octocat/Spoon-Knife.git# new directory in current directory knife clone spoon-knife directory $ git clone https://github.com/octocat/Spoon-Knife.git knife
Both git init and git clone will create a git version library that contains the. Git directory locally.
Submit update
You can create a local git version library using the GIT init name or git clone command. After the version library is created successfully, you can save files locally and submit updates.
In git, you can use git status to view the file change information, but this information is more summary. To obtain more detailed change information, run the GIT diff command:
# View changes to unsaved files (compared with the last saved/submitted) $ git diff # view changes to saved files (compared with the last submitted) $ git diff -- cached # view changes to any version in the version library $ git diff 2bd094a # view changes between any two versions $ git diff 2bd094a 78ab3d1 # specific to a file $ git diff 2bd094a 78ab3d1 readme
Diff adds that if no unsubmitted files exist in the temporary/saved areas, the latest version of diff will be compared with the previous version.
After the file changes are clear, you can use git commit to submit files in the temporary storage area.
To submit an unsaved file, run the GIT commit-a command:
$ Git commit-a-m'All changes'
Delete a file from the GIT Repository
You can use git Rm <File>:
$ Git RM myfile
Of course, you can also manually delete the files in the file directory. The difference between the two is that using the git rm command is equivalent to manually deleting the files and then using the Add command to add the changes to the temporary storage area.
Delete a file in the temporary storage area
$ Git reset head <File>
Version rollback
If you want to cancel a certain (several) commit and roll back to a specific version, you can use git reset [-- mode] <commit>:
$ Git reset [--(Mixed | soft | hard
)] Cec8506
- -The default mixed mode is used to roll back to the unsaved status after a submission.
- -Soft is rolled back to the temporary storage status after a submission.
- -Roll Back to the complete state of a certain submission and discard all modifications after submission.
File rename
Git does not track Manual file rename operations in the file directory. If you manually rename a file, git considers it a delete-create operation. However, you can use the git mv command to rename the file directory ):
$ Git MV oldfile newfile
View submission history
This is an operation with a very high level in git. It also has powerful functions to view the submission history and provides various filtering and output format customization functions.
Run the GIT log command, and you will see a detailed commit log:
Git-Log
# Of course, you can view only a specific version of $ git log fd0a1b2
The information content is well understood. Focus on the 40-character string after the first line of commit. This is the corresponding SHA-1 value for this commit. In git), file (BLOB), directory (tree), tag to generate a unique SHA-1 value, git is based on this to learn the file or directory changes, because the SHA-1 values calculated by these four types of objects are unique, you can also directly use the SHA-1 value to refer to the corresponding objects. For example:
$ Git show bdd3996 # view a specific file in a specific version $ git show bdd3996 readme
Git Log also has many Command Options to customize historical records
Option |
Description |
-(N) |
Show only the last n submissions |
-Since,-after |
Only the submission after the specified time is displayed |
-Until,-before |
Only the submission before the specified time is displayed |
-Author |
Only displays submissions related to the specified author |
-Committer |
Only displays submissions related to the specified submitter |
-Reverse |
Display in reverse chronological order |
-P |
Display the differences between each update in patch format |
-Stat |
Displays the statistics of each file update. |
-Reset stat |
Show only the last row modification in-Stat, add and remove statistics |
-Name-only |
Only the modified file list is displayed after the information is submitted. |
-Name-status |
Display the list of new, modified, and deleted files |
-Abbrev-commit |
Only the first few characters of SHA-1 are displayed, not all 40 characters. |
-Relative-Date |
Use a shorter relative time (for example, "2 weeks ago ") |
-Graph |
Display branch merge history in ASCII format |
-Pretty |
Display Historical submission information in other formats. Available options include oneline, short, full, Fuller, and format (followed by the specified format) |
You can combine the preceding options to customize more personalized log information, for example:
$ Git log -- committer'God'
-- Cmdstat -- pretty = oneline
This command displays statistics submitted by God in single-line mode.
$ Git log-P-5
Displays the last five submissions and their differences
In addition, git log-graph is also fun. I used git log-graph to view the GIT project log, which is very spectacular.
Git graph log
-Pretty = format
The-pretty = format option is described separately. You can use formats and placeholders to customize a more personalized display format.
Option |
description |
% H |
complete hash string of the submitted object (COMMIT) |
% H |
brief hash string of the submitted object |
% T |
complete hash string of the tree object |
% T |
short hash string of the tree object |
% P |
complete hash string of the parent object (parent) |
% P |
brief hash string of the parent object |
% An |
author's name |
% AE |
the author's email address |
% ad |
author's revision date (you can use the-date = option to customize the format) |
% ar |
the author's revision date, displayed in the previous manner |
% CN |
name of the submitter |
% Ce |
the email address of the submitter |
% Cd |
submission date |
% Cr |
submission date, displayed in the previous manner |
% S |
submission description |
$ Git log -- pretty = format:'% H by % Ce at % Cd'
This command will display logs in the format of "Brief SHA-1 by submitter at submission time"
Graphical log interface gitk
Gitk naming will enable the graphical log Interface
Gitk
Cancel operation
We have already introduced how to use git reset to cancel files in the temporary storage zone and roll back the entire version. However, if you only want to restore a file, you need to use checkout-<File> Name:
$ Git checkout -- readme
It uses the latest submitted version for restoration only when the file to be modified has not been saved. If the file has been saved for temporary storage, you must first use git reset head <File> to delete the file from the temporary storage area, and then use this command.
Last modification submitted
The GIT commit-amend name allows you to re-edit the last submitted information. It is equivalent to submitting again, overwriting the previous submission.
Use of remote version Library
Although git can also facilitate version management locally, it usually needs to be used for version maintenance in a remote repository for multi-person collaboration or multi-site operations. In the content of the previous version library, the clone name is already used to interact with the remote version library. After cloning the remote library, a remote library named origin is automatically created, you can use the GIT remote-V name to view detailed information about the remote database.
$ Git remote-V
In practice, we may need to frequently interact with one or more remote databases. A better way is to use an alias to save the remote database. The method for adding a remote repository in git is simple:
# $ Git remote add <Name> <remote-URL> $ git remote add pro-Git https://github.com/progit/progit.git
In this way.
Push
After you complete local work and submit the changes to the local version library, you can use push to push the local submission to the remote Repository:
# $ Git push <remote-Name> <branch-Name> $ git push pro-Git master
By default, origin and master are used as the names of the remote repository and local branches.
Of course, you can also push the local branch to the remote warehouse as a branch:
# $ Git push <remote-Name> <local-branch>: <remote-branch> $ git push pro-Git master: Git-branch
If <local-branch> is empty, the remote branch is deleted:
$ Git push pro-Git: Git-branch
The above command will delete the "Git-branch" branch in the remote Repository
Fetch and pull
The fetch and pull commands both capture a remote warehouse to the local. The difference is that fetch only captures the remote warehouse to the local for subsequent operations. In addition to capturing the remote warehouse to the local, it also tries to merge with the current local branch.
Different from clone, clone copies a version repository to the local device. If a version repository already exists, it is replaced by the cloned repository. Both fetch and pull need to be operated under the conditions of an existing local warehouse. They cannot be used to create a local warehouse. That is, they must first use git init or git clone before they can use fetch and pull.
Delete remote Repository
$ Git remote-D <remote-Name>
To be accurate, this only deletes the local alias of the remote repository, rather than deleting the GIT repository on the remote server.
Rename a remote Repository
$ Git remote rename oldname newname
Git alias
Although many names in git are easy to remember, it is a waste of time to manually enter these commands each time, and some command options are very lengthy, in this case, you can use aliases to simplify command input.
The alias is a configuration item, so you need to use the GIT config command. For example, you can configure the alias "cob" for the "checkout-B" command"
$ Git config -- Global alias. Cob'Checkout-B'
Configure the alias "Cam" for "Commit-a-m ":
$ Git config -- Global alias. Cam'Commit-a-m'
Configure the alias "LOL" for the log command "log-pretty = oneline-graph" displayed as a single line image ":
$ Git config -- Global alias. Lol'Log -- pretty = oneline -- graph'
So far, the GIT basics have ended. You can use git for daily code management and maintenance. The next advanced article will focus on Branch, git configuration, git principles, GitHub, and so on.