Git Learning notes __git

Source: Internet
Author: User
Tags net command rollback tagname

First, understand a few basic concepts:

Origin: Default remote version library;

Master: Default Development Branch;


(1) Git log

View the Submit log. will show up every time you submit. As shown in figure:


(2) Git log--pretty=oneline

You can use this command if you feel that the output is too much. The information is displayed on one line. As shown in figure:


(3) Git branch

View the current branch. As pictured, I am now in the Master branch.



(4) Git reflog

Review the submission history to determine which version you want to roll back to. The rollback can be done based on the preceding hash value or head.


(5) Git diff filename

View the changes in your file before and after the comparison. Red "-" for deletion, green "+" for insertion. The process of modification is to delete first and then insert.


(6) Git reset--hard 7-bit hash value

Roll back to a version. The 7-bit hash value can be found by Git reflog. As shown in figure:



(7) Cat filename

View the contents of a file. As shown in figure:



(8) Git checkout--filename

To remove all changes to the file in the workspace, here are two things:

One is that the file has not been modified after the registers (that is, no git add), now undo the change back to the same state as the version library;

One is that the file has been added to the registers after the modification (already implemented git add), and made changes, undo the change back to the state after adding to the registers;

Anyway, let this file go back to the state of the most recent git commit or git Add.

Note that the middle "--" is important, no "--", becomes the "switch to another branch" command.


.


(9) git reset head filename

Remove the registers changes (Unstage) and put them back in the workspace.

The git reset command can either roll back the version or registers the changes back to the workspace, when we use head to represent the latest version.



(ten) git rm filename

Adds a file to the workspace and adds it to the local branch. But if you don't need this file. You delete the file directly using "RM filename". At this point, git knows you've deleted the file, so the workspace and version library are different. Git status can see the current situation.

There are two choices at this time:

1. You are sure that you want to delete the file from the version library by using the command "git rm filename" and that the file is actually deleted from the version library when Git commits to commit.

2. Another situation is the deletion of the error, because the version of the library also has the file, so you can delete the deleted files to the latest version, "Git checkout--filename." Git checkout replaces the workspace version with a version in the version library, and can be "one-click", whether the workspace is modified or deleted.


.


(one) Git remote add Origin ***github warehouse address

Associates a remote warehouse. Prior to the first submission.


(in) Git remote-v

View the address of the remote warehouse.


Git remote RM origin

Deletes the original remote warehouse address.


() Git push-u origin Master

First push all the contents of the Master branch.


Git push Origin Master

This command is used for each subsequent modification.


(16) About branches:

In version rollback, every time you commit, git strings them into a timeline, which is a branch of the timeline. So far, there's only one timeline, in git, this branch is called the main branch, the Master branch. The head pointer is not strictly directed to the submission, but to the Master,master is the point of submission. So, head is pointing to the current branch.


git checkout-b Dev

Create a dev branch and switch to the Dev branch.

.


(17) The above order is equivalent to the following two orders:

Git branch dev: Create a new dev branch;

git checkout dev: switch to Dev branch;


(a) Git branch

View all branches locally. The current branch of the presentation preceded with an asterisk *.


Attention:

Git branch-a: View all branches, including local and remote branches.

The local branch is shown in green and black, and green indicates that you are now in the branch and that black is the other branch. The remote branch is represented in red.


Git branch-r: View all branches of the remote, all in red.


git branch-d dev: Deletes a local branch, such as the Dev branch.


(20) Merging branches

1. Git checkout-b dev: first create a dev branch, and switch to Dev branch;


2. Make a modification file and submit it.

.


3.cat main.m

Dev Branch under

Found inside I added "//dev branch" these words;







4.git Checkout Master: Now that the Dev branch is done, we can switch to the master branch.

.


5.cat Main

Master Branch.

It was found that there was no "//dev branch" because the submission was on the Dev branch, and the submission point for the Maser branch was unchanged at the moment.

。.


6. Git merge dev

Merge the work results of the Dev branch into the master branch.

The git merge command is used to merge the specified branch to the current branch. After merging, when you view the contents of the file, you can see that the latest submissions from the Dev branch are exactly the same.


7. The "Fast-forward" information above, Git tells us, this time the merger is "Fast forward mode", that is, direct the master to point to the current submission of Dev, so the merging speed is very fast.

8.git branch-d Dev

After the merge is complete, you can safely delete the dev branch.

.


9. Branching summary: Because creating, merging, and deleting branches is very fast, git encourages you to use a branch to accomplish a task, merge and then delete the branch, which works the same way directly on the master branch, but the process is more secure.



(21) Conflict resolution

1.git Checkout-b testconflict

git Add.

Git commit-m "testconflict commit, test conflict"

Creates a new Testconflict branch to test for conflicts. and modifies one line of code;

.


2. Git checkout Master

git Add.

git commit-m "test"

Under Master Branch, the same line of code is modified and submitted;

.


3.git Merge Testconflict

At this point, the two branches have their own new commits, at which point git cannot perform a "quick merge", only the view merges their own changes, but there is a conflict between the merges. The results of this merge are as follows:

.


4.git prompts us to have a conflict. You can see which file has a conflict through the git status command. As shown in figure:


5. We can go directly to the conflict file view, you can see the following code:

Where the <<<<<,=====,>>>>>>, there is conflict. Can be modified manually, after the modified code is as follows:

Then you can submit it under master. As shown in figure:


6.git Log--graph

You can see the merge of the branches, as shown in figure:

Note above: "merge:5da331c 1225c73" is to merge the following branches into the previous branch. That is, the Testconfict branch is merged into the master branch. This 7-digit number is the first 7 digits of the serial number behind the commit.

The serial number behind the commit is a hash value, using the SHA-1 hash algorithm, with a length of 40 bits. is a hash value based on the contents of your file.


7.git Log--graph--pretty=oneline

You can have the output displayed on one line, as shown in figure:

.


8. Finally, the Testconflict branch can be deleted. Note: When git cannot automatically merge branches, it must first resolve the conflict. After resolving the conflict, submit again, merge complete.


(22) Branch management strategy

1. When you normally merge a branch, Git uses fast forward fast mode if you can, but in this mode, the branch information is discarded after the branch is deleted. We can use the--no-ff way of git merge.

2.git Checkout-b Dev

Still create and switch to the Dev branch.

3.git Add.

git commit-m "Merge--no-ff"

Submit under the Dev branch.

4.git Checkout Master

Switch to the Master branch.

5.git Merge--no-ff-m "merge with No-ff" Dev

The merge uses the--NO-FF parameter, which indicates that the fast Forward is disabled. Because this merge is going to create a new commit, so add the-m parameter.

6.git Log--graph--pretty=oneline--abbrev-commit

View Branch history. Note Use the--abbrev-commit parameter to shorten the commit's 40 hash value to 7 bits for easy viewing.


7. Branch Strategy:

First of all, the master branch should be very stable, that is, only to release the new version, usually can not work on it. Work is on the Dev branch, that is, the dev branch is unstable, and at some point, such as Release 1.0, merge the Dev branch into master and release the 1.0 version on Master. Everyone has his or her own branch, and it's OK to merge on the Dev branch.

Merging branches, plus the--NO-FF parameter can be merged in normal mode, after the merged history has branches, can see once did merge. The Fast forward merger does not see a merger. The default is fast forward.





(%) Bug Branch

Each bug can be repaired by a new temporary branch, after the fix, merge the branch, and then delete the temporary branch.

1.git Stash

Store the current work site, and continue to use it later after replying to the site.

git stash List

View the stash stored in the current branch


2.git status

Viewing the workspace at this point in the Dev branch is clean. Therefore, you can safely create branches to fix bugs.


3.git Checkout Master

Git checkout-b issue-101

First determine which branch to fix the bug on, assuming that you want to fix it on the master branch and create a temporary branch from master.


4. Fix the bug on the issue-101 and submit it.


5.git Checkout Master

git merge--no-ff-m "fix complete, merge issue-101 branch" issue-101

git log--graph--pretty=oneline--abbrev-commit

Merge bug branches.


6.git branch-d issue-101

Delete the bug branch.


7.git Checkout Dev

git status

Back to the Dev branch again, the work site is completely clean.


8.git Stash List

View the current work site. Git stores the stash content somewhere. We need to restore the scene.

.


9.git Stash Pop

git stash List

In the restoration of content while the contents of the stash also deleted.

.


10.git Stash Apply

git stash Drop

These two lines of command are equivalent to "git stash pop." "Git stash apply" restores, but after the recovery, stash content is not deleted, you need to use "Git stash drop" to delete.


11.git Add.

Git commit-m "submit in Dev Branch"

Submit the content of the dev branch work. Wait to merge to the Master branch.


12.git Checkout Master

Git merge--no-ff-m "The work content of the merged dev branch" Dev

If there is a conflict, modify it manually.


13.git Add.

Git commit-m "Consolidated Dev's master commit"

git branch-d Dev

After the conflict is resolved manually, commit in master, and then delete the dev branch.


14. Summary: When fixing a bug, we fix it by creating a new bug branch, then merging, and finally deleting it.

When the work is not finished, first git the work site stash, and then fix the bug, fix it, then git stash pop, back to work site.



(feature) Branch

1. Add a new feature and you certainly don't want to mess up the main branch because of some experimental code. So, each add a new feature, it is best to create a new feature branch, developed on it, completed, merged, and finally deleted feature Branch.

When development is complete, this new feature is not needed until it has been merged. You need to delete the branch.

Git branch-d feature-net

.


2.git branch-d feature-net

Note: The deletion in 1 cannot succeed because the branch has not been merged and, if deleted, changes will be lost. If you want to forcibly delete, use the git branch-d feature-net command.

3. Summary: Develop a new feature, preferably a new branch. If you want to discard a branch that has not been merged, you can forcibly delete it through Git branch-d <name>.


() git remote

View the information for the remote library.

Git remote-v

Displays detailed information. Shows the origin addresses that can be crawled and pushed. If there is no push permission, the push address is not visible.



(26) Multi-person collaboration

1. Push branch: All local commits on this branch are pushed to the remote library. When pushing, you specify the local branch so that git pushes the branch to the remote branch of the remote library.

Git push Origin Master

If you want to push to another branch, such as Dev, change it to:

Git push Origin Dev

But not necessarily the local branch to the remote push, then, which branches need to push, which branches do not need?

The--master branch is the main branch, so it should be synchronized with the remote at all times;

--dev Branch is the development branch, all members of the team need to work on the above, so also need to sync with the remote;

The--bug branch is only used to fix bugs locally, and there is no need to push them to the remote;

The--feature branch can look at actual development needs.


2. Crawl Branch

git checkout-b Dev Origin/dev

Create a remote Origin dev branch to the local.


Git branch--set-upstream Dev origin/dev

Set up a link to the local dev and remote Origin/dev branch.


3. Multi-person Collaboration model:

-First, you can try to push your own changes with GIT push Origin branch-name;

If the push fails because the remote branch is newer than your local branch version, you need to merge with git pull view first;

-If there is a conflict, resolve the conflict and submit it locally;

-No conflict or conflict resolution, then use GIT push origin branch-name push to succeed.

--If Git pull prompts "No tracking information", the link between the local branch and the remote branch is not created, and the command "Git branch--set-upstream branch-name origin/ Branch-name ".

4. Summary

--View Remote library information, use "Git remote-v";

--A new local branch is not visible to others if it is not pushed to the remote;

--from the local push branch, using "Git push Origin branch-name", if the push failed, first use "Git Pull" crawl remote new submissions;

--in the local library to create the branch corresponding to the remote branch, using the "Git checkout-b branch-name origin/branch-name", the local and remote branch name best consistent;

--Establish an association of local and remote branches, "Git branch--set-upstream branch-name origin/branch-name";

--From a remote crawl branch, use git pull. If there is a conflict, first deal with the conflict.



(27) Label Management

When we publish a version, we usually start with a label in the repository, so that we have the only one that determines the time of the tag. Whenever you take a label version, you take the historical version of the label moment. Therefore, the label is also a snapshot of the version library.

The gitde tag is a snapshot of the version library, but in fact he is a pointer to a commit. So, creating and deleting tags is instantaneous.



(28) Create labels

git Tag v1.0

Switch to the branch that you want to label, perform "Git Tag v1.0", and the default is on the most recently committed commit.


git tag

View all the labels.

.


git tag v1.1 commit_id

Tags can be hit on any one of the submissions.


Git Show v1.0

View the label information.

.


(29) Operation label

1.git tag-d v1.0

Because the labels you create are only stored locally, they are not automatically pushed to the remote. Therefore, the wrong label can be safely removed locally.


2.git Push Origin v1.0

Push a tag to a remote branch.



3.git Push Origin--tags

A one-time push all has not been pushed to the remote local label.



4. If the label has been pushed to the remote, to remove the remote label is a bit of a hassle, first delete from the local

Git tag-d v1.0

Then remove from the remote. The Delete command is also push, but the format is as follows:

git push origin:refs/tags/v1.0

.


5. Summary:

--command "Git push Origin <tagname>": can push a local label;

--command "Git push Origin--tags": can push all not push local label;

--command "Git tag-d <tagname>": You can delete a local label;

--command "Git push origin:refs/tags/<tagname>": You can delete a remote tag.








This article refers to the Liaoche git tutorial. Thank.


GitHub home: https://github.com/chenyufeng1991. You are welcome to visit.



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.