The most common commands for Git:
Lonegit clone [email protected]:p roject-name.gitlocal branchgit checkout-b my_branchgit add filesgit commit-am ' some thin G ' Git push origin my_branchmerge Devgit checkout Devgit pull Origin devgit checkout my_branchgit Merge Dev
Detailed
initialization when using Git
1.1 Git initialization configuration
1) Configure the name of the person using the Git repository
git config--global user.name "Your name Comes here"
2) Configure the people who use the GIT repository email
git config--global user.email [email protected]
Comparison of 1.2Git and repo
A git operation typically corresponds to a warehouse, and the repo operation typically corresponds to a project, where a project is composed of several warehouses.
For example, use repo when you manipulate an entire Recket project, and git when you manipulate one of the warehouses. Perform a git operation in the directory that contains the hidden directory. Git.
2. Git Help
git help get git basic commands
(If you want to know how a particular command is used, for example: Use Git help clone to get git clone usage)
3. Git local Operation basic command
3.1. Git Init
or use Git init-db.
Create an empty git library. Produces a. Git subdirectory in the current directory. Later, all file change information is saved to this directory, and unlike CVS, a CVS directory is created in each directory and subdirectory.
There is a config file under the. git directory where you can modify the configuration information.
3.2. Git add
Adding the changes in the current working directory to the GIT index and adding it to the Git index represents the version history, which is a step you need to take before committing.
Can be added recursively, that is, if followed by a directory as an argument, all subdirectories and files in the entire directory are added recursively. For example:
git add dir1 (add dir1 This directory, all files in the directory are added)
Git Add F1 f2 (add f1,f2 file)
git Add. (Add all files and subdirectories in the current directory)
3.3. Git RM
Deletes files from the current working directory and from the index.
Can be deleted recursively, that is, if followed by a directory as a parameter, it will recursively delete all subdirectories and files in the entire directory. For example:
Git rm–r * (enters a directory, executes this statement, deletes all files and subdirectories in that directory)
git rm F1 (delete file F1, including this file record in local directory and index)
git rm--ached F1 (Delete the file F1, will not delete the local directory file, only delete the file record in index, remove the git add file to the cache, so the commit will not submit this file, for a moment to add a lot of files, But also want to exclude the case of individual several documents.
3.4. Git Commit
Commits the modification of the current working directory.
Calling the git commit command directly prompts you to fill in the comments. Complete the commit comment at the command line by using the following method: Git commit-m "Initial commit of Gittutor reposistory". Note that, unlike CVS, the commit comment for git must not be empty or it will fail to commit.
Git commit also has a-a parameter that can be forcibly submitted without a change to the Git add tag, but this method is not recommended.
With each commit, Git creates a unique commit identifier code for the global code that the user can revert to at any one time with the git reset command.
git commit–-amend–m "message" (continuous modification of the submitted content on a commit ID)
3.5. Git Status
View the status of the repository. You can tell which files have changed, which files have not been added to the Git repository, and so on. It is recommended that you confirm the library status with this command before each commit.
The most common mistake is to modify a file without calling Git add to notify the Git repository that the file has changed to call the commit operation directly, causing the file to not be actually committed. If the developer thinks that the file has been submitted, and then continues to modify or even delete the file, then the modified content is not managed by version. If you use Git status to see it every time before committing, you can find this error. Therefore, if you call the git status command, be careful with those files that prompt for "Changed but not updated:". These files are files that have changed since the last commit, but have not been identified by git Add.
3.6. Git Log
View the history log, which contains each version change. Each version change corresponds to a commit ID.
Git log-1
1 means only one commit, if you want to show 5, 5. If not specified, git log will be displayed backwards from that commit.
Git log--stat–summary (show detailed changes for each version)
3.7. Git Merge
Merge the code downloaded from the server with the local code. or a branch merge.
For example: On the Master branch, if you want to merge the branch dev onto master, git merge dev
Note: git merge nov/eclair_eocket (the Eclair_eocket branch of the server git library is merged into the local branch)
Git rebase nov/eclair_eocket (is to map the Eclair_eocket branch of the server Git repository to a local temporary branch, then merge the changes on the local branch into this temporary branch and then initialize the local branch with this temporary branch)
3.8. Git diff
Compare the local code with the code in index, or compare the code in index with the code in the local repository.
1) Git diff
Compare the code in the Working directory and index.
2) Git diff--Cached
Compare the code in index and local warehouses.
3.9. Git Checkout
3.9.1. Switching to a branch
1) Create a new branch and switch to that branch
Git checkout–b New branch Name
2) switch to an already established local branch Local_branch
Git Checkout Local_branch
(After using Cat. Git/head, show Refs:refs/heads/local_branch)
3) switch to a branch on the server Remote_branch
Git Checkout Remote_branch
(Remote branch Remote_branch can be listed via git branch–r)
4) switch to a commit ID
Git Checkout commit_id
(After using Cat. Git/head, show commit_id)
5) switch to a tag
Git Checkout Tag
(After using Cat. Git/head, show tag)
Note: In addition to 1 and 2), the remaining three are only switched to a temporary (no branch) state (this head is detached), then Git branch can be seen on (no branch), Cat Git/head see pointing The corresponding commit ID. This (no branch) is just a temporary existence, not a really established branch. If 2 is executed at this point, then this (no branch) disappears automatically, and if 1 is executed, the new branch is created, and this (no branch) is attached to the new branch, at which time the cat Git/refs/heads/new_branch You can see that the commit ID has been pointed to just now.
3.9.2. Initializing a new branch with an existing branch
Execute the following command to create a new branch new_branch while switching to an already established local branch or a remote branch or a commit ID or a tag, and hang it on the new branch.
1) switch to an already established local branch Local_branch, and use this branch to initialize a new branch new_branch.
git checkout–b new_branch local_branch
2) switch to a remote branch remote_branch, and use this branch to initialize a new branch new_branch.
Git checkout–b New_branch Remote_branch
3) switch to a commit ID and create a new branch New_branch
Git checkout–b New_branch commit_id
4) switch to a tag and create a new branch New_branch
Git checkout–b new_branch Tag
3.9.3. Restoring code
For example, "Git checkout app/model/user.rb" will update the user.rb file from the last committed version, and the contents of the uncommitted working directory will all be overwritten.
3.10. Git-ls-files
See which files are in the current git library.
3.11. Git MV
Rename a file, directory, or link.
For example: Git mv helloworld.c helloworld1.c (Rename the file helloworld.c to helloworld1.c)
3.12. Git Branch
3.12.1. General statement
The cost of creating a branch in the Git repository is almost zero, so you don't have to skimp on creating a few more branches. When Git init is executed for the first time, a branch called "Master" is created. Other branches are created by hand.
Some common branching strategies are listed below:
Create a personal branch of your own, to avoid too much interference with Master Branch master, and to facilitate communication and collaboration with others;
When doing high-risk work, create an experimental branch;
When merging other people's work, it is best to create a temporary branch to merge, and then "fetch" to your branch when the merge is complete.
The branch is added, deleted, checked and other operations.
Note: The branch information is usually in the. git/refs/directory, where the heads directory is a local branch, remotes is the branch on the corresponding server, tags is the tag.
3.12.2. Viewing branches
Git branch lists all the branches in the local git library. In the branches listed, if there is * in front of the branch name, this branch is the current branch.
Git branch–r lists all the branches of the server git library.
(You can continue to use the command "git checkout-b local branch name Server branch name" to get a code file for a branch on the server).
3.12.3. See which branch is currently on
Cat. Git/head
3.12.4. Creating a Branch
1) Git Branch branch name
Although a branch is created, the current work branch is not switched to the newly created branch, so the command "git checkout branch name" is also required to switch,
2) Git checout–b branch name
Not only did you create a branch, but you also switched the current work branch to that branch.
3.12.5. Switch to a branch: Git checkout branch name
Switch to Main branch: Git checkout Master
3.12.6. Deleting a branch
git branch–d branch Name
Note: After deletion, all changes that occur in the branch cannot be recovered. Forces the deletion of this branch.
3.12.7. Comparing the differences of files on two branches
git diff Master branch name (compare the difference between the main branch and the other branch)
3.12.8. Viewing branch history
Git-show-branch (view submission comments and information for the current branch)
Git-show-branch-all (view submission comments and information for all branches) for example:
* [Dev] D2
! [Master] m2
--
* [Dev] D2
* [dev^] D1
* [Dev~2] D0
*+ [Master] m2
In the above example, the two lines above "--" represent two branch Dev and master, and the last commit log on the dev branch is "D2", and the last committed log on the master branch is "m2". The "--" lines below represent the history of branching evolution, where Dev represents the last commit that occurred on the Dev branch, and dev^ represents the penultimate commit that occurred on the Dev branch. Dev~2 represents the third-to-last commit that occurred on the Dev branch.
3.12.9. Viewing operational records for the current branch
Git whatchanged
3.12.10. Merging branches
Law One:
Git merge "comments" merge the source branch of the target branch merge
Git will be prompted if there is a conflict with the merge.
Example: Git checkout master (switch to master branch)
git merge HEAD dev~2 (merge Master branch and Dev~2 branch) or: git merge master dev~2
Law II:
The source branch of the target branch merge of the git pull merge
Example: Git checkout master (switch to master branch)
Git pull. Dev~2 (merge current branch and Dev~2 branch)
3.13. Git rebase
Generally used when the latest server content is merged to local, for example: get the content from the server on version c to local, modify the local content, at this time to commit the locally modified content to the server, but found that the version on the server has become G, you need to first execute git rebase, Merge the latest version on the server to local. For example:
When the rebase command executes, it actually moves the branch point from C to G, so that the branch has a function from C to G.
3.14. Git Reset
The reversal and recovery of the library has an important role to play in addition to the replacement of some obsolete research and development code. For example, we have a code base from remote clone, which is developed locally and ready to be submitted back to the remote. But the local code base is developed with a functional commit, a commit for backup purposes, and so on. In summary, there are a lot of useless logs in the commit log, and we don't want to commit the log to the repository when it is submitted back to the remote. Therefore, git reset will be used.
The concept of git reset is more complex. Its command form: git reset [--mixed |--soft |--hard] [<COMMIT-ISH>]
Options for the command:
--mixed This is the default option. such as git reset [--mixed] dev^ (dev^ definition can be found in 2.6.5). It only functions to reset the branch state to dev1^, but does not change the contents of any working file. That is, all the file changes from dev1^ to Dev1 are preserved, but all the commit logs from dev1^ to Dev1 are cleared, and the changed file contents are not identified by the Git add, and if you want to re-commit, you need to do git for the changed file once. Add That way, after committing, you get a very clean commit record. (The content in index and warehouse is rolled back.)
--soft is equivalent to doing Git reset–mixed, and then git add to the changed file. If you use this option, you can commit directly. (The contents of the warehouse are rolled back)
--hard This command will cause all information to be rolled back, including the contents of the file. It is generally only used when you reset the obsolete code. After execution, the contents of the file can not be restored back. (The contents of the working directory, index, and warehouse are rolled back)
For example:
Switch to the used branch;
Git reset head^ fallback first record
Git reset head~2 fallback second record
If you want to roll back the files in your working directory, use git reset-hard head^ to rewind the first record
Git reset-hard head~2 fallback second record
3.15. Git revert
Revert a modification to a version, for example: git revert commit_id (where commit_id is a unique representation of a string generated when the commit code)
For example: (3.6) git revert dfb02e6e4f2f7b573337763e5c0013802e392818 (to do this, restore the last commit operation)
3.16. Git Config
With this command, you can add and change various git settings, such as "git Config branch.master.remote origin" to set the master's remote repository to an alias called the Origin repository.
3.17. Git Show
Displays the different types of objects.
3.18. Git Tag
Create, list, delete, or validate a Label object (signed with GPG).
You can tag a specific version so you don't need to memorize a complex version number hash string, for example you can use "Git tag revert_version bbaf6fb5060b4875b18ff9ff637ce118256d6f20" To mark the version that you restored, you can use the revert_version tag name instead of the hash value when you want to view the version later.
4. Git server Operation command (interacting with server)
4.1. git clone
Remove the code from the server's warehouse to the locally established directory (interacting with the server) After you get the remote git library through Git clone, the. Git/config developer information will not be clone together. You still need to add developer information for the. git/config file for your local library. In addition, developers need to add their own . Gitignore file.
The remote git library acquired through Git clone contains only the current working branch of the remote Git library. If you want to get other branch information, you need to use "Git branch–r" to see, if you need to get other remote branch code, you can use the command "git checkout-b local branch name Remote branch name", where the remote branch named "Git Branch–r" The names of the branches listed are generally like the "origin/branch name". If the local branch name already exists, then the "-B" parameter is not required.
4.2. Git pull
Gets the code from the server's warehouse and merges with the local code. (Interacting with the server, downloading the latest code from the server, equivalent to: git fetch + git merge)
to update the code locally from the other repository (which can be remote or local), for example: "Git pull Origin Master" is to update the code for the Origin repository to the local master Master branch.
git pull can get the contents of a branch from any git library. Use the following:
git pull [email protected] : Remote repository name Remote branch name local branch name. This command takes the remote branch name from the remote Git repository to a local branch of the local git repository. Where the local branch name is not written, the default pull is to the local current branch.
It is important to note that Git pull can also be used to merge branches. It works the same as Git merge. Therefore, if your local branch already has content, git pull merges the files and, if there is a conflict, alerts you.
4.3. Git push
Updating local commit code to the remote repository, such as "Git Push origin", updates the local code to the remote repository named Orgin.
git push and git pull just want to reverse the content of a local branch to commit to a remote branch. Usage: git push[email protected]: Remote repository name local branch name Remote branch name. This command will push a local branch of the local git repository to the remote branch name of the remote Git repository.
It is important to note that git push does not seem to automatically merge files. Therefore, if there is a conflict with git push, it will be forcibly overwritten with the contents of the post-push file, and there is no hint. This is a very dangerous thing to do in co-development.
4.4. Git Fetch
Download the code from the server's warehouse. (Interacting with the server, downloading the latest code from the server)
The equivalent of getting the latest version from remote to local, not automatically merge, more secure than git pull.
Use this method to get updates on the server.
For example, if you use Git checkout Nov/eclair_rocket (nov/eclair_rocket as the branch name on the server), get the code that was downloaded from the server the last time you used the git fetch command, or if you first use git fetch, Then use git checkout nov/eclair_rocket to get the latest updates from the server and download the latest code from the server.