Git basic tutorial

Source: Internet
Author: User
Tags git mergetool

Git basic tutorial

The following is actually a basic tutorial for Git use within the company. I will record it here.

Modify Git configurations

Windows operating system line feed Configuration
  1. git config --global core.autocrlf input
User name and email Configuration
  1. git config user.name yourname --local
  2. git config user.email yourname@tudou.com --local

Clone v3 code library

  1. git clone git@gitlab.intra.tudou.com:static/v3.git

View Branch

List all local branches
  1. git branch
List all remote branches
  1. git branch -r

Create Branch

Create based on local branch
  1. git branch [BRANCH_NAME]
  2. git push origin [BRANCH_NAME]
Create a remote Branch
  1. git branch [BRANCH_NAME] origin/master

Bind a local branch to a remote Branch

After binding, the operation can be omitted. Origin [branch], which is more convenient and efficient to use. Of course, if multiple remote operations are required, it cannot be omitted.
  1. git push -u origin [branch]

Switch Branch

Switch to the local branch. If the local branch does not exist, the system automatically searches for the remote branch.
  1. git checkout [BRANCH_NAME]

Update Branch

Update master

  1. git pull --rebase origin master

Resolves rebase conflicts.

  1. git status

# Open the corresponding file as prompted, search for the "<" character, and execute

  1. git add -u
  2. git rebase --continue

# Stop the pull Operation and return to the status before pull rebase.

  1. git rebase --abort

Code submission

Submit to local version Library
  1. git commit -m "commit message"

Submit to remote master
  1. git push origin master

Merge Branch

Switch to the master.

  1. git checkout master

Merge branches.

  1. git merge --no-ff [BRANCH_NAME]

Resolves conflicts in the src directory.

  1. https://help.github.com/articles/resolving-a-merge-conflict-from-the-command-line

Use external tools to resolve conflicts

Resolves all conflicts in this merge.
  1. git mergetool
Resolve conflicts with specific files

Git mergetool src/js/g. js
Re-build all conflicting files under the build and dist directories.

  1. ytpm [PATH]

Submit the code.

  1. git add .
  2. git commit -m "commit message"

Restore code

Discard local modification (discard all local uncommitted files, consistent with remote)

  1. git reset --hard origin/master

Restore A submission

  1. git revert [COMMIT_ID]

Restore merge

  1. git revert -m 1[MERGE_COMMIT_ID]

Restore to the specified version

  1. git reset --hard [COMMIT_ID]

Delete Branch

Delete local branch
  1. git branch -d [BRANCH_NAME]
Delete remote Branch
  1. git push origin --delete[BRANCH_NAME]

View change records

View change history
  1. git log --decorate --numstat [PATH]
View A submission record
  1. git show --name-only [COMMIT_ID]

Comparison File

Git default mode

Compare the workspace version with the temporary workspace version.
  1. git diff --[PATH]
Compare the version of the temporary storage area and version Library
  1. git diff --cached --[PATH]
Compare the workspace version and version library version (HEAD)
  1. git diff HEAD --[PATH]
Compare two submitted files
  1. git diff [COMMIT_ID_1][COMMIT_ID_2]--[PATH]
Compare two branch files
  1. git diff develop master -- src/js/g.js
  2. git diff develop:src/js/g.js master:src/js/g.js
View files on other branches
  1. git show master:src/js/g.js

Comparison with external comparison tools

Compare all the modifications in this merge (only compared with the records before submission)
  1. git difftool
Compares the historical versions of a specified file.
  1. git difftool [GitHash] HEAD -- src/js/g.js

Solution when developing on develop accidentally

Not submitted
  1. git stash
  2. git checkout [BRANCH_NAME]
  3. git stash pop
Submitted
  1. Git log-1 # Remember the COMMIT ID
  2. git reset --hard origin/develop
  3. git checkout [BRANCH_NAME]
  4. git cherry-pick [COMMIT_ID]

Solution when you accidentally merge the branches created by develop or based on develop into the master

  1. A--M---B <---develop
  2. \
  3. --C--G---J---P---Q <---master

In the preceding example, you need to confirm the error submission point of J and the hash value of the previous and later submission points, and ensure that the current HEAD is on Q, and then execute:

  1. git rebase -i -p --onto P G

After execution, you need to manually resolve the conflict after each submission. After manual comparison, You can execute the following commands:

Git add. // add this conflict Modification

  1. git rebase --continue

Note that the above steps may be performed many times, and each time the above method is used. For manual comparison methods, see [external comparison tool] [1.

Others
Beyond Compare comparison tool Configuration
In ~ /. Add the following configuration to the gitconfig file. The comparison tool is Beyond Compare. You can download it by yourself. If you are a win user, you need to modify the corresponding path.

In addition, Beyond Compare 4 for Mac also needs to be set to support calling through command Line: Click the menu-> Install Commond Line Tools... and then enter the system logon password.
[Merge]
Tool = bcomp
[Mergetool]
Prompt = false
KeepBackup = false
[Mergetool "bcomp"]
TrustExitCode = true
Cmd = "/usr/local/bin/bcomp" "$ LOCAL" "$ REMOTE" "$ BASE" "$ MERGED"
[Diff]
Tool = bcomp
[Difftool]
Prompt = false
[Difftool "bcomp"]
TrustExitCode = true
Cmd = "/usr/local/bin/bcomp" "$ LOCAL" "$ REMOTE"

Common git Alias

  1. [alias]
  2. st = status --short--branch
  3. pu = pull --rebase
  4. ca = commit --amend
  5. ci = commit -a -v
  6. br = branch
  7. bv = branch -vv
  8. co = checkout
  9. cb = checkout -b
  10. df = diff
  11. un = reset --hard HEAD
  12. uh = reset --hard HEAD^
  13. ll = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]"--decorate --numstat
  14. ld = log --pretty=format:"%C(yellow)%h\\ %C(green)%ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]"--decorate --date=short--graph
  15. ls = log --pretty=format:"%C(green)%h\\ %C(yellow)[%ad]%Cred%d\\ %Creset%s%Cblue\\ [%cn]"--decorate --date=relative

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