Getting Git commands and some frequently asked questions


Git is a distributed version control system open source, can be effective, high-speed processing from very small to very large project version management. Now the most common are github, clouds and other code, see the platform for their own use, but the basic command Git's still the same. Here the author summarizes some of the usual Git commands for your reference, also please indicate if there are questions, we participate in many Liao Xuefeng's official website , more explanation about command also see the site, here I simply summed up some commands.


Upload basic steps

1. establish a remote repository on github

2 command line to enter the local folder, execute the command:

$ git init

3. Add the upload files to the repository, execute the command:

$ git add.

4. submission instructions, execute the command:

$ git commit -m "文件说明"

5. The change in the local repository connected to a remote repository main branch:

$ git pull origin master

6. association github repository, execute the command:

$ git remote add origin [url]

7. upload native code, execute the command:

$ git push -u origin master

Download Command

$ git clone  [url]

Other commands

Configuration

  • Displays the current git configuration:
$ git config --list
  • Edit the configuration file git
$ git config -e [--global]
  • Setting up user information at the time of submission of the code
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"

Add / delete files

  • Adds the specified files to the staging area
$ git add [file1] [file2]
  • Adds the specified directory to the staging area, including subdirectories
$ git add [dir]
  • Add all files in the current directory to the staging area
$ git add .
  • For many variations of the same file can be achieved graded submitted
$ git add -p
  • Delete a workspace file and delete into the staging area
$ git rm [file1] [file2]
  • Stop following the specified file, but the file will be retained in the workspace
$ git rm --cached [file]
  • Rename files, and will be renamed into the staging area
$ git mv [file-orignial] [file-renamed]

Code Submit

  • Submitted to the staging area warehouse district
$ git commit -m [message]
  • Submit files to the staging area designated storage areas
$ git commit [file1] [file2] ... -m [message]
  • Changes since the last commit submit work area, directly to the warehouse district
$ git commit -a
  • Display all information submitted diff
$ git commit -v
  • Use a new commit, commit the alternative
  • If the code does not have any new changes, then commit to rewrite the submission of information on
$ git commit --amend -m [message]
  • Redo the last commit, and includes new changes specified file
$ git commit --amend [file1] [file2] ...

Branch

  • List all local branches
$ git branch
  • List all remote branch
$ git branch -r
  • List all local branches and remote branch
$ git branch -a
  • Create a new branch, but still remain in the current branch
$ git branch [branch-name]
  • Creating a new branch, and the branch switch to
$ git checkout -b [branch]
  • Create a new branch to the specified commit
$ git branch [branch] [commit]
  • Create a new branch, a tracking relationship with remote branches specified
$ git branch --track [branch] [remote-branch]
  • Switch to the designated branch and update the work area
$ git checkout [branch-name]
  • Switch to a branch
$ git checkout -
  • Create a tracking relationship between the existing branch and remote branches specified
$ git branch --set-upstream [branch] [remote-branch]
  • Pushed to the remote branch
$ git push origin master:master
  • Merging a branch to the current branch
$ git merge [branch]
  • Select a commit, merge into the current branch
$ git cherry-pick [commit]
  • Deleted branches
$ git branch -d [branch-name]
  • Delete remote branch
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

label

  • List all ta
$ git tag
  • Create a new tag commit in the current
$ git tag [tag]
  • Create a new tag in the specified commit
$ git tag [tag] [commit]
  • Delete the local tag
$ git tag -d [tag]
  • Delete remote tag
$ git push origin :refs/tags/[tagName]
  • View tag information
$ git show [tag]
  • Submit the specified tag
$ git push [remote] [tag]
  • Submit all tag
$ git push [remote] --tags
  • Create a new branch, pointing to a tag
$ git checkout -b [branch] [tag]

View information

  • There are documents show change
$ git status
  • Displays the current version of the history of this branch
$ git log
  • Show commit history, and every commit changed files
$ git log --stat
  • Search your history based on keywords
$ git log -S [keyword]
  • After all the changes show a commit, commit each occupied row
$ git log [tag] HEAD --pretty=format:%s
  • After all the changes show a commit, which "commit" must match the search criteria
$ git log [tag] HEAD --grep feature
  • Displays the version history of a file, including file renamed
$ git log --follow [file]
$ git whatchanged [file]
  • Displays the files associated with each diff
$ git log -p [file]
  • Display last five submitted
$ git log -5 --pretty --oneline
  • All user submitted, sorted by number of commits
$ git shortlog -sn
  • Displays the specified file is what people at what time modified
$ git blame [file]
  • Show staging area code differences and work area
$ git diff
  • Show Differences on a staging area and commit the
$ git diff --cached [file]
  • Differences between the workspace and display the latest commit the current branch
$ git diff HEAD
  • The difference between the two commits display
$ git diff [first-branch]...[second-branch]
  • Today show how many lines of code you write
$ git diff --shortstat "@{0 day ago}"
  • Display a commit metadata and content changes
$ git show [commit]
  • Shows that a change in the submission of documents
$ git show --name-only [commit]
  • When a submission display the contents of a file
$ git show [commit]:[filename]
  • Displays the current branch a few times recently submitted
$ git reflog
  • Pulling the code from the master update the current local branch: branch generally master
$ git rebase [branch]

Remote synchronization

  • Update Remote Storage
$ git remote update 
  • All changes download remote repository
$ git fetch [remote]
  • Show all remote repository
$ git remote -v
  • Display information about a remote repository
$ git remote show [remote]
  • Add a new remote repository, and named
$ git remote add [shortname] [url]
  • Changes retrieve remote repository, and merge with the local branch
$ git pull [remote] [branch]
  • Upload a local branch to a specified remote repository
$ git push [remote] [branch]
  • Forcibly push the current branch to a remote warehouse, even if there is a conflict
$ git push [remote] --force
  • Push all branches to a remote repository
$ git push [remote] --all

Undo

  • Restore files to the staging area designated work area
$ git checkout [file]
  • Restore a commit specified files to the staging area and a work area
$ git checkout [commit] [file]
  • All file recovery staging area to the work area
$ git checkout .
  • Reset the temporary area specified file, with the last commit consistent, but the same workspace
$ git reset [file]
  • Reset the staging area to the work area, and the last commit consistent
$ git reset --hard
  • Reset the current branch pointer for a given commit, at the same time to reset the staging area, but the same work area
$ git reset [commit]
  • Reset the current branch HEAD specified commit, at the same time to reset the staging area and working area, consistent with the assigned commit
$ git reset --hard [commit]
  • Reset current HEAD to the specified commit, but remain unchanged staging area and working area
$ git reset --keep [commit]
  • Create a commit, commit to specify revocation
  • All changes in the latter will be offset by the former, and applied to the current branch
$ git revert [commit]
  • Temporarily change uncommitted removed, and then later moved
$ git stash
$ git stash pop

other

  • Generate a compressed package release available
$ git archive

Errors and solutions

error: failed to push some refs to ‘[email protected]:plafeng/git_command.git’

hint: the Updates were Rejected Because at The Remote the contains Work that you do
hint:. not have have locally This IS usually Caused by Another Repository Pushing
hint:. to at The Same, ref by You May want to First integrate at The Remote Changes
hint: (EG, 'git ... pull ') before Pushing Again.
hint: the See'. Note About FAST-Forwards' in 'Git Push --help' for Details
below: Here Insert Picture Description
error cause: README.md github file is not in the native code directory
solution method: the code can be incorporated by the following command

git pull --rebase origin master

After executing the above code you can see native code library more README.md file, then re-execute the statement git push -u origin masterto complete the code uploaded to github

error: Your local changes to the following files would be overwritten by merge: Please commit your changes or stash them before you merge (error: Merge will cover the following changes to the local file:

Please submit change or store them prior to the merger. )
Here Insert Picture Description
Solution

  1. Server code into native code
$ git stash	//暂存当前正在进行的工作
$ git pull origin master		//拉取服务器的代码
$ git stash pop		//合并暂存的代码
  1. Server code coverage native code
$ git reset --hard 		//回到上一个版本
$ git pull origin master

Return to the previous local version of warehouses

Today I made a very serious mistake, a local file An error occurred during the upload, the local co-workers pulled the files, local files are gone. If you encounter this problem, do not panic, I tell you
first execute the following command to view the changes in circumstances of their own local repository

$ git reflog

Then execute the following command, chose to return to the state

$ git reset --hard ac70ec1

Wherein "ac70ec1" step number

This blog is for reference only, hope for your help

Published 65 original articles · won praise 141 · views 190 000 +

Guess you like

Origin blog.csdn.net/Silent_F/article/details/83927011