Git-Repo-Gerrit-2-Git Basics

Source: Internet
Author: User
Tags diff using git
The file status in the GIT working directory. All files in the working directory are in either of the following statuses: tracked or not tracked. Tracked files are files that have been incorporated into version control management. They are recorded in the previous snapshot. After a period of time, their statuses may not be updated, modified or placed in the temporary storage area. All other files are not tracked. They neither have snapshots of the last update nor are in the current temporary storage area. When cloning a repository for the first time, all files in the working directory belong to the tracked file and the status is not modified.
After editing some files, git marks these files as modified. We gradually put these modified files in the temporary storage area until all these temporary files are submitted at one time. So the file state change cycle 2-1 when using git is shown below:

The following example uses [email protected] For git operations. clone Repository: git clone I have created a new project in open source China. Run the following command to clone the repository to a local directory (go to the E:/gyrepo/directory)
Git clone http://git.oschina.net/gaoyuan/git-study.git
Then we can see that a git-study directory is automatically created in this directory, which contains several files. The. Git folder is the folder that records the entire repository.
View the File status: git status is in Git-study:
This indicates that there is nothing to modify. This command is very common.
Include files in version control: git addAssume that the current directory contains an AA directory and bb.txt and cc.txt files. If you want to include all files in the AA Directory into version control, run the following command:
Git add AA bb.txt cc.txt
In addition, the GIT add command can include new files in version control, or add modified files to the temporary storage area.

For example, I modified the readme. md file: used the vim editor to modify the file.
Vim readme. md
After modification, add it to the temporary storage area
Git add readme. md
Then run the git status Command to view the File status:
Git will prompt you what commands are used to perform operations on the file. For example, you can use git add to add the modified file to the cache, or use git checkout -- file to discard the modifications. it is recommended that you use git add to write the following files one by one. It is best not to write the folder name; otherwise, it is likely to add files that do not need to be saved to the temporary storage area.
Ignore some files:. gitignoreIn general, some files do not need to be managed by git, and we do not want them to appear in the list of untracked files. Generally, these files are automatically generated, such as log files or temporary files created during compilation. You can create a file named. gitignore to list the file modes to ignore. Let's look at a practical example: # Built application files *. APK *. AP _ # files for the dex vm *. dex # Java class files *. class # generated filesbin/GEN/# local configuration file (SDK path, etc) Local. properties # Eclipse project files. classpath. project # proguard folder generated by eclipseproguard/# intellij project files *. IML *. IPR *. iws. idea/
This is the. gitignore file automatically generated when you create an android project in [email protected.
View File changes: Add git diff to a file and save it temporarily after modification. Use git status to view the changes:
To view the updates of files that have not been saved, enter git diff without parameters.
This command compares the difference between the current file in the working directory and the snapshot in the temporary storage area, that is After modification, no changes have been saved. If you only want to view the changes to a file, use Git diff filenameYou can use Git diff -- cached filenameCommand. (GIT 1.6.1 and later versions are also allowed Git diff -- staged, The effect is the same, but it is easier to remember .) Let's take a look at the actual results: note that git diff alone only shows that there are no temporary changes, rather than the difference between this job and the previous commit. So sometimes you temporarily save all the updated files and run git diff, but there is nothing. That's why.
Submit for update: git commitThe current temporary storage area is ready for submission. Before that, be sure to confirm whether there are any modified or newly created files Git addOtherwise, the changes that have not been saved will not be recorded during submission. Therefore, use Git statusCheck whether all of them have been saved, and then run the submit command. Git commitIn this way, the text editor is started to enter the description of this submission. As you can see, the default commit message contains the output of the last running git status, which is placed in the comment line. There is an empty line at the beginning for you to enter the commit instructions. You can remove these comment lines, but it doesn't matter if you keep them. It can help you think back to the updated content. (If you think this is not enough, you can use the-V option to include each line of the change difference in the comment .) When you exit the editor, git will discard the comment line and submit the description content and this update to the repository. Note: If you do not write the submission instructions, the submission cannot be completed. you can also use the-M parameter followed by the instructions to submit an update in a command line: You can see that after the submission, it will tell you which branch (master) is currently in) submitted, what is the complete SHA-1 checksum submitted this time (463dc4f), and how many files have been revised and modified in this submission. When you submit a snapshot, it is recorded as a snapshot in the temporary storage area. Any snapshot that has not been saved is still in the modified status. You can include the snapshot in version management when you submit the snapshot next time. Each time you submit an operation, a snapshot is taken for your project. You can return to this status or compare it later. Note that if you do not write the submission information, git does not allow you to submit the information. If you want to use a command to submit the information, you can use: Git commit-m'initial commit'The text in quotation marks indicates the submission information, but this method is not recommended. We hope to submit the information as detailed as possible. Remove File: git RMTo remove a file from git, you must remove it from the list of Tracked files (specifically, from the staging area) and then submit the file. You can run the git rm command to complete this task and delete the specified file from the working directory. This will not appear in the list of untracked files. If you just manually delete the file from the working directory, you will see it in the "changes not staged for commit" section (that is, the unsaved list) when running git status:
This is the output of git status after I delete test.txt and then run git RM to record the file removal operation:
At the end of the submission, the file is no longer included in version management. If you have deleted the modified file and put it in the temporary storage area, you must use Force delete option-FThe first letter of force to prevent loss of modified content after the file is deleted by mistake. In another case, we want to delete the file from the GIT repository (that is, remove from the staging area), but still want to keep it in the current working directory. In other words, Only remove from the trail list. For example, some large log files or a pile of logs. a. After the file is compiled and accidentally included in the repository, you need to remove the trace but not delete the file so that the file can be later in. fill in the gitignore file and use the -- cached option: Git Rm -- cached readme.txt
Move file: git mvUnlike other VCs, git does not track file movement operations. If a file is renamed in git, the metadata stored in the repository does not show that this is a renaming operation. However, git is very clever, and it will deduce what happened. As for how it is done, let's talk about it later. In this case, you will be confused when you see the git mv command. To rename a file in git, you can do this: Git MV file_from file_toFor example:

View submission history: git logAfter submitting several updates or cloning a project, you can use Git logCommand to view.
By default, if no parameters are used, git log lists all updates based on the submission time, with the most recent updates at the top. Each update has a SHA-1 checksum, the author's name, email address, and submission time. The last section shows the submission instructions. Git log has many options to help you search for submissions you are interested in. Next we will introduce some of the most commonly used. We often use -POption. -2Only the last two updates are displayed:
This option can be used when performing code reviews or when you want to quickly browse the changes made to updates submitted by other collaborators. If the number of submissions is large, one screen won't fit, You can use J and K to move up and down to view the log. you can exit the log after entering an uppercase ZZ value..
Submit the modification: git commit -- AmendSometimes, after we submit the file, we find that a few files are missing, or the submitted information is wrong. To cancel the submission operation, you can use the -- amend option to resubmit:
Git commit -- Amend
This command will be submitted using the snapshot of the current temporary region. If no changes have been made after the submission, directly run this command, which is equivalent Have the opportunity to re-edit the submission instructionsBut the file snapshot to be submitted is the same as the previous one. After you start the text editor, you will see the description of the last submission. After you confirm that there is no problem with the editing, save and exit. Then, you will use the new submission description to overwrite the submitted error.
If you forget to save some modifications during the submission, You can first complete the temporary operation and then run -- Amend to submit, such as the following operations:
Git commit-m'initial commit' Git add forgotten_file Git commit -- Amend
The preceding three commands generate only one commit, and the second commit command fixes the first commit content. For example, I submitted the following operation:
Then I modified a file and added it to the temporary storage area:
Then git commit -- Amend
The server only submits information once.
Cancel a temporary file: git ResetThere are two modified files that we want to submit separately, but accidentally added to the temporary storage area with GIT add. How Cancel temporary storageWhat about a file? In fact, the git status Command output tells us how to do it:
Git reset head a.txt
After this command is run, a.txt returns to the previous Modified and not savedStatus. cancel modification to the file: git checkout -- if you think you have modified the file to benchmarks. RB modification is completely unnecessary. How can we cancel the modification and return to the previous state (that is, to change the previous version? Git status also prompts the specific Undo method. In the example above, the unsaved region now looks like this: # changes not staged for commit: # (use "Git add <File>... "To update what will be committed) # (use" Git checkout -- <File>... "To discard changes in working directory) # modified: benchmarks. RB # In the second bracket, we can see that the command to discard the file modification shows that the file has been restored to the version before modification. You may have realized that this command is dangerous, All modifications to the file are missing.Because we copied the previous version of the file to overwrite the file. Therefore, before using this command, make sure that you do not need to retain any modifications that have been submitted to git. Even if you submit a branch that has been deleted or use -- Amend to rewrite the submission, it can be restored (for details about data recovery, see Chapter 9 ). So, The data you may lose is limited to those that have not been submitted. For git, they are just as they have never existed..
View the current remote database in the remote Repository:
Git remote
Add the parameter-V to display the clone address:
Git remote-V

Capture data from the remote database: git fetch/git pull can use the following command to capture data from the remote warehouse to the local:
Git fetch [Remote-name]
This command will go to the remote Repository Pull all data that is not in your local repository. After running, you can access all the branches in the remote warehouse locally, merge one of them to the local machine, or just retrieve a branch to find out. (We will discuss in detail the concept and operations of branches in Chapter 3 .) After fetch, local files will not change. It is very important to remember that, The FETCH command only pulls the remote data to the local warehouse and does not automatically merge the data to the current work branch. It can be combined manually only when you have prepared the data (using the GIT merge command). If a repository is cloned, this command automatically lists the remote Repository OriginName. So, Git fetch OriginIt captures all updates uploaded to the remote repository since your last clone (or updates submitted by others since the last fetch ).
You can use Git pullCommand to automatically capture data, and then The remote branch is automatically merged to the current branch in the local repository.. In our daily work, we often use this method fast and well. In fact, by default, the GIT clone command automatically creates a local master branch to track the master branch in the remote warehouse (assuming that the remote warehouse does have a master branch ). So Generally, we run git pull to capture data from the original cloned remote repository and merge the data to the current branch in the working directory..
Push data to a remote Repository: git push Git push [Remote-name] [branch-name]. If you want to push the local master branch to the origin server (again, the clone operation will automatically use the default master and origin names), run the following command:
Git push origin master
This command will complete the task on schedule only when the cloned server has the write permission or no one else is pushing data at the same time. If you have already pushed several updates before pushing data, your push operation will be rejected. You must first capture their updates locally and merge them into your own projects before pushing them again. For more information about pushing data to a remote warehouse, see the next article.
View the remote Repository Information: git remote show. You can run the command Git remote show [Remote-name]To view the details of a remote repository, for example, to view the cloned origin repository, run:
Git remote show origin

Tag git can tag versions at a specific time point. People often do this when releasing a software version (such as V1.0. About this part of content, because we are not commonly used, I do not record here, interested can see: http://git.oschina.net/progit/2-Git-%E5%9F%BA%E7%A1%80.html#2.6-%E6%89%93%E6%A0%87%E7%AD%BE





From Weizhi note (wiz)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.