Git: Some basic commands

Source: Internet
Author: User
Tags file info using git git mergetool

1. Quick access to remote projects

1) git clone xxx.git//such as: Git clone git://git.kernel.org/pub/scm/git/git.git

2) git clone [email protected]Http://www.kernel.org/pub/scm/git/git.git//username access, Git will prompt you password

3) git clone-b master2. /server//-B <name> Specify a branch clone

4) git clone-b [new_branch_name] Xxx.git//clone creates a new branch instead of the default master

2.git memory Consumption view

1) git gc shrinking space

2) git count-objects -v View the amount of space occupied.

3. Initialization and Status View

1) Git init//Create a new project locally, go to project execution git init will do git initialization, and a. git file is created under the current folder

2) 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.

3) Log information:

Show 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-p master: Origin/master     git log--oneline branch1 ^branch2, you can view the commit in branch 1, but not in branch 2. ^ means exclude this branch ( ^BRANCH2 may be quoted under window).      git log--decorate will display the tag information .      git log--author=[author name] You can specify the author's commit history.      git log--since--before--until--after Filter Log.     --no-merges based on submission time you can exclude the commits of the merge.      git log --grep Filter Log:git Log according to commit information--grep=keywords      by default, git log--grep--author is the or relationship, That is, to satisfy one is to be returned, 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 every commit.      each commit is a snapshot (snapshot), and Git calculates the diff for each commit, As a patch show you see .      another way is git show [sha].  &Nbsp;  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.   4. Update index, commit code

1) Update index

git Add. The code is added to the staging area

2) Submit Code

Submit the changes that have been added. Git commit-m "The commit message" Git commit-a will first add all the changes that have been made to the track file and submit it (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 fetch origin master//specify which branch of pull uses the rebase command to compress multiple commits into a git rebase-i head~[number_of_commits] If you want to compress the last two Co Mmit, you need to run the following command: Git rebase-i head~2 5. Pull code, submit code to remote 1) pull substitution code Download new branches and data from a remote repository. You can git fetch [alias] fetch a remote repo, or git fetch--all fetch all repo fetch will fetch all the data that you do not have locally, all the branches taken down can be called the remote branches, they are the same as the local branch ( You can see Diff,log, etc., or merge to other branches), but Git doesn't allow you to checkout them. Fetch from a remote repo and try to merge into the current branch.         The pull = = fetch + Merge Fetch_head git pull executes the git fetch first, then executes the git merge and merges the HEAD of the branch into the current branch. This merge operation will produce a new commit.  If you use the--rebase parameter, it executes git rebase instead of the original git merge. 2) Submit to remote Push your new branches and data to a remote repository.     git push [alias] [branch] will merge the current branch into 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 the server's branch now tip is visible in its history, and if it's not seen in the local history,  If the local code is not up-to-date, Git will reject your push, letting you fetch,merge and then push, so that everyone's changes will be taken into account. 3) Merger: git merge origin/master//merge Branches Merge a branch into the current branch. git merge [Alias]/[branch] merges 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.     --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 in the process, there may be conflicts, 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, just 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. 5. Cleanup 1) Delete git rm "file info"//delete files from git cache git 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 clean-fd//Clears files that are not monitored It clean is to remove files without track from the working directory.  The usual argument is git clean-df:-D means to remove the directory at the same time,-F for force, because in the git configuration file, clean.requireforce=true, if not added-f,clean will refuse to execute. 6. Branch 1) Branch Git branch: Can be used to list branches, create branches, and delete branches.     Git branch-a: View remote branch git branch-v you can see the last commit for each branch.     git branch-m devel Develop: rename local 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. Git branch-r-d origin/branch-name: Delete Remote branch Delete remotely branch: git push (remote-name):(branch-name): Delete a remote B     Ranch. This is because the complete command form is: git push remote-name local-branch:remote-branch and the part of Local-branch here is empty, which means delete remote-branch 2) remote List, 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 a different access address for the same alias set. Git remote rename PB paul: Repository Rename git remoteList the remote libraries for the current project
git remote -vDisplays the cloned address for the current item
git remote add [shortname] [url]Add a remote repository
git remote show [remote-name]To view the details of a remote warehouse
git remote [remote-old-name] [remote-new-name]Modify the remote warehouse name
git remote rm [remote-name]Delete a remote repository git pull [remotename] [localbranchname] git push [remotename] [Localbranchname]

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, as follows:

$git Push Origin Test:master//submit local Test branch as remote Master Branch

$git Push Origin test:test//submit local test branch as remote Test branch

3) switch 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: replace local changes: Git checkout--<filename> git checkout--track origin/branch-name: Switch Remote branch and create new branch locally  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 unaffected. NOTE: git checkout filename Removes all changes that are not staged and committed in the file, and this operation is irreversible. 7. View, Undo, revert 1) view git diff --name-only 73a79c 2d49d2View a list of the files in the middle of the two versions
Git diff:workspace and index file.
git diff head:workspace and Commint
git diff--cached:index file and commit git without Parameters diff: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 the: 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 HEAD show 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 we want the result is two branches apart after the individual changes are what, is by the command: Git diff [Brancha] ...     given by [BRANCHB]. In fact it is: Git diff $ (git merge-base [Brancha] [BRANCHB]) [BRANCHB] result 2) Undo Reverses 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. 3) Restore Undo changes and commits. The HEAD keyword here refers to the most recent commit of the current branch. That is, the latest version on the branch in the repository. Git reset head:unstage files from index and reset pointer to HEAD this     A command used to remove the accidentally added file from the staged state, which can be manipulated for a single file: Git reset HEAD--filename, this--can also be done without.     git reset--soft move HEAD to specific commit reference, index and staging is untouched.     git reset--hard unstage 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 has changed the head of Git (that is, the commit record has 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.     In fact, it was git reset–mixed ID, and then git add again. The content of the commit was canceled.     git reset--hard ID. The git head has changed and the file has changed. Sort by range of changes: soft (commit) < mixed (commit + Add) < hard (commit + add + local working)

8. Mark, change record

1) Tag

Tag 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.0 git 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 give a past commit to hit Tag: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 make sure that all tags are  Included, you need to add the--tags option. 2) Reflog Git 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 the head, and [email protected]{3} represents the value before the head changes 3 times. 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.

Git: Some basic 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.