Common git commands

Source: Internet
Author: User
Tags git commands

Windows:

Git:

https://git-for-windows.github.io/

After the installation is complete, enter the command line to do the final step:

$ git config--global"Your Name"-global"[email Protected]"

To create a repository, select the disk you want to place, enter the following command (Learngit for the repository name, suggested English)

$ mkdir learngit$ CD learngit$ pwd

Enter the following command to turn this directory into a repository that git can manage

$ git init

Place the file you want to save to the library under the Learngit directory or under a subdirectory

Use the following command to save to staging area

$ git Add readme.txt

Use the following command to tell git to submit the file to the repository

" wrote a readme file "

Where-M is followed by the instructions for this submission

To view the changes or status of the warehouse, enter the following command

 $ git status# on branch master# changes not staged  for   commit:# (use  "  git add <file>..   "   Span style= "color: #800000;" >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  " ) 

The above information tells us that Readme.txt has been modified, specifically to see the revised instructions, you can enter the following instructions

If you want to see the history, you can enter the following instructions

$ git log

If you want to see a little more concise, you can enter the following instructions

$ git log--pretty=oneline

In Git, head represents the current version, last version is head^, last is head^^, and more can be written head~100. If you want to fallback to a previous version, you can enter the following instruction

$ git reset--hard head^

If you want to see the version, you can enter the following instructions

$ cat Readme.txt

If you want to return the specified version, you need to refer to Xiao commit_id, you can enter the following instructions

3628164

If you want to view each instruction, you can enter

$ git Reflog

Enter the following directives to discard modifications to the workspace

$ git checkout--readme.txt

If you want to undo the staging area changes and re-return to the workspace, you can use the command

$ git reset HEAD readme.txt

Use the following command to delete a file

$ RM test.txt

Remote repository:

In the user's home directory (c/user/xxx), see if there is a. SSH directory, and then continue to see the wood has Id_rsa and id_rsa.pub these two files, if any, you can skip the next step, otherwise enter the following command, create SSH Key

" [email protected] "

Generally do not set a password

Then login to GitHub, open the Accout settings,ssh keys page, click Add SSH Key, fill in the title, paste the contents of the Id_rsa.pub file in the Key text box, click Add Key, complete.

Add to Remote Library

Once you have added a warehouse to GitHub, you can use the following command to associate the local warehouse with it

$ git Remote add origin [email protected]:githubname/learngit.git

Githubname have to replace their GitHub account name.

Push all the contents of the local library to the remote library, using the following code:

$ Git push-u Origin Master

SSH warning

The first time you use Git's clone or push command to link to GitHub, you get a warning:

' github.com (xx.xx.xx.xx) ' can't be established.  is  Continue connecting (yes/no)?

This is because Git uses SSH connection, and SSH connection is the first time to verify the GitHub server key, you need to confirm that GitHub key fingerprint information is really from the GitHub server, enter Yes.

Cloning from a remote library

You can clone a warehouse to a local library on GitHub by entering the following command

$ git clone [email protected]:githubname/gitskills.git

Githubname is the account name for GitHub and Gitskills is the name of a repository above GitHub.

Create a branch

Use the following command to create a branch and switch to the branch

$ git checkout-new'dev'

The-b parameter means create and switch, equivalent to the following two commands

' Dev '

View current branch using the following command

$ git Branch* Dev  master

The command lists all the branches, and when the forward branch is in front of the table * number

The following command user merges the specified branch to the current branch

$ git merge devupdating D17efd8. Fec145afast-|    1 1 1 insertion (+)

GIT encourages the use of branching:

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>

When git cannot merge branches automatically, it must resolve conflicts first, then commit, merge complete

Use the following command to see the branch merge diagram

$ git log--graph

Use the following command to disable Fast forward

" Merge with No-ff the Dev

Dev represents a branch

Use the following Ming Lin to view the branch history

$ git log--graph--pretty=oneline--abbrev-Commit*   7825a50 merge with no-ff|  6224937 Add Merge|/*   fixed

Merging branches, plus the--NO-FF parameter can be combined with the normal mode, the merged history has branches, can see that there has been a merger, and fast forward merge can not see that there has been a merger.

Bug Branch

Git also provides a stash feature to "store" the current work site, and then continue to work after resuming the site:

6224937is6224937 add Merge

Now, with the git status view workspace, it's clean (unless you have a file that's not managed by git), so you can safely create a branch to fix the bug.

Use the following command to view the work site

$ git stash list[email protected]{06224937 Add Merge

The work site is still there, git put stash content somewhere, but need to restore, there are two ways:

One is to use git stash apply recovery, but after recovery, stash content does not delete, you need git stash drop to use to delete;

Another way is to use git stash pop , restore the stash content also deleted:

$ git stash pop# on branch dev# changes to being committed:# ( use"git reset HEAD <file>"to Unstage) # #Newfile:hello.py## changes not staged forcommit:# ( use"git add <file>, ... ."To update what'll be committed) # ( use"git checkout--<file>, ... ."To discard changesinchworking directory) # # modified:readme.txt#dropped Refs/[email protected]{0} (F624F8E5F082F2DF2BED8A4E09C12FD2943BDD40)

git stash listIf you look at it again, you won't see any stash content:

$ git Stash list

You can stash multiple times, restore the time, first with git stash list view, then restore the specified stash, with the command:

$ git stash Apply [email protected]{0}

Not finished, to be continued ...

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.