Common operations in Git work

Source: Internet
Author: User

  Start your work, turn on your computer,gitPull: Get the latest code from Git;

When you edit the code and prepare the commit,git stash: Save your edited code and prevent Git pull from conflicting with the code in the library, or your code will be white.

Then,gitpulls: Pull the code, and the code in the library, to do synchronization, conflict is resolved conflict, if you save this step, others have submitted the code, no update, the submission will be error, then go this step, will put someone else's code pulled out, and then submitted together, It is equivalent to you submit your own code, but also to submit the code of others, and sometimes this will make the code in the library, other people's efforts will be lost, you are the culprit;

Then,git status: View the status of the file, red indicates that the modified file has not been submitted, green for the submitted modified files, can be omitted, the initial proposal to view the comparison before and after the submission;

Then,git add: Add the submitted file, and you can add it all. can also be single or multiple add, git add followed by the file name, do not know the name can refer to the GIT status list of modified files;

Then,git commit-m "file modification Instructions" to develop a good habit, the instructions written clearly, convenient check and version recovery;

Then,git status : To see whether the file is submitted, especially suitable for a single file submission, to view the missing files submitted, can be omitted;

Finally,git push : Push the code into the Git repository;

The above operation, only in the normal development of use, development will encounter a variety of problems, Git also gives a lot of methods, the following I copied the others summarized git command Daquan, for reference only use, do not do any commercial use; There are errors please indicate

First, create a new code base

# 在当前目录新建一个Git代码库$ git init# 新建一个目录,将其初始化为Git代码库$ git init [project-name]# 下载一个项目和它的整个代码历史$ git clone [url]

Second, the configuration

Git's settings file is .gitconfig , it can be in the user's home directory (global configuration), or under the project directory (project configuration).

# Displays the current git configuration $ git config --list# edit git config file $ git config -e [--global]# set user information when submitting code $ git config [--global User.name  "[name]" $ git config [--global< Span class= "token punctuation" > User.email                

Third, add/delete files

# Add the specified file to staging area $ git add[File1][File2]...# Add the specified directory to staging area, including subdirectories $ git add[dir]# Add all files of the current directory to staging area $ git add.# before adding each change, you will be asked to confirm# for multiple changes to the same file, you can implement a split commit $ git add -p # delete workspace file and put this deletion into staging area $ git rm [file1] [file2 # stop tracking the specified file, but the file will remain in the workspace $ git rm --cached [file]# rename file and put this name in staging area $ git mv [file-original [file-renamed]   

Iv. Code Submission

# Commit staging Area to warehouse area $ git commit-M[Message]# Commit Staging area file to warehouse area $ git commit[File1][File2]...-M[Message]# submit workspace changes since last commit, go directly to warehouse area $ git commit -a# Show all diff information when submitting $ git commit -v# use a new commit, instead of the last commit # if the code does not have any new changes, it is used to overwrite the commit information of the last commit $ git commit Span class= "token operator" >--amend -m [message]# redo the last commit and include a new change to the specified file $ git commit --amend [file1 [file2" .            

V. Branches

# List all local branches $ git branch# list all remote branches $ git branch-R# list all local branches and remote branches $ git branch-A# Create a new branch, but still stay on the current branch $ git branch[Branch-name]# Create a new branch and switch to the branch $ git checkout-B[Branch]# Create a new branch, point to specify commit$ git branch[Branch][Commit]# Create a new branch to establish a tracking relationship with the specified remote branch $ git branch--track[Branch][Remote-branch]# switch to the specified branch and update the workspace $ git checkout[Branch-name]# Switch to previous branch $ git checkout-# Establish a tracking relationship between the existing branch and the specified remote branch $ git branch--Set-upstream[Branch][Remote-branch]# merge specified branch to current branch $ git merge [branch]# Select a commit, merge into current branch $ git cherry-pick [commit]# Delete branch $ git branch Span class= "token operator" >-d [branch-name]# delete remote branch $ Git push origin -- Delete [branch-name ]$ git branch -dr [ Remote/branch]      

Six, label

# list all tag$ git tags# Create a new tag in the current commit$ git tag[Tag]# Create a new tag in the specified commit$ git tag[Tag][Commit]# Delete local tag$ git tag-D[Tag]# Delete remote tag$ Git push origin: Refs/tags/[tagname]# view tag info $ git show [tag< Span class= "token punctuation" >]# submit specified tag$ git push [ Remote [tag]< Span class= "token comment" ># submit all tag$ git push [remote  --tags# create a new branch, Point to a tag$ git checkout -b [branch" [tag     

Vii. Viewing information

# show changed files $ git status# shows the version history of the current branch $ git log# Show commit history, and file $ git log for each commit change--stat# Search Commit history, according to keyword $ git log-S[keyword]# shows all changes after a commit, each of which occupies one line of git log[Tag] HEAD--pretty=format:%s# shows all changes after a commit, and its "submit description" must match the search criteria $ git log[Tag] HEAD--grep feature# shows the version history of a file, including file name rename $ git log--follow[File]$ git whatchanged[File]# Displays each time diff$ git log associated with the specified file-P[File]# Show last 5 commit $ git log-5--pretty--oneline# Show all submitted users, sort by commits $ git shortlog-sn# shows what the specified file is and at what time has the git blame been modified.[File]# Show staging area and Workspace differences $ git diff# Show staging area and previous commit differences $ git diff--cached[File]# shows the difference between the workspace and the current branch of the latest commit $ git diff HEAD# show the difference between two commits $ git diff[First-branch]...[Second-branch]# shows how many lines of code you wrote today $ git diff --shortstat  "@{0 day ago}" # show metadata and content changes for a commit $ git show [commit]# shows a file that has changed during a commit $ git show --name-only [commit]# shows the contents of a file when a commit, $ git show [commit]:[filename]# Show recent commits of the current branch $ git reflog           

Eight, remote synchronization

# Download all changes to the remote repository $ git fetch[Remote]# Show all remote warehouses $ git remote-V# Show information for a remote repository $ git remote show[Remote]# Add a new remote repository and name $ git remote add[ShortName [url]# retrieve changes to the remote repository and merge with local branch $ git pull [remote] [branch # upload locally designated branch to remote warehouse $ git push [remote] [branch # forcibly pushes the current branch to the remote repository, even if there is a conflict $ git push [remote" --force# push all branches to remote repository $ git push [remote --all   

Ix. Revocation

# Restore staging area of the specified file to the workspace $ git checkout[File]# Restore the specified file of a commit to staging area and workspace $ git checkout[Commit][File]# Recover all files from staging area to workspace $ git checkout.# resets the specified file for staging area, consistent with the last commit, but the workspace is unchanged $ git reset[File]# Reset Staging area with workspace, consistent with last commit $ git reset--hard# resets the current branch pointer to the specified commit, while resetting staging area, but the workspace is unchanged $ git reset Span class= "token punctuation" >[commit]# Resets the head of the current branch to the specified commit, while resetting the staging area and workspace, consistent with the specified commit $ git reset --hard [commit]# resets the current head to the specified commit, But keep staging area and workspace unchanged $ git reset --keep [commit]# create a new commit to revoke the specified Commit# All changes to the latter will be offset by the former and applied to the current branch $ git revert [commit]< Span class= "token comment" ># temporarily remove uncommitted changes and later move into git stash$ git stash pop       

X. Other

# 生成一个可供发布的压缩包$ git archive

Common operations in Git work

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.