Common git commands

Source: Internet
Author: User
Tags using git git commands

1. Git Concepts
1.1. Git libraries are made up of three parts
The git repository is the. git directory, which holds the document index content that we submit, and Git can track the document it manages based on the document index content, enabling document versioning. The Git directory is located in the working directory.
1) Working directory: User's local directory;
2) Index: Generate a snapshot of all files (including subdirectories) in the working directory, and store them in a temporary storage area, which Git calls the area as an index.
3) Warehouse: The index is submitted to the warehouse through a commit command, and each commit means that the version is being updated.
1.2. Initialization when using Git
1.2.1. Git initialization configuration
1) Configure the name of the person using the Git repository
git config--global "Your name Comes here"
2) Configure the people who use the git repository email
git config--global [email protected]
1.2.2. Git document ignoring mechanism
There are some files in the working directory that you do not want to accept git to manage, such as intermediate files generated at the time of program compilation, and so on. GIT provides a document-ignoring mechanism for writing document information in the working directory that you do not want to accept in git management in the. gitignore file in the same directory.
For example: in the working directory there is an zh directory, if you do not want to add it to git management, then execute:
echo "en" >. Gitignore
git Add.
a lot of detail about the Gitignore file can be read in its manual: Man Gitignore
1.3. Git vs. 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)
In the project log information, the first line of each log (that is, the string of characters) is named for the version update submission, which we can understand as the project version number. The project version number should be unique and is automatically generated by Git by default to indicate an update to the project. If we use the project version number as a parameter to the Git-show command, we can view the update details for that minor project version. For example:
1) Git Log

2) Git Show

In fact, the above command is not really a version number customization, it just makes a tag object, which is useful when the project version is published externally.
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:
The following two illustrations will be more clear, after the rebase command executes, it is actually moving the branch point from C to G, so that the branches have functions 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
You can also use the following methods:
The current working directory is completely rolled back to the specified version number, assuming, for example, that we have a-g five commits, where C's version number is BBAF6FB5060B4875B18FF9FF637CE118256D6F20, we executed the ' git reset Bbaf6fb5060b4875b18ff9ff637ce118256d6f20 ' Then there's only one a-c. Three submitted versions

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 Operations Command (interacting with the server)
4.1. Git Clone
Remove the code from the server's warehouse to a locally created directory (interacting with the server)
After obtaining the remote Git repository via 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 them themselves. Gitignore file.
The remote git repository obtained through Git clone contains only the current working branch of the remote Git repository. 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.
For example:

4.2. Git Pull
Get the code from the server's warehouse, and merge the local code. (Interacting with the server, downloading the latest code from the server, equivalent to: git fetch + git merge)
Update the code locally from the other repository (which can be remote or local), for example: "Git pull Origin master" updates 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 the 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.
For example:

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.
For example:

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.

Git often commands git pull to automatically help you merge, and git fetch needs to merge itself, so everyone uses git fetch to update the project to know what others have changed. Specific commands:> Git fetch Origin master:tmp
> Git diff tmp
> Git merge tmp Git Common Operations Command collection:   1) Remote Warehouse related commandsCheck out Warehouse: $ gitClone git:// gitView remote warehouses: $ gitRemote-v Adding a remote repository: $ gitremote add [name] [url] Delete the repository: $ gitremote RM [name] Modifying the repository: $ gitRemote Set-url-- Push[Name] [Newurl] Pull remote repository: $ gitpull [RemoteName] [localbranchname] Push remote repository: $ git push[RemoteName] [Localbranchname] 2) Branch (branch) operation related commandsView Local Branch: $ gitBranch View Remote branch: $ gitBRANCH-R Create local branch: $ gitbranch [name]----Note that the new branch will not automatically switch to the current branch after it is created: $ gitCheckout [name] creates a new branch and immediately switches to the new branch: $ gitcheckout-b [name] Delete branch: $ gitbranch-d [name]-----D option removes only the branches that have joined the merge and cannot be deleted for branches that do not have a merge. If you want to force the deletion of a branch, you can merge the branch with the-D option: $ gitMerge [name]----Create a remote branch with a branch named [name] with the current branch (local branch PushTo remote): $ git pushOrigin [name] Delete remote branch: $ git pushOrigin:heads/[name]

I created a issue5560 branch from the master branch and made some changes, using git push origin master to commit, but the result is ' everything up-to-date ', the problem is because git push Origin master commits the master branch by default in the local branch without the track remote branch, because the master branch defaults to the Origin master branch, which uses the git push origin Issue5560:master can push the issue5560 to the remote Master branch.

You can do this if you want to submit a local branch test to the remote repository as the master branch of the remote repository, or as another branch called Test.

$ git pushOrigin Test:master//submit local Test branch as remote Master Branch
$ git pushOrigin Test:test//submit local test branch as remote Test branch

What if you want to delete a remote branch? Similar to the above, if: The left branch is empty, then delete: The remote branch on the right.

$ git pushOrigin:test//Just submit to the remote test will be deleted, but the local will be saved, do not worry 3) version (tag) operation related commandsView Version: $ gitTag creation version: $ gittag [name] Delete version: $ gittag-d [name] View Remote version: $ gitTag-r Creating a remote version (local version PushTo remote): $ git pushOrigin [name] Delete remote version: $ git pushOrigin:refs/tags/[name] 4) Sub-module (submodule) Related Operations CommandAdd sub-module: $ gitsubmodule add [url] [path] such as: $ gitSubmodule add git:// gitSrc/main/webapp/ui-libs initialization submodule: $ gitSubmodule init----Run only once when the warehouse is first checked out update submodule: $ gitSubmodule Update----Each time you update or switch branches, you need to run the delete submodule: (4 steps) 1) $ gitRM--cached [path]2] Edit the ". Gitmodules" file, delete the associated configuration node of the Submodule 3) edit. git/config "file, delete the associated configuration node of the Submodule 4) manually delete the remaining sub-modules directory

Common git commands

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: 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.