What is Git?
Git is a distributed code versioning system that makes it easy to manage code versioning with Git
In the process of writing code, it is easy to upload or download code, code updates, and then upload to git
With Git, it's easy to see the revision history of your code
You can use Git on Linux, Windows, Mac
For a tutorial on Git, you can refer to the following articles
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
Under Windows, you can use Msysgit, which can be downloaded from the following URLs: http://msysgit.github.io/
Under Ubuntu, you can use sudo apt-get install git to download the latest version of the Git tool
The first time you use Git, you have to execute the following two commands:
git config--global user.name "Wenjs"
git config--global user.email "[Email protected]"
Learn how to use Git on Linux ubuntu
1. First, create a directory for Git practice, and execute the following command:
Mkdir-p/opt/git_test
chmod 777/opt/git_test
Cd/opt/git_test
2. After creating the Git_test directory, execute git init in the directory, indicating that the directory is initialized,
After execution, the directory is a local, empty git repository with a hidden folder. Git
This folder contains some information about the local repository and does not attempt to change the files in that directory, otherwise it destroys the local repository
3. In the Git_test directory, create a hello.c folder, write the code, then save it and prepare to upload it to the git test
4. Once you have finished editing the file, you can upload the file to the GIT repository using the following command
git add hello.c means adding hello.c to the local git repository, which can be used repeatedly
Git commit-m "This is a test hello.c file" means using the git commit command to submit the files to the Git repository, the-m parameter of the command indicates the following is a description of the file, strongly recommended to add a file description
With the git add command, you can add multiple files and use the git commit command to commit to the Git repository once, meaning to add multiple files, one commit
5. If the hello.c file has been modified but not yet submitted to the Git repository, you can use the following command to view the file status
Git status This command indicates that you can view the current file status to see if the file has been modified
git diff This command indicates that you can view the contents of a file that has been modified
6. After modifying the completed file, you can also use the git add and git commit two commands to add and submit
The great thing about 7.git is that you can use commands to view the history of a version at any time, using the following command:
git log This command indicates that the submitted log records can be viewed from near to far
If the command output too much information, you can use git log--pretty=oneline
[Email protected]:/opt/git_test# git log--pretty=oneline
F7F74F3D08F42872C85E71BC23057DA0E3B275C2 This is a update hello.c file Wenjs
86a76e63fb5aa8cbba4a3d2fe35b75ff081efa8f This is a update hello.c file
50b070643f12ee130e5266c5f7dc3cb1e4470c32 This is a test hello.c file
A large string of previous strings representing the version number (commit ID)
8. Using the git command to go back to the previous version, you must first know what the current version number is.
In Git, the current version number with HEAD, the previous version is expressed in head^, the previous version with head^^, the last 100 versions are represented by head~100
9. Use the following command to go back to the previous version
git reset--hard head^
The--hard parameter indicates:
Use git log to see that the latest version of the file is missing.
10. If you want to revert to the latest version, you can look at the history of the terminal, view the latest version of the commit ID number, using the following command
git reset--hard f7f74f--Indicates recovery to ID number f7f74f ... Document, the ID number does not need to be filled out, Git will automatically find
11. If the terminal record cannot be turned upside down, git can also use the following command to view the GIT commands that have been used
git reflog This command to see every git command you've ever used
12. If you want to discard the changes to the workspace, go back to the previous state, you can use the following command
Git checkout--hello.c
13. If the contents of the workspace are modified and add to staging area, you can use the following command to undo the operation
git reset HEAD hello.c git reset command can either rollback the version or recall the contents of the staging area to the workspace
14. If the local file is deleted and you want the local repository to be deleted at the same time, you can use the following command:
git rm hello.c This command to delete files from the repository
15. If the file is deleted locally, but the repository has a file, you can use the following command:
Git checkout--hello.c git checkout is actually a one-click Restore tool that you can use git checkout back to a previous version, whether you're deleting a restore or modifying it
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
How to create and use a remote repository for git
Using the GitHub site, you can create your own remote repository and register a github account first
Since the local git repository and the remote Git repository are transmitted using SSH protocol encryption, it is necessary to check the home directory of the PC to see if there is a. SSH folder
If not, you can use the following command to create an SSH key
Ssh-keygen-t rsa-c "[Email protected]"
e-mail address to their own mailbox, all the way back, the process does not need to set the password, successfully completed, will be found in the main directory. SSH directory with Id_rsa and id_rsa.pub two files
These two files are key pairs of SSH key, where Id_rsa is the private key, cannot be exposed, id_rsa.pub is the public key, can be exposed
1. Using a registered account, log in to the Git hub, open the "Accounts settings", "SSH Keys" page
2. Click "Add SSH Key", fill in any title, paste the contents of the Id_rsa.pub file in the Key text box
3. Click "Add Key" and you should see the Key already added.
With these steps, you can create a remote repository.
Suppose you've created a local repository locally, and you want to create a remote library on GitHub, and have the two warehouses synchronize
1. Log on to GitHub and then, in the upper right corner, find the "Create a new Repo" button to create a new warehouse
2. In the repository name fill in the Git_test, the other remains the default settings, click the "Create Repository" button, the successful creation of a new Git repository
3. How to associate a local repository with a GitHub repository, go to the local git repository and execute the following command:
git remote add origin [email protected]:wenjs0620/git_test.git
Where wenjs0620 is the name of his GitHub account, and the remote library name is origin and can be modified, but this is the default for GitHub
4. You can use the following command to push the contents of the local repository to the remote repository
Git push-u origin master pushes the current branch master to the remote
Since the remote library is empty, the first time we pushed the master branch, we added the-u parameter,
Git will not only push the local master branch content to the remote new master branch,
It also associates the local master branch with the remote master branch to simplify the command at a later push or pull
5. After the above steps, from now on, as long as the local git commit commits, you can use GIT push origin master for remote push
When you first connect to GitHub using Git's clone or push command, you get a warning:
The authenticity of host ' github.com (xx.xx.xx.xx) ' can ' t be established.
RSA key fingerprint is xx.xx.xx.xx.xx.
Is you sure want to continue connecting (yes/no)?
This is because Git uses an SSH connection, and the SSH connection is the first time the GitHub server's key is verified,
You need to confirm that the fingerprint information for GitHub key is really from the GitHub server, enter Yes to return.
Git will output a warning telling you that you have added GitHub key to a trust list on this machine:
warning:permanently added ' github.com ' (RSA) to the list of known hosts.
This warning will only appear once, and there will be no warning after the operation.
To summarize:
To associate a remote library, use the command git remote add origin [email protected]:p Ath/repo-name.git
Once associated, use the command Git push-u Origin master to push all the contents of the master branch for the first time
Thereafter, after each local commit, whenever necessary, you can use the command GIT push Origin master to push the latest changes
--------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------
How to clone in a remote library
1. Log in to GitHub and create a new repository named Git_skills
2. Tick initialize this repository with a README,
So GitHub will automatically create a readme.md file for us. Once created, you can see the readme.md file
3. Now that the remote library is ready, the next step is to clone a local library with the command git clone
git clone [email protected]:wenjs0620/git_skills.git
With the three steps above, you can clone from a remote library to a local library.
In fact, GitHub gives more than one address, and you can use an address like https://github.com/xxxxxx/xxxxxx.git.
GIT supports multiple protocols, and the default git://uses SSH, but other protocols such as HTTPS can also be used.
In addition to the slow use of HTTPS, but also the biggest trouble is that each push must enter a password,
However, in some companies that only open HTTP ports, you cannot use the SSH protocol and only use HTTPS.
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
git Branching and management
A git branch is used to work on a branch without affecting the main branch, and team members work on their own branches without affecting each other.
When the work is done, you can merge your branches into the main branch
GIT Branch Management Policy
Typically, when merging branches, GIT uses fast forward mode if possible, but in this mode, the branch information is discarded when the branch is deleted.
If you want to force the fast forward mode to be disabled, GIT will generate a new commit at merge, so you can see the branching information from the branch history.
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
git tag Management
When we publish a version, we usually start with a tag in the repository, so that it's the only version that determines the time of the tag.
Any time in the future, the version of a tag is taken out of the historical version of that tag. Therefore, the tag is also a snapshot of the repository.
Git's tag is a snapshot of the repository, but it's actually a pointer to a commit
(like a branch, right?) However, the branch can be moved and the label cannot be moved, so creating and deleting tags is instantaneous.
1. First, switch to the branch that needs to be labeled, using the following command:
git checkout Master
2. Use the following command to label:
git Tag v1.0
The default tag is on the latest commit
3, if you want to label the history of the version, you can use the following methods:
git log--pretty=oneline--abbrev-commit view ID numbers for all versions
git tag v0.9 <commit_id> tag a specific version
git tag View all versions of tags
4. To view the label information, you can use the following command:
Git show <tagname>
5. Create a label with a description, specify the label name with-A,-m to specify the description text:
git tag-a v1.0-m "This is the first tag"
6. You can delete the label by using the following command:
Git tag-d <tag_name>
7, the label will only be saved locally, to be pushed to the remote, you can use the following command:
Git push origin <tag_name> push a specific tag to a remote
Git push origin--tags push all non-push tags to remote
8, if the label has been pushed to the remote, to delete the label, you can use the following command:
git tag-d <tag_name> Delete local tags first
git push origin:refs/tags/v0.9 from remote removal
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
Use Git to ignore special files
Sometimes you have to put some files in a git working directory, but you can't commit them, like a configuration file that keeps a database password, and so on.
Every time git status shows untracked files ..., children with obsessive-compulsive disorder must be uncomfortable with their shoes.
Fortunately, Git takes into account how everyone feels, and it's easy to solve.
Create a special. gitignore file in the root directory of the Git workspace, and then fill in the file name you want to ignore, and git will automatically ignore the files.
There is no need to write from scratch. gitignore file, GitHub has prepared a variety of configuration files for us, just a combination to use.
All profiles can be viewed directly online: Https://github.com/github/gitignore
The principle of ignoring files is:
1, ignoring the operating system automatically generated files, such as thumbnail images;
2, ignoring the compilation generated intermediate files, executables, etc., automatically generated files are not necessary to put into the repository, such as Java compiled by the. class file;
3. Ignore your own profile with sensitive information, such as the configuration file that holds the password.
The last step is to commit the. Gitignore to Git, and it's done!
Of course, the standard for Gitignore is that the git status command does not say working directory clean.
Children's shoes with Windows Note that if you create a new. gitignore file in the Explorer, it will be very mentally retarded to prompt you to enter the file name,
But in the text editor "save" or "Save as" can save the file as. Gitignore.
There are times when you want to add a file to Git, but find that it is not added because this file was ignored by. Gitignore:
If you do want to add the file, you can force the add to Git:git add-f with-f app.class
Or you find that maybe. Gitignore write a problem, need to find out which rule is wrong, you can use the git check-ignore command check:,
Git check-ignore-v app.class
. Gitignore:3:*.class App.class
Git will tell us that. Gitignore's 3rd line of rules ignores the file, so we know which rule to revise.
When you omit certain files, you need to write. Gitignore
The. gitignore file itself is put into the repository and can be versioned for. Gitignore!
-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
Summarize how Git is used:
Use Git for the first time:
git config--global user.name "Wenjs"
git config--global user.email "[Email protected]"
Create a new directory locally: Mkdir-p git_test/
Initializing the directory: Git init git_test
Add file to local repository: git add hello.c
Submit file to local repository: Git commit-m "This is a git test file"
View warehouse status: Git status
View file modifications git diff hello.c
View historical version: Git log--pretty=oneline
Revert to the latest version: Git reset--hard f7f74f
Discard workspace Modifications: git checkout--hello.c
Undo Staging Area Modify: Git reset HEAD hello.c
Remote sync, create key pair: ssh-keygen-t rsa-c "[Email protected]"
Remote and Local association: GIT remote add origin [email protected]:wenjs0620/git_test.git
Content pushed from local to remote: Git push-u Origin Master
From remote clone to Local: Git clone [email protected]:wenjs0620/git_skills.git
View branches: Git branch
Create a branch: Git branch <name>
Switch branches: git checkout <name>
Create + switch branches: git checkout-b <name>
Merge a branch to the current branch: git merge <name>
Delete branch: Git branch-d <name>
View Branch merge diagram: git log--graph
Create a new label that defaults to head, or you can specify a commit id:git tag <name>
Specify tag information: Git tag-a <tagname>-M "Blablabla ..."
Tag with PGP signature: Git tag-s <tagname>-M "Blablabla ..."
View all tags: git tag
Push a local tag: Git push Origin <tagname>
Push all the local tags that have not been pushed: Git push origin--tags
You can delete a local tag: git tag-d <tagname>
Delete a remote tag: git push origin:refs/tags/<tagname>
Version management tools git usage Summary