Common git commands

Source: Internet
Author: User
Tags diff version control system git commands


1. Set User name/password
$ git config--global user.name "Your name" $ git config--global user.email "[Email protected]"

2. 初始化一个Git仓库,使用git init命令。

$ git initinitialized empty git repository in/users/michael/learngit/.git/


3.  Add files to the Git repository in two steps:

    • The first step, use the command git add <file> , note, can be used repeatedly, add multiple files;

    • The second step, using git commit the command, is done.

$ git Add readme.txt

$ git commit-m "wrote a readme file" [Master (Root-commit) Cb926e7] wrote a Readme file 1 file changed, 2 insertions (+) CR Eate Mode 100644 readme.txt

4.
    • To keep track of the status of your workspace, use the git status command.

    • If git status you are told that a file has been modified, you git diff can view the modified content.

$ git status# on branch master# changes not staged for commit:#   (use "git add <file> ...." To update what would be Committed) #   (use "Git checkout--<file> ..." to discard changes in working directory) # #    Modified:   Readme.txt#no changes added to commit (use "git add" and/or "Git Commit-a")

$ git diff readme.txt diff--git A/readme.txt B/readme.txtindex 46d49bf: 9247DB6 100644---a/readme.txt+++ b/readme.txt@@ -1,2 +1,2 @@-git is a version control System.+git is a distributed versio N Control System. Git is free software.

5
    • HEADThe point is the current version, so git allows us to navigate between versions of history, using commands git reset --hard commit_id .

    • Before you travel, git log you can view the commit history to determine which version to fallback to.

    • To return to the future, use the git reflog view command history to determine which version to return to in the future.

Git log View history submissions

$ git logcommit 3628164fb26d48395383f8f31179f24e0882e1e0author:michael Liao <[email protected]>date:   Tue 15:11:49 +0800    Append gplcommit ea34578d5496d7dd233c827ed32a8cd576c5ee85author:michael Liao <[email Protected]>date:   Tue 14:53:12 +0800?    Add Distributedcommit cb926e7ea50ad11b8f9e909c05226233bf755030author:michael Liao <[email protected]>Date:   Mon 17:51:55 +0800?    Wrote a Readme file

$ git log--pretty=oneline3628164fb26d48395383f8f31179f24e0882e1e0 append Gplea34578d5496d7dd233c827ed32a8cd576c5ee85 Add distributedcb926e7ea50ad11b8f9e909c05226233bf755030 wrote a Readme File

$ cat Readme.txtgit is a distributed version control system. Git is free software.
$ git reset--hard 3628164HEAD is now at 3628164 append GPL

$ git reflogea34578 [email protected]{0}: reset:moving to head^3628164 [email protected]{1}: commit:append GPLea34578 [E Mail protected]{2}: Commit:add distributedcb926e7 [Email protected]{3}: Commit (initial): wrote a readme file

6.

Scenario 1: When you mess up the contents of a file in your workspace and want to discard the workspace changes directly, use the command git checkout -- file .

Scenario 2: When you not only changed the contents of a file in the workspace, but also added to the staging area, want to discard the changes, two steps, the first step with the command git reset HEAD file , back to Scene 1, the second step by scene 1 operation.

git checkout -- fileYou can discard modifications to the workspace:

$ git checkout--readme.txt
With the command git reset HEAD fileYou can undo the staging area changes (Unstage) and put them back in the workspace:
$ git reset HEAD readme.txtUnstaged changes after reset:M       readme.txt

7. To delete the file from the repository, use the command git rmErased, and git commit

$ git rm test.txtrm ' test.txt ' $ git commit-m "Remove test.txt" [Master D17efd8] Remove Test.txt 1 file changed, 1 deletion (-) Delete mode 100644 test.txt

git checkoutInstead, replace the workspace version with the version in the repository, which can be "one-click Restore", regardless of whether the workspace is modified or deleted.

$ git checkout--test.txt
8. Add a remote repository

git remote add origin [email protected]:michaelliao/learngit.git

Push all the contents of the local library to the remote library:
$ git push-u origin mastercounting objects:19, done. Delta compression using up to 4 threads.compressing objects:100% (19/19), done. Writing objects:100% (19/19), 13.73 KiB, done. Total (delta 6), reused 0 (Delta 0) to [email protected]:michaelliao/learngit.git * [New branch] Master-,      Master Branch Master set up to track remote Branch master from Origin.
As long as the local commits, you can pass the command:
$ GIT push origin master

To associate a remote library, use the command git remote add origin [email protected]:path/repo-name.git ;

Once associated, use the command to git push -u origin master push all the contents of the master branch for the first time;

Thereafter, after each local submission, whenever necessary, you can use the command to git push origin master push the latest changes;

9.

To clone a warehouse, you must first know the address of the warehouse and then use the git clone command clone.

GIT supports a variety of protocols, including https , but with the fastest ssh supported native git protocols.

$ git clone [email protected]:michaelliao/gitskills.gitcloning into ' gitskills ' ... remote:counting objects:3, Done.remote:Total 3 (Delta 0), reused 0 (Delta 0) receiving objects:100% (3/3), done.

10.

To view branches:git branch

To create a branch:git branch <name>

To switch branches:git checkout <name>

Create + switch Branches:git checkout -b <name>

Merge a branch to the current branch:git merge <name>

To delete a branch:git branch -d <name>

$ git checkout-b devswitched to a new branch ' Dev '

$ git branch dev$ git checkout devswitched to branch ' Dev '
$ git branch* dev  master


$ git merge devUpdating d17efd8..fec145aFast-forward readme.txt |    1 + 1 file changed, 1 insertion(+)
$ git branch-d devdeleted branch Dev (was fec145a).

11. Use the git log --graph command to see the branch merge diagram

$ git log--graph--pretty=oneline--abbrev-commit*   59BC1CB Conflict fixed| | * 75a857c and simple* | 400b400 & Sim ple|/* fec145a Branch Test ...

12. When merging branches, add --no-ffParameters can be combined in normal mode, the merged history has branches, can be seen to have been merged, and fast forwardThe merger would not have been seen as a merger.

$ git merge--no-ff-m "merge with No-ff" Devmerge made by the ' recursive ' strategy. Readme.txt |    1 + 1 file changed, 1 insertion (+)

13.

When fixing a bug, we will fix it by creating a new bug branch, then merging and finally deleting;

When the work is not finished, first put the job site git stash , and then go to fix the bug, repair, and then git stash pop back to the job site.

stashfunction, the current work site can be "stored" up, and so on after the resumption of the site to continue to work:
$ git stashsaved working directory and index state WIP on dev:6224937 add Mergehead are now at 6224937 add Merge<pre na Me= "code" class= "plain" >$ git stash list[email protected]{0}: WIP on dev:6224937 Add merge

$ git stash pop# on branch dev# changes to being committed:#   (use "git reset HEAD <file> ..." to Unstage) # #       New File:   hello.py## changes not staged for commit:#   (use "git add <file> ..." To update what would be committed) #   (use "Git checkout--<file> ..." to discard changes in working directory) # #       modified:   readme.txt# Dropped refs/[email protected]{0} (F624F8E5F082F2DF2BED8A4E09C12FD2943BDD40)

$ git stash apply [email protected]{0}



-
    • git remote-v ;

    • The locally created branch is not visible to others if it is not pushed to the remote;

    • Push branches locally, use git push Origin branch-name , if push fails, Use git pull to crawl the remote new commit first;

    • git checkout-b branch-name origin/branch-name , the names of local and remote branches are best consistent;

    • git branch--set-upstream branch-name Origin/branch-name ;

    • Crawl branches from the remote, using git pull , if there is a conflict, deal with the conflict first.

$ git remote -vorigin  [email protected]:michaelliao/learngit.git (fetch)origin  [email protected]:michaelliao/learngit.git (push)
$ git push origin master
$ git branch --set-upstream dev origin/devBranch dev set up to track remote branch dev from origin.
15. Tags
    • The command is git tag <name> used to create a new label, either by default HEAD or by specifying a commit ID;

    • git tag -a <tagname> -m "blablabla..."Label information can be specified;

    • git tag -s <tagname> -m "blablabla..."Can be signed with PGP tag;

    • Command git tag to view all tags.

    • Command git push origin <tagname> to push a local tag;

    • Command git push origin --tags to push all the local labels that have not been pushed;

    • Command git tag -d <tagname> to delete a local tag;

    • Command git push origin :refs/tags/<tagname> to delete a remote label.


Common git commands

Related Article

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.