Git basic Commands

Source: Internet
Author: User

Git is a distributed version control system



To create a version library
mkdir dirname New Directory
CD dirname
Git init turns this directory into a git repository

There is a. git hidden directory In this directory, do not change files in this directory



Add File
git add filename.txt



Submit the file to the warehouse
git commit-m "xxx"-M followed by the instructions for this submission



View the current status of the warehouse
git status



Compare what you've modified
git diff filename.txt



View the history of a commit
The git log command displays the commit log from the most recent to the farthest

git log--pretty=oneline output only one line per version (commit ID and commit description)

View the submitted command history
Git reflog

A large string of characters you see is the commit ID (version number)




In Git, the HEAD represents the current version, the previous version is head^, the last version is head^, and the first n versions are head~n


git reset--hard head^ fallback to previous version


Git reset--hard version number to the commit ID number corresponding to the version of the commit ID can not write full, but must write to the extent that git can differentiate




Concepts of workspaces and staging area

DirName is a working area.

The hidden directory in the workspace. Git, not a workspace, is a git repository


Git's repository contains a lot of things, the most important of which is called the stage (or index) of the staging area,
and the first branch master that git automatically created for us, and a pointer to master called head.

The git add command actually puts all the changes you want to commit to staging area (Stage), and then executes git commit to commit all the staging area changes to the branch at once.

Git manages the changes. The first time you modify filename.txt and execute git Add, commit it to staging area, the second modification filename.txt not add, execute git commit

You will find that only the first modification has been submitted and the second one has not been submitted. Git commit is only responsible for content in staging area

Each modification, if not add to staging area, it will not be added to the commit


Undo Changes
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-filename

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, in two steps, the first step with the command git reset head filename (head for the latest version), back to scene 1,
The second step is done by scene 1.

Scenario 3: When an inappropriate modification to the repository has been submitted, you want to undo this commit and roll back a version.




deleting files

RM filename

Git will log it and use git status to tell you which files have been deleted.

If you really want to remove it from the repository, delete it with Git rm and commit with Git

If you mistakenly delete git checkout--filename git checkout is actually replacing the workspace version with the version in the repository, which can be "one-click Restore" Whether the workspace is modified or deleted.



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

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

Thereafter, after each local commit, whenever necessary, you can use the command GIT push Origin master to push the latest changes

Cloning a repository, the address of a Git clone repository





View branches: Git branch

Create a branch: Git branch <name>

Switch branches: git checkout <name>

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

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

Delete branch: Git branch-d <name>



You can see the branch merge diagram with the git log--graph command.
git log--graph--pretty=oneline--abbrev-commit



Branching policy
Git merge--no-ff-m "merge with No-ff" Dev
Merge branches plus--no--ff are combined in normal mode,-m shows that the merged history has branches and can be seen to have been merged,
Typically, when merging branches, Git uses fast forward mode if possible, and this mode merges without ever merging.


Git stash
Storage of the current work site, can be resumed after the scene to continue to work, like a snapshot

Git stash list view saved work site

Resume work site
Git stash apply recovery, after recovery stash content will not be deleted, need to use git stash drop to delete

Git stash pop, restores stash content at the same time


Force deletion of a branch that has not been merged
Git branch-d <name>


View Remote library information, using Git remote-v;

Local new branches are not visible to others if they are not pushed to the remote;

Push the branch from the local, use GIT push Origin branch-name, if push fails, first crawl the remote new commit with git pull;

Create local and remote branches corresponding to the branch, using git checkout-b branch-name origin/branch-name, local and remote branch names are best consistent;

Establish the Association of local and remote branches, using Git branch--set-upstream branch-name origin/branch-name;

Crawl branches from the remote, use Git pull, and if there is a conflict, deal with the conflict first



Create a label


Command git tag <name> to create a new tag, the default is head, you can also specify a commit ID;

Git tag-a <tagname>-M "Blablabla ..." to specify tag information;

Git tag-s <tagname>-M "Blablabla ..." can be signed with PGP tag;

command git tag to view all tags.



Manage tags


Command GIT push origin <tagname> can push a local tag;

Command GIT push origin--tags to push all the local tags that have not been pushed;

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

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

This article from "Chu Water June" blog, please be sure to keep this source http://artvary.blog.51cto.com/10506823/1892717

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