"Bad writing" git common commands

Source: Internet
Author: User
Tags using git

Git is the most commonly used version control tool in our daily routine. Compared to Svn,git has the following advantages: (1) Git is distributed management, does not need a separate server storage code, each machine in the team can be a server, are independent of each other. The biggest advantage is that, in the case of network disconnection, git can still be used to manage the local commit and other version management operations. After the network is restored, you only need to push all the commits to the remote git server. (2) Switching branches is very fast, almost instantaneous, the management of the branch is more efficient than SVN, SVN branch switching is too slow, resulting in this function is a dummy.

In this article, the usual commands used in peacetime are recorded, most of which are the commands they have used in peacetime. When learning and using tools, the following two sites are the main references:

https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

Https://git-scm.com/book/zh/v2

First, the relevant configuration

1. View git configuration information $ git config--list

2, modify the configuration information (usually used, only modified user.name and User.email)

$ git config --global user.name "Your Name"

   $ git config --global user.email "[email protected]

3, $ git init turns the current directory into a git repository

Second, local git operation

1, $ git Status View new modified file status

2, $ git checkout filename Restore modified file (based on the results of your presentation, get the conclusion)

(1) When the Unstage area (that is, only modified but not add) is available, and the filename file is not in the stage area, the command restores the filename file to the file in the latest commit

(2) When the Unstage area is there, the stage section also sometimes, after the command is executed, the Unstage file reverts to the contents of the stage

(3) When there is no in the Unstage area, the stage section sometimes, after the command is executed, the stage area file is not changed, you need to use the git reset HEAD filename Restore (later on the command)

3, $ git add adds a new modified file in the workspace to the stage area

FileName adds the specified file to the stage area

Dir adds a folder to the stage area

. In Git version2.x, add any files under the current folder that have changed (modified, deleted, newly created) to the stage area. Only newly modified and newly created files are added to the stage area in Git version1.x.

-U (UPDATE) adds all the tracked files to the stage area, from the test situation, the deleted files and the newly modified files are tracked, the newly created file is the untracked file, the git status can be viewed (simply put, it will be deleted, The newly modified file is added to the stage area).

-A (all) submit all changes (modified, deleted, newly created) to the stage area

--ignore-removal newly created files and newly modified files are added to the stage area. The deleted file is ignored. git add in git version1.x. function as well.

-H Display Help information

4, $ git reset head can restore the change in the stage to the Unstage area (that is, the state before add), in detail can refer to the 6th.

5, $ git commit

-A (--all) will be track files submitted. Include files that have not been add.

-M (--message) "message" adds a file from the stage area to the current branch with log

--amend will pop up an edit page and edit the contents of the last commit.

-C (--reuse-message) <commit> use and Commit-id the same submission information (can be used to supplement submissions)

--reset-author re-set the author.

6, $ git reset points The warehouse submission pointer to a version (either forward or rewind)

The current change remains unchanged after the--soft pointer is moved.

When the--hard pointer moves, the current change is reverted to the unmodified state.

When there is no hard/soft, the change in the stage area is restored to the Unstage area.

The Commit-id,head pointer points to the submission point version of the Commit-id.

HEAD indicates that the current version head^ represents the previous version, multiple ^ can be used together, the reader can query on their own.

7, $ git revert Commitid will have submitted an ID of Commitid record before returning to the commit. will produce a new commitid, note that the reset will remove the corresponding commit record when it is distinguished from reset.

8, $ git diff

File adds a file name, which is compared against a certain document.

No parameters (1) If the stage area has, then the work area and the stage to do the comparison

(2) If the stage area is not, the comparison is done in the workspace and branch

Compare the head comparison workspace with the file in the repository that the head pointer points to

--cache staging area and branches are compared.

9, $ git rm

10, $ git stash hide all tacked (modified and deleted) Change,stash is hidden meaning. Both the Unstage area and the stage area can be saved by stash hiding.

untracked files cannot be stash (results found during testing), you need to add to the stage area first, and then stash. After the restore is reused, the tracked file will be transferred to the state before Add.

(1) Save <msg> where <msg> means to make some comments about the currently hidden information, you can not fill in. From the test results, git stash and git stash save when you don't add <msg>.

(2) List shows all currently stash information

The above three messages correspond to the preservation of stash (msg information is intercepted part):

[Email protected] {0}--$ git stash save "test stash Save"

[Email protected] {1}--$ git stash save, where ":" After the information is added automatically to Git, representing the latest submitted Commt-id and submission information in the current repository

[Email protected] {2}--$ git stash

(3) Pop remove stash stack top (ie [email protected]{0}) information, and delete the message

(4) Apply [email protected]{n} specifies which stash record to use, but the record will not be affected.

(5) Drop stash [email protected]{n} Deletes the specified stash record, and the record is not restored to the workspace.

(6) Clear Delete all stash records.

(7) Recovery of deleted stash records

1) Find the deleted record ID, as in point (5)

To view records that were previously deleted: Git fsck--lost-found

2) Restore the modification record according to the commit ID

Command: git merge Commitid

By the above steps, the stash record that was mistakenly deleted is returned to the code, but if you want to get these changes back to the Unstage area, you need to perform git reset head^

10. Create a local branch

(1) $ git branch new-branch-name

(2) $ git checkout-b new-branch-name after the branch is created, the current branch switches to the newly created branch.

11. Switch branches (note the difference from creating local branch-B parameters)

$ git checkout brance-name

12. Merging branches

$ git Merge Branch-name

At this point, the changes made in the master branch are merged into the Test-branch.

13. Pull Remote Branch content

$ git pull--rebase (the general recommendation is to use--rebase, which makes the submission line more elegant, you can go online to check git pull, and git pull--rebase difference)

14. Submit local branch to remote branch

$ GIT push origin Branch-name

Third, remote branch:

1, view the remote path, pull the version of the code when the path (depending on the permissions, fetch and push are not necessarily displayed)

$ git remote-v

2. View Remote Branch

(1) $ git Branch view local branch (* for current branch)

(2) $ git branch-a view local and remote all branches (-av can display the latest commit record for each branch)

3. Create a remote branch

Create a local branch dev1, and then push to a remote git server.

(1) $ Git push Origin local-branch-name:remote-branch-name (: No spaces before and after, local branch and remote branch preferably with the same branch name for easy identification)

(2) $ Git push Origin dev (also the command when submitting a local branch modification)

4, delete remote branch (online check data, delete the command there are many, using the following two ways)

(1) $ git branch-r-D origin/branch-name

(2) $ git push origin:branch-name (sometimes valid, sometimes invalid, reason not found)

5. Renaming a remote branch

Online search of some information, is generally said cannot be directly modified, indirect way

(1) Delete the remote branch first

(2) Renaming local branch $ git branch-m branch-name new-branch-name

(3) Push the local branch to the remote repository

6. View the update record for a remote branch

$ git reflog show--date=iso origin/master

7. Pull remote Branch and create local branch

(1) Git branch-r

To view the remote branch name

(2) Git checkout-b local branch name Remote branch name

Create and switch to local branch

Iv. Use of Log

1. View commit record $ git log

--filename Displays the submission information for a file

-N Displays the number of log bars

-P show diff per submission

--grep=str find commit records by keyword

2, view a record of the submission diff and other information git show Commit-id

FileName to view information about a file in a commit

3. View the submission record for a file

git log--pretty==online filename find Commitid and then combine commit show to view

4. View the commit record somewhere in a file

$ git blame-l line number, + column number file path name

The current command represents viewing the commit record in row 75th, column 5th, of the Faceidsdk.java file

Add git's Ignore file.

In project development, some files do not want to be committed to git, you can create a. gitignore file, add a file path that does not require git identification, and commit to Git. No need to write the file from scratch .gitignore , GitHub has prepared a variety of configuration files for us, just need to combine to use. All profiles can be viewed directly online: Https://github.com/github/gitignore

The principle of ignoring files is:

(1) Ignore the operating system automatically generated files, such as thumbnail images;

(2) Ignore the compilation of intermediate files, executables, etc., that is, if a file is automatically generated through another file, then the automatically generated files will not need to put into the repository, such as the Java compiler generated .class files;

(3) Ignore your own profile with sensitive information, such as the configuration file that holds the password.

Six, alias configuration

GIT provides the ability to use aliases instead of commands, for example, git status, configuring the alias St for status, directly using Git St. For some commonly used, but the command is long, and not easy to remember the situation, the use of aliases has brought great convenience.

Configure your favorite aliases in such a format. The configured aliases are global and are valid in all Git repositories. There is a corresponding record in the configuration file, my profile path is: C:\Users\rd0489\.gitconfig

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 Run effect

Is it very insane? -_-

"Bad writing" 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.