Git Common commands

Source: Internet
Author: User
Tags git cheat sheet

1. About deletion and rollback


1 git Rm -- cached file wants git to stop tracking this file, but does not want to delete it from the hard disk.


2. When deleting files in the directory managed by git, you can select either of the following methods:
① RM + git commit-am "ABC"
② Git RM + git commit-M "ABC"

3 if RM is used, but you want to restore git checkout -- File


4 gir RM:

① If git reset head is not submitted

② You have already commit git reset -- hard <previous version, which can be viewed by git reflog>


5. Restore after modification

① When you change the content of a file in the workspace and want to discard the modification, run git checkout -- file.

② When you not only disrupt the content of a file in the workspace, but also add the file to the temporary storage area, you want to discard the modification, in two steps, the first step is to use the command git reset head file, return to scenario 1. Step 2: perform operations based on scenario 1.

③ If an inappropriate modification has been submitted to the version library and you want to cancel this submission, perform version rollback (GIT reset -- hard)

Indicates the latest version. The previous version is head ^, the previous version is head ^, And the last version is head ^. The first version is written as head ~ 100.


2. view history and modifications


1 git log

2 git log -- pretty = oneline

3 git diff head -- file: view the difference between the latest version in the workspace and the version Library

4 git log -- graph -- pretty = oneline -- abbrev-commit is better at viewing Branch records



3. Git Configuration


1. Initial Configuration

Git config -- global user. Name "your name"

Git config -- global user. Email "[email protected]"


2. Add remote repository and push

Git remote add origin [email protected]: lanbing510/learngit. gitre

Push git push-U Original master for the first time after Configuration

Adding-u git not only pushes the content of the local master Branch to a remote new master branch, but also associates the local master branch with the remote master branch, in the future, you can simplify the command: git push original master


3. Clone the GIT clone remote repository address


4. Ignore configuration files

① Create a special. gitignore file under the root directory of the GIT workspace, and then fill in the file name to be ignored, git will automatically ignore these files, some configuration files can see the https://github.com/github/gitignore

② Alternatively, run the GIT config -- global core. excludesfile ~ command ~ /. Gitignore_global add to git global configuration to reduce repeated rule definitions for each layer directory. For more information, see http://blog.csdn.net/lanbing510/article/details/40588323


5. Configure aliases

For example, after git config -- Global alias. unstage 'reset head', you can use git unstage to undo the modification of the temporary storage area (unstage) and put it back to the workspace.

Git config -- Global alias. Last 'Log-1' --> displays the last modified git last

Git config -- Global alias. LG "log -- color -- graph -- pretty = format: '% cred % H % creset-% C (yellow) % d % creset % S % cgreen (% Cr) % C (bold blue) <% an> % creset '-- abbrev-commit "Git LG better display log

The GIT configuration files of Each repository are stored in the. Git/config file.



Four Branches


1. Create and switch branches

Git checkout-B newbranch

The above command is equivalent to git branch newbranch and git checkout newbranch


2. Run the GIT branch command to view the current branch.


3. merge a branch to the current branch: git merge name

4. Delete branch: git branch-D name


5 disable fast forward: Add-no-FF to merge

If possible, git uses the "Fast Forward" mode when merging branches, but in this mode, branch information is lost after the branches are deleted. If you want to forcibly disable the "Fast Forward" mode, git will generate a new commit at merge, so that the branch information can be seen from the branch history.


6. When the task is not completed, you must fix the previous bug.

Git stash stores on-site work

Git checkout master

Git checkout-B fixbug

Git add

Git commit-M "Fix bug"

Git checkout master

Git merge -- no-Off-M "merge bug fix fixbug" fixbug

Git branch-D fixbug

Git stash list to list stash content

Git stash apply for Recovery + git stash drop Delete stash = git stash pop


7. Develop a new feature. It is best to create a new branch.

Git branch-D <Name> drop a branch that has not been merged


8. Push Branch

Git push origin master push master Branch

Git push origin Dev push new branch

Master is the master branch and must be synchronized remotely at any time.

Dev is a development branch. Team members need to work on it and need to be remotely synchronized.

The bug branch is used to fix bugs locally and does not need to be pushed to a remote server unless the boss needs

Whether the feature is pushed depends on whether you work with others for development.


9 git clone

After cloning from another computer, you can only see the master branch, but not the dev. To develop on Dev, you must create the dev branch of the remote origin to the local: git checkout-B Dev origin/dev


10 git push failed

The reason is that you have updated your local data remotely. First, git pull merge

If a conflict exists in the merge, resolve the issue. If git pull prompts no tracking information, the link between the local branch and the remote branch is not created: git Branch -- Set-upstream branch-name/ORIGIN/branch-name



Five tags


1 git Tag Name

Example: git tag v0.1 6224937

2. view the GIT tag

3 git show <tag> View Tag Information

4. You can also create a label with instructions. Use-a to specify the label name and-M to specify the description text: git tag-A v0.1-M "version 0.1 released" 3628164

5 use the PGP signature label: git tag-S <tagname>-M "blablabla..." you must first install GPG

6. Push a local Tag: git push origin <tagname>

7. Push all unpushed local tags: git push Origin -- tags

8. delete a local Tag: git tag-D <tagname>

9 delete a remote Tag: git push origin: refs/tags/<tagname>



Build a git Server


Reference http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137583770360579bc4b458f044ce7afed3df579123eca000




Seven Common Problems


1! [Rejected] Master-> master (non-fast-forward)

Solution:

① Git push-F for forced pushing

② Git fetch git merge


Summary: git cheat sheet






Git Common 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.