Git learning and common operations

Source: Internet
Author: User

· How git works diagram:


A file from the process of modification to submission:

Modify---workspace, where the state of the workspace is greater than staging area, the repository area is new

Add---The modified file is added to the staging area, and the workspace is the same as the staging area file state, which is newer than the state of the repository area.

Commit---The files in staging area are written to the repository area, when the workspace, staging area, and file status in the workspace are the same

Note: Workspaces, staging areas are temporary storage points, unsafe

The repository area is a secure point of storage, and all versions of the files are controlled in the repository area.

Description: Cloning from a remote git server to a local git repository, there is a hidden. Git directory, which is the location of the staging area and the repository, and do not delete, except that the. git directory is a workspace.


• Upload the download code using the git command

1. Gitconfig Configuration

Configure Gerrit Account and email

Gitconfig--global User.Name Gerrit_account

Gitconfig--global User.email and account-bound mailbox

Since this is a global configuration, it is OK to configure it locally only once.


2, Gitclone download code

$CD Working-tree

$gitclone ssh://[email protected][Code Server Ip]:29148/projectname

Note: The name of the ProjectName project library

Gerrit_account Replace with personal Gerrit account

IP: Replace with gerrit/git IP


3. Copy the hook script from the Gerrit server to the local library

$SCP –P 29418-p [email protected][code Server ip]:/hooks/commit-msgworking-tree/projectname/.git/hooks/

This is often a request to download when the push code fails, and after the name is executed, the push operation is rolled back and the push succeeds again.


4, local modification submit and push to Gerrit Web page

1) determine which branch code to modify, and establish a local branch to track the remote branch

$ CD Working-tree/projectname

$git Branch–r

Then decide which branch to modify, such as the code that modifies the main line master,

It is necessary to establish a master branch locally to track the branch of the remote repository.

$ git checkoutmaster (establishes the local master branch, tracks the master branch of the remote repository, and switches to the local master branch)


2) Modify the contents of the file or add a new file

$gitstatus (View status, you will be prompted what to do next)

$git Add file name

$gitstatus

$gitcommit –m "CR number content Simple description"


3) If you delete or rename a controlled file or directory,

$GITRM File/directory or $git MV file 1/directory 1 file 2/directory 2

$gitstatus (View status, find files deleted from workspace and staging area, or renamed files are already in the workspace-level staging area, the next step is to submit only)

$gitcommit –m "CR number content Simple description"


4) Push local branch modification to Gerrit Server Page

Git push Origin Branchname:refs/for/branchname

Brachname is the current local branch of the code library and can be viewed through the git branch command


5) The Code submitter logs on to the Gerrit page and adds a list of people to go to


git fallback operation

1: In the local project library, modify or delete the controlled file in the workspace, regret, want to fall back to the previous state (workspace file back to the state before the change, staging area, the library area is not affected)

git checkout--filename fallback a file

git checkout--. Fallback all modifications to the current directory

2: In the local project library, after modifying or adding or deleting a file, after executing the Git add action, regret, want to cancel the Add action (the workspace is not affected, the library area is unchanged, staging area back to the state before add)

git reset HEAD filename

git reset HEAD.

3: In the local project library, after modifying or adding or deleting a file, after executing git add, git commit action, want to cancel the Git commit action, want to re-commit once (the workspace, staging area unchanged, the library area back to the state before the commit)

git reset--soft head^

4: In the local project library, after modifying or adding or deleting a file, after executing git add, git commit action, want to cancel the add and commit action, but keep the workspace changes: (Staging area, the library area back to the previous state)

git reset--mixed head^

5: After modifying or adding or deleting a file in the local project library, after executing git add, git commit, you want to cancel the add and commit action, and discard the workspace changes:

git reset--hard head^

• Description: If you find that the fallback operation of class fifth is wrong, you want to go back to the previous state

# git Relog

# 2522695 [email protected]{0}: reset:moving tohead^

# git reset--hard [email protected]{0}

6: In the local project library, after modifying or adding or deleting a file, after executing git add, git commit, and has been pushed to the remote library, if fallback remote library this time?

The Commit-id (40-bit hash value) of this commit was found in the local repository via git log

Git revert-n commit-id

git commit–m "xxxxx"

Git push Origin Branchname:refs/for/branchname


git conflict resolution

(1) Executing GIT fetch origin

git rebase origin/master , ( Please replace master with the actual branch name )

We saw the conflict file ...
After confirming the existence of the conflict, execute git mergetool to start the GUI for code Fusion: The middle three regions correspond to the local base remote three versions (where base is the common base version, and local is the version that was merged before you joined. Remote is the version that prompts for conflicts when we hop in, the bottom area is the final version after the conflict is resolved and the exit is saved.

After the conflict file is resolved, delete the. orig file, execute the git rebase--continue command, and note: here mergetool has automatically executed git add for us command, you do not need to execute the command again.

Finally push, in the original out of the single will be more than a patch package, as shown below, re-audit, through the directly after.


(2) Less information on conflict files

How to operate the local library: Assuming that the local branch of the local library is the Master branch, and if it is a different branch, the following Master is replaced with the corresponding branch name

CD test

Git fetch origin

git rebase origin/master


git status Introduction

The git status command allows you to quickly locate the status of your local git library, and the print information from the git status command output can guide you through what to do next.


Use of git diff

diff keeps changing over workspace, staging area, repository area

git diff vs. workspace and Staging Area changes

The change of git diff--cached ratio to staging area and reservoir area

git diff HEAD vs. library and workspace changes


git log Introduction

' Git log ' is one of the most commonly used commands in Git, and after execution, it shows the commit history of the project. If the command does not add any arguments, then all the commit histories that are present on the branch, starting from the last commit, and sequentially in chronological order, are displayed.

Git logs the default output of the content format for the commit history:

* Submitted Checksum
* Author information (author name and email address)
* Date of submission
* Submit Information

In most cases, the output is quite perfect. However, sometimes you just want to see the last few submissions on a branch, or some interesting data, as long as the ' git log ' command is followed by a corresponding parameter, it can do more meaningful things.


Gittag Introduction

How to hit Tags:

1, here only to describe the commonly used methods of marking:

git tag-m "tag log" Tagnamecommit-id

Specific operation:

$CD Working-tree/projectname

$git Branch

*master

Label the Master Branch

$git Log

Find version number Commit-id

$gittag –m "label log" TagName Commit-id

$gitpush--tags #推送本地标签到远程, it is often necessary for an administrator to configure push permissions in the Gerrit Project library to

2. How to view the label

$gittag –l

List all the emoticons names

$gitshow tagname

Display the details of a label

$gitcheckout tagname

Get the tag's code

3. How to delete a label

$gittag –d tagname

Delete a label for a local library

$gitpush Origin:tagname

Delete the label for the remote library

Git learning and common operations

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.