Common git commands

Source: Internet
Author: User
Tags git commands git mergetool

I. Instructions

1,Git clone

Obtain the remote git repo corresponding to a URL and create a local copy. the general format is GIT clone [url]. the cloned repo will be named after the last slash of the URL to create a folder. to specify a specific name, you can use git clone [url] newname. 2. Git statusQuery the repo status. Git status-S:-s indicates short. The output tag of-s has two columns. The first column is for the staging region, and the second column is for the working directory. 3. Git addBefore submission, git has a temporary storage area (staging area) that can be added to a newly added file or added to a new change. the changes submitted during commit are the changes added to the staging area last time, rather than the changes on the disk. git add. recursively add all files in the current working directory. 4. Git diffGit DIFF: Show diff of unstaged changes without parameters. this command compares the differences between the current file in the working directory and the snapshots in the temporary storage area, that is, the changed content has not been saved. if you want to see the difference between the files that have been saved and the snapshot that was last submitted, you can use the: git diff -- cached command. show diff of staged changes. (GIT 1.6.1 and later versions can also use git diff -- staged, with the same effect ). git diff head show diff of all staged or unstated changes. that is, compare all the changes between woking directory and the last commit. if you want to see what has been changed since a specific version, you can use: git diff [version tag], like the log command, diff can also add the -- stat parameter to simplify output. Git diff [brancha] [BranchB] can be used to compare two branches. it will actually return a patch from A to B, not the expected result. generally, the result we want is what are the changes made after the two branches are separated by the command: git diff [brancha]… [BranchB] is actually the result of git diff $ (GIT merge-base [brancha] [BranchB]) [BranchB. 5. Git commitSubmit the added changes. git commit-M "The commit message" Git commit-A will first add changes to all files that have already been tracked, and then submit (a bit like a SVN commit, you do not need to save the data first ). for files without track, you still need to add git. git commit -- Amend addition commit. A new submission will be performed on the same parent node as the current submission node, and the old submission will be canceled. 6. Git BranchGit branch can be used to list branches, create branches, and delete branches. git branch-V can see the last commit of each branch. git branch: list all local branches. The current branch is marked with an asterisk. git Branch (branchname): Creates a new branch (when you create a branch in this way, the Branch is created based on your previous commit ). git branch-D (branchname): delete a branch. delete a remote branch: git push (Remote-name) :( branch-name): delete a remote branch. this is because the complete command format is: git push remote-name local-branch: Remote-branch, and the local-branch part here is empty, it means that the remote-branch is deleted. 7. Git checkoutGit checkout (branchname)
Switch to a branch. git checkout-B (branchname): Creates and switches to a new branch. this command is the result of combining git branch newbranch and git checkout newbranch. checkout has another function: replace local changes: git checkout -- <FILENAME> This command will replace the files in your working directory with the latest content in the head. changes that have been added to the temporary storage area and new files will not be affected. note: git checkout filename will delete any temporary and submitted changes in the file. This operation is irreversible. 8. Git mergeAdd a branch merge to the current branch. git merge [alias]/[branch] transfers the remote branch merge to the current branch. if a conflict occurs, you can use git mergetool. git diff can be used to resolve the conflict. After resolving the conflict, add it with GIT add, indicating that the conflict has been resolved. 9. Git tagTag a point in history as import. A permanent bookmarks will be created on a submission. Usually a release version or ship is published and a tag is added. for example: git tag V1.0 git tag-A V1.0, the-a parameter allows you to add some information, that is, make an annotated tag. when you run the GIT tag-a command, git opens an editor for you to enter tag information. we can use commit Sha to tag a previous commit: git tag-A v0.9 XXXX push does not contain tags. If you want to include tags, you can add the -- tags parameter when pushing. during fetch, the tags that the branch head can reach are automatically fetch, and tags that aren't reachable from branch heads will be skipped. to ensure that all tags are included, add the -- tags option. 10. Git remoteList, add and delete remote repository aliases. because you do not need to use a complete URL every time, Git creates an alias for each remote repo URL and uses Git remote to manage the list. git remote: List remote aliases. if you clone a project, git will automatically add the original URL. the alias is: origin. git remote-V: You can see the actual URL of each alias. git remote add [alias] [url]: Add a new remote repo. git remote RM [alias]: deletes an existing remote alias. git remote RENAME [old-alias] [New-alias]: Rename. git remote set-URL [alias] [url]: Update URL. you can add the-push and fetch parameters to set different access addresses for the same alias. 11. Git fetchDownload new branches and data from a remote repository. you can use git fetch [alias] to retrieve a remote Repo, or git fetch -- all to retrieve all the repo fetch to retrieve all the data that you do not have locally, all acquired branches can be called remote branches. They are the same as local branches (you can view diff, log, etc., or merge to other branches ), but git does not allow you to checkout to them. 12. Git pullFetch from a remote repo and try to merge into the current branch. pull = fetch + merge fetch_head git pull will first execute git fetch, then execute git merge, and the obtained branch head merge to the current branch. this merge operation will generate a new commit. if the -- rebase parameter is used, it will execute git rebase to replace the original git merge. 13. Git rebase-- Rebase will not generate merged commits. It will save all local commits as patches temporarily and put them in ". in the GIT/rebase directory, update the current branch to the latest branch tip, and apply the saved patch to the branch. in the rebase process, conflicts may occur. Git will stop rebase and ask you to resolve the conflict. After the conflict is resolved, use git add to update the content without executing commit, you only need to: git rebase -- continue to continue with the remaining patches. git rebase -- abort will terminate rebase, and the current branch will return to the status before rebase. 14. Git pushPush your new branches and data to a remote repository. git push [alias] [branch] will put the current branch merge to the [branch] branch on Alias. if the Branch already exists, it will be updated. If it does not exist, it will be added. if multiple people push code to the same remote Repo, git will first run git log on the branch you are trying to push, check whether the current tip of branch on the server can be seen in its history. If the tip of the server cannot be seen in the local history, it indicates that the local code is not up-to-date, git will reject your push and let you fetch, merge first, and then push again, so that everyone's changes will be taken into account.

15. Git init

Create a repo locally, enter a project directory, execute git init, initialize a repo, and create a. Git folder under the current folder. 16. Git logShow commit history of a branch. git log -- oneline -- number: each log displays only one row and number. git log -- oneline -- Graph: graphical representation of the branch merge history. git log branchname can display the logs of a specific branch. git log -- oneline branch2 ^ branch2, which can be viewed in branch 1 but not submitted in branch 2. ^ indicates that this branch is excluded (the ^ branch2 may be enclosed in quotation marks in the window ). git log -- decorate will display the tag information. git log -- author = [author name] can specify the submission history of the author. git log -- since -- before -- until -- after filters logs based on the submission time. -- no-merges can exclude commits of merge. git log -- grep filters logs based on commit information: git log -- grep = keywords by default, git log -- grep -- author is an or relation, that is, if one condition is met, it is returned, if you want them to be the relationship between and, you can add the option of -- all-match. git log-S: Filter by introduced diff. for example, git log-smethodname (note that there is no equal sign between S and the following words ). git log-P: Show patch introduced at each commit. each commit is a snapshot. Git calculates the diff submitted each time and displays it as a patch. another method is GIT show [Sha]. git log -- stat: Show diffstat of changes introduced at each commit. it is also used to view the relative information of changes. -- stat is simpler than-P output. 17. Git ResetUndo changes and commits. the head keyword here refers to the latest commit at the end of the current branch. that is, the latest version of the branch in the version library. the GIT reset head: unstage files from index and reset pointer to head command is used to extract the file accidentally added from the staged state. You can operate on a file separately: git reset head--filename, which can also be left empty. git reset -- soft Move head to specific commit reference, index and staging are untouched. git reset -- hard unstage files and undo any changes in the working directory since last commit. use git reset-hard head for reset, that is, after the last commit, all staged changes and working directory changes will disappear and be restored to the last submitted status. the head can be written as any submitted SHA-1. git reset without the soft and hard parameters actually carries the default parameter mixed. conclusion: git reset -- mixed ID changes the GIT head (that is, the commit record is changed), but the file has not changed (that is, the working tree has not changed ). the contents of commit and add are canceled. git reset -- soft ID. actually, after git reset-mixed ID, another git add is performed. the contents of commit are canceled. git reset -- hard ID. changed the head of git and the file. sort by change range: Soft (COMMIT) <mixed (commit + Add) 18. Git revertReverse undo commit. you only need to pass the reference of the failed commit as a parameter to the command. git revert head: undo a recent commit. git revert will create a reverse commit. You can use the-n parameter to tell git not to commit. 19. Git RMGit RM file: remove the file from the staging area and remove the working directory. git Rm -- cached: removes files from the staging zone, but remains in the working directory. git Rm -- cached is functionally equivalent to git reset head. It clears the cache but does not move the working directory tree. 20. Git cleanGit clean removes files without track from the working directory. the common parameter is GIT clean-DF:-D, indicating that the directory is removed at the same time, and-F indicates force, because in the GIT configuration file, clean. requireforce = true. If-F is not added, clean will reject execution. 21. Git mvGit Rm--cached orig; MV orig new; git add new 22. Git stashPress the current change into a stack. git stash will push all the changes in the current directory and index (but does not include files without track) into one stack, and then leave you in a clean working state, that is, it is in the last latest submission. the GIT stash List displays the list of this stack. git stash apply: Get the previous project ([email protected] {0}) in stash and apply it to the current working directory. you can also specify other projects, such as git stash apply [email protected] {1 }. if you want to delete a stash project at the same time, you can use git stash pop to delete the project in Stash: git stash drop: Delete the previous project, or specify a parameter to delete the specified project. git stash clear: delete all projects. 23. Git reflogGit reflog is a command for managing reflog. reflog is a mechanism that git uses to record reference changes, such as record branch changes or head reference changes. when git reflog does not specify a reference, the reflog of the head is listed by default. [email protected] {0} indicates the current head value, and [email protected] {3} indicates the head value before three changes. git records the changes to the reflog file corresponding to the head. The path is. git/logs/head. All the reflog files of the Branch are stored in. in the subdirectory under the GIT/logs/refs directory. II, Git operation process:
  • Clone a new project, complete new features, and submit:
  1. Git clone XXX // clone the code library
  2. Git checkout-B test // create a branch
  3. Modify some files // complete modification
  4. Git Add. // Add modifications to stage
  5. Git commit-m'' // submit the modification to the test branch.
  6. Review code
  7. Git checkout master // switch to the master Branch
  8. Git pull // update code
  9. Git checkout test // switch to the test Branch
  10. Git meger master // merge the code of the master branch to the test Branch
  11. Git push origin branch name // push the code of the test branch to the remote database

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