Common Git operation skills

Source: Internet
Author: User
Tags temporary file storage

Common Git operation skills

I have been learning Git for more than a year, and some common skills are also very powerful. But sometimes I Have Some uncommon skills, I always need to go to Google, and I forget it again in the second time. Therefore, this is a Git skill table that I think is important to me. It is mainly used for self-check. Repeated check can help to deepen my impression and improve skill proficiency. If you do not know what Git is, read Liao Xuefeng's Git tutorial first.

Workflow

A core concept of Git is workflow. The Workspace is the actual directory in the computer. The Index area is like a cache area, saving your changes temporarily. The last is the version Library (Repository ), it can be divided into local warehouses and remote warehouses. It's really amazing.

Remote Repository

Add remote Repository

Git remote add origin git @ server-name: path/repo-name.git # add a remote Library

View remote Repository

Git remote # view the remote database information
Git remote-v # Show more details

Push Branch

Git push origin master # push to remote master Branch

Capture Branch

Git clone git @ server-name: path/repo-name.git # clone remote repository to local (master branch can be seen)
Git checkout-B dev origin/dev # create the dev branch of the remote origin to the local device and name it dev
Git checkout origin/dev -- track # same effect as above
Git pull origin master # update from remote Branch
Git fetch origin master # obtain data on a remote Branch

$ Git branch -- set-upstream branch-name origin/branch-name, which can be used to establish associations between local branches and remote branches, and then git pull can directly capture branches remotely.

In addition, git pull = git fetch + merge to local

Delete remote Branch

$ Git push origin -- delete bugfix
To https://github.com/wuchong/jacman
-[Deleted] bugfix

Update remote Branch Information

As the project moves forward, some branches are often added to and deleted from the remote repository. So sometimes you need to remotely synchronize the branch information.

Git fetch-p

-P indicates the trim. After fetch, it deletes the local branch that does not correspond to the remote branch, and synchronizes some newly created remote branches and tags.

History Management

View history

Git log -- pretty = oneline filename # display in one row
Git log-p-2 # display the difference between the last two submissions
Git show cb926e7 # view a modification

Version rollback

Git reset -- hard HEAD ^ # Roll back to the previous version
Git reset -- hard cb926e7 # Roll back to a specific version
Git reflog # view command history, which is often used to help retrieve lost commit

Use HEAD to represent the current version. The previous version is HEAD ^, and the previous version is HEAD ^, HEAD ~ 100 is the last 100 versions.

Manage changes

Git status # view the status of the workspace and temporary storage area
Git checkout -- <file> # discard modifications to a file in the Workspace
Git reset HEAD <file> # discard the modification of a file in the temporary storage area and put it back in the workspace.

View differences

Git diff # view unsaved File Updates
Git diff -- cached # view updates to saved files
Git diff HEAD -- readme.txt # view the difference between the latest version in the workspace and version Library
Git diff <source_branch> <target_branch> # preview the differences between the two branches before merging changes

Use the built-in graphical git: gitk to view the differences more easily and clearly. Of course, the Github client is also good.

Delete an object

Git rm <file> # delete a file directly
Git rm -- cached <file> # Delete the temporary file storage status

Storage and recovery

Git stash # store the current job
Git stash list # view the storage site
Git stash apply # restore the work site. The stash content is not deleted.
Git stash pop # restore the work site and delete the stash content

Branch Management

Create Branch

Git branch develop # create branches only
Git checkout-B master develop # create and switch to the develop Branch

Merge Branch

Git checkout master # Switch to master Branch
Git merge -- no-ff develop # merge develop to the master branch. The no-ff option is used to keep the original branch record.
Git branch-d develop # Delete the develop branch

Tag

Show tags

Git tag # list existing tags
Git show <tagname> # display Tag Information

Create tags

Git tag v0.1 # new tag, default Bit HEAD
Git tag v0.1 cb926e7 # tag the specified commit id
Git tag-a v0.1-m'version 0.1 released' # create a tag with a comment

Operation tag

Git checkout <tagname> # Switch to the tag

Git push origin <tagname> # push branch to source
Git push origin -- tags # One-time push of all the local tags that have not been pushed to a remote device

Git tag-d <tagname> # delete a tag
Git push origin: refs/tags/<tagname> # delete a remote tag

Git settings

Set the commit user and email address

Git config user. name "xx" # Set the commit user
Git config user.email.com "xx@xx.com" # Set the commit mailbox
Git config format. pretty oneline # When the history is displayed, only one row of information is displayed for each commit.

Git Tutorials:

GitHub tutorials

Git tag management details

Git branch management

Git remote repository details

Git local Repository (Repository) Details

Git server setup and Client installation

Git Overview

Share practical GitHub tutorials

How to Build and use Git servers in Ubuntu

Git details: click here
Git: click here

This article permanently updates the link address:

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.