git common Commands summary
git initCreate a new repo locally, enter a project directory, execute git init, initialize a repo, and a. git folder under the current folder.
git cloneget a URL for the remote git repo, and create a local copy.The general format is git clone [url].clone down repo will be named after the last slash of the URL, create a folder, if you want to specify a specific name, you can git clone [url] newname specified.
git statusqueries the status of repo.git status-s:-s means short, the-s output tag will have two columns, the first column is for the staging region, the second column is for the working directory.
git logShow commit history of a branch.git log--oneline--number: Each log displays only one row, showing the number bar.git log--oneline--graph: You can graphically represent the history of a branching merge.git log branchname can display the log for a particular branch.git log--oneline branch1 ^branch2, you can view the commit in branch 1, but not in branch 2. ^ Indicates that the branch is excluded (the ^BRANCH2 may be quoted under window).git log--decorate will display the tag information.git log--author=[author name] can specify the author's commit history.git log--since--before--until--after Filter log based on commit time.--no-merges can exclude the commits of the merge.git log--grep filter log:git log based on commit information--grep=keywordsby default, git log--grep--author is the or relationship that satisfies one that is returned, and if you want them to be and the relationship, you can add--all-match option.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 (snapshot), and Git calculates the diff for each submission as a patch display for you.Another approach is git show [SHA].git log--stat:show diffstat of changes introduced at each commit.also used to look at the relative information of the change,--stat is simpler than the output of-p.
git addbefore committing, Git has a staging area (staging area) that can be placed in a newly added file or added to a new change. Commit is the last change that was added to the staging area, not the change on our disk.git add . all files in the current working directory are added recursively.
git diffgit diff with no parameters:show Diff of unstaged changes.This command compares the differences between the current file in the working directory and the snapshot of the staging area, i.e. changes that have not yet been staged after the modification. to see the difference between a file that has been staged and a snapshot from the last commit, you can use:git diff--cached command.show Diff of staged changes.(Git 1.6.1 and later also allows Git diff--staged to be the same effect). git diff HEADshow Diff of all staged or unstated changes.that is to compare all changes between Woking directory and the last commit. If you want to see what has changed since a certain version, you can use:git diff [version tag]As with the log command, diff can also add the--stat parameter to simplify the output. git diff [Brancha] [BRANCHB] can be used to compare two branches.It actually returns a patch from A to B, not the result we want.generally the result we want is two branches apart after the individual changes are what, is by the command:git diff [Brancha] ... given by [BRANCHB].In fact it is the result of Git diff $ (git merge-base [Brancha] [BRANCHB]) [BRANCHB].
git commitsubmit the changes that have been added.git commit-m "The commit message"git commit-a will add all the changes that have been made to the track file, and then commit (a bit like an SVN commit without first staging). For files without track, git add is still required.git commit--amend supplemental commit. A new commit is made using the same parent node as the current commit node, and the old commit will be canceled.
git resetundo changes and commits. The head keyword here refers to the latest commit at the top of the current branch. That is, the latest version on that branch in the repository. git reset head:unstage files from index and reset pointer to HEADThis command is used to remove the accidentally added files from the staged state, which can be manipulated for a single file: Git reset HEAD--filename, this--or not.git reset--softmove HEAD to specific commit reference, index and staging is untouched.git reset--hardunstage files and undo any changes in the working directory since last commit.using git reset-hard head for reset, after the last commit, all staged changes and working directory changes will disappear to the last commit state.the head here can be written as a SHA-1 of any one commit.git reset without soft and hard parameters is actually the default parameter, mixed. Summary:The git reset--mixed ID changes the git head (that is, the commit record is changed), but the file does not change (that is, the working tree does not change). The contents of commit and add are canceled .git reset--soft ID. In fact, after the Git reset–mixed ID, git add again. That is, the content of the commit was canceled.git reset--hard ID. The git head has changed and the file has changed.Sort by the scope of the changes as follows: Soft (commit) < mixed (commit + Add) < hard (commit + add + local working)
git revertreverses the undo commit. Simply pass the name of the error commit (reference) as a parameter to the command.git revert HEAD: Undo the most recent commit.git revert creates a new, reverse commit that you can tell git not to commit by using parameter-N.
git rmgit rm file: Removes files from the staging zone and also removes the working directory.git rm--cached: Removes files from the staging area, but stays in the working directory.git rm--cached is functionally equivalent to git reset HEAD, which clears the cache but does not move the working directory tree.
git mvgit rm--cached orig; MV orig new; git Add new
git stashpress the current change into a stack.git stash will push all changes in the current directory and index (but not the non-track files) into a stack and leave you with a clean working state, which is at the last commit.The git stash list displays the list for this stack.git stash apply: Take out the previous item in the Stash ([email protected]{0}) and apply it to the current working directory.You can also specify other items, such as git stash apply [email protected]{1}.If you want to delete a project while applying stash, you can use Git stash pop to delete an item in stash:git stash drop: Deletes the previous one, optionally specifying a parameter to delete the specified item.git stash clear: Delete all items.
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: Lists all branches locally, and the current branch is marked with an asterisk.git Branch (branchname): Create a new branch (when you create a branch in this way, the branch is based on your last commit).git branch-d (branchname): Deletes a branch.to delete a remote branch:git push (remote-name):(branch-name): Delete a remote branch.This is because the complete command form is:git push remote-name local-branch:remote-branchand the local-branch part here is empty, which means deleting the remote-branch
git checkout
git Checkout (branchname)
switch to a branch.git checkout-b (branchname): Create and switch to a new branch.This command is the result of a combination of Git branch newbranch and git checkout newbranch. Checkout also has another role: replacing local changes: git checkout--<filename>This command replaces the files in your working directory with the latest content in the head. Changes that have been added to staging area and new files are not affected.Note: git checkout filename Removes all changes that are not staged and committed in the file, and the operation is irreversible.
git mergeMerge a branch into the current branch.git merge [Alias]/[branch]merge the remote branch into the current branch. If there is a conflict, you need to modify it manually, you can use Git mergetool.when resolving conflicts, Git diff can be used, and after the fix is added with git add, that means the conflict has been resolved.
git tagtag a point in the history as import.will create a permanent bookmark on a commit, usually by releasing a release version or by adding a tag after the ship has something.For example: Git tag v1.0git tag-a v1.0, the-a parameter will allow you to add some information that makes an annotated tag.when you run the git tag-a command, Git opens an editor to let you enter the tag information. we can use commit SHA to tag a past submission:git tag-a v0.9 XXXX push is not included in the tag, if you want to include, you can add the--tags parameter when push.when fetch, branch head can reach the tags are automatically fetch down, tags that aren ' t reachable from branch heads would be skipped. If you want to ensure that the Some tags are included, you need to add the--tags option.
git remotelist, add and delete remote repository aliases.because you don't need to use the full URL every time, Git creates an alias for each remote repo URL, and then uses GIT remote to manage the list.git remote: Lists the remote aliases.If you clone a project,git will automatically add the original URL, alias is called: Origin.git remote-v: You can see the actual URL corresponding to each alias.git remote add [alias] [url]: Add a new remote repo.git remote rm [alias]: Delete 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.
git fetchdownload new branches and data from a remote repository.you can git fetch [alias] fetch a remote repo, or git fetch--all take all repoFETCH will fetch all the data that you do not have locally, and all the removed branches can be called remote branches, which are the same as local branches (you can see Diff,log and so on, or merge to other branches), but Git doesn't allow you to checkout them. .
git Pullfetch from a remote repo and try to merge into the current branch.Pull = = fetch + Merge Fetch_head git pull performs a git fetch first, then executes the git merge and merges the head of the branch to the current branch. This merge operation produces a new commit. If you use the--rebase parameter, it executes git rebase instead of the original git merge.
git rebase--rebase does not produce a merged commit, it will temporarily save all commits locally as patches, put them in the ". Git/rebase" directory, update the current branch to the newest branch tip, and finally apply the saved patches to the branch.rebase, there may be a conflict, Git will stop rebase and let you resolve the conflict, after resolving the conflict, with git add to update the content, and then do not have to commit, only need:git rebase--continue will continue to play the remaining patches.git rebase--abort will terminate rebase, and the current branch will return to the state before rebase.
git pushPush your new branches and data to a remote repository.git push [alias] [branch]The current branch will be merge to the [branch] branch on alias. If the branch already exists, it will be updated and will be added if it does not exist.If multiple people are to the same remote repo push code, GIT will first run git log on the branch you're trying to push, checking to see if it's history sees branch's current tip on the server. If the server tip is not visible in local history, the local code is not up-to-date, and Git rejects your push, letting you fetch,merge and then push, so that everyone's changes are taken into account.
git refloggit reflog is a command to manage Reflog, and Reflog is a mechanism that GIT uses to record changes to a reference, such as a change in branch or head reference.when git reflog does not specify a reference, the Reflog of the head is listed by default.[Email protected]{0} represents the current value of head, [email protected]{3} represents the value of head before 3 changes.git logs changes to the Reflog file in the HEAD, with a path of. Git/logs/head, and the Reflog files for the branch are placed in subdirectories under the. Git/logs/refs directory .
Special symbols :^ Represents the parent commit, when a commit has multiple parent commits, it is possible to represent the first few parent commits by following a number in the ^: ^ equivalent to ^1.~<n> equivalent to a continuous <n> ^. Referencesreference:http://gitref.org/index.htmlhttp://git-scm.com/book/zh/v1
Git Common Commands Summary