Git's odd skill

Source: Internet
Author: User
Tags git commands

Git is a "Distributed version management Tool", a simple understanding of version management tools: Everyone in the writing of the use of "retracement" this function, but the withdrawal can only be a few steps back, if you want to get back my three days before the changes, the light with "retracement" is not to find back. and the version management tool can record each change, just submit to the repository, you can find the status of any previous time (text state).

The following is a list of commonly used git commands and a few tips, you can "find in the page" way to quickly query: Ctrl/Command+f .

Read-out required

If you haven't used git before, learn how to get started with teacher Liao's free git tutorial

    1. Be sure to test the effect of the command before it is used in the work environment to prevent irreparable consequences! don't come looking for me with a machete.
    2. All the commands are git version 2.7.4 (Apple Git-66) under test pass
    3. Unified Concept:
      • Workspaces: Changes (additions and deletions to files and content)
      • Staging area: Input command: git add 改动的文件名 , this change is put to ' staging area '
      • Local Warehouse: Enter command: git commit 此次修改的描述 This change is put in the ' local repository ', each commit, I call it a ' version '
      • Remote repository: Enter command: git push 远程仓库 This change was put to ' remote repository ' (GitHub, etc.)
    4. If you like this project, welcome star, Fork, submit PR, feedback questions??
Directory
Show Help information
git help -g
Back to the state of the remote repository

Discards all versions of the local repository (commit), returning to the state of the remote repository.

git fetch --all && git reset --hard origin/master
Reset the first commit

That is, put all the changes back into the workspace and clear all commitsso that you can resubmit the first commit.

git update-ref -d HEAD
Shows the difference between the workspace and the latest version

The different (different) of the output workspace and the most recent version (commit) in the local.

git diff
Show the difference between the staging area and the latest version

The different (different) of the output staging area and the local latest version (commit).

git diff --cached
Show the different staging area, workspaces, and recent versions

Different (different) of the output workspace , staging Area , and local latest version (commit).

git diff HEAD
Quickly switch branches
git checkout -
Delete a branch that has been merged into master
git branch --merged master | grep -v ‘^\*\| master‘ | xargs -n 1 git branch -d
Show all remote repositories associated with a branch
git branch -vv
Associate a remote Branch

Once associated, the git branch -vv associated remote branch name can be displayed and pushed to the remote repository directly: git push No remote repository is required.

git branch -u origin/mybranch
Delete local Branch
git branch -d <local_branchname>
Delete Remote Branch
git push origin --delete <remote_branchname>

Or

git push origin :<remote_branchname>
Delete local tag (tag)
git tag -d <tag-name>
Delete a remote tag (tag)
git push origin :refs/tags/<tag-name>
Discard modifications to the workspace
git checkout <file_name>

Discard all Modifications:

git checkout .
Go back to the state of a commit and add a commit again
git revert <commit-id>
Return to the state of a commit and delete the subsequent commit

Difference from revert: The reset command erases all commits after a commit ID

git reset <commit-id>
Modify the description of the previous commit
git commit --amend
View commit History
git log
Show locally executed git commands

Just like the Shell's history.

git reflog
Modify author name
git commit --amend --author=‘Author Name <[email protected]>‘
Modify the URL of a remote repository
git remote set-url origin <URL>
List all remote warehouses
git remote
List local and remote branches

-A parameter equals: all

git branch -a
List Remote Branches

The-r parameter is equivalent to: remote

git branch -r
See what's changed in two weeks
git whatchanged --since=‘2 weeks ago‘
Put a commit of a branch on the B branch

This process requires a cherry-pick command, reference

git checkout <branch-name> && git cherry-pick <commit-id>
To alias a git command

Simplify commands

git config --global alias.> <command>比如:git status 改成 git st,这样可以简化命令git config --global alias.st status
Stores the current modification without committing a commit

Refer to Liaoche's git tutorial

git stash
Save the current state, including untracked files

untracked File: New file

git stash -u
Show All stashes
git stash list
Go back to the state of a stash
git stash apply <[email protected]{n}>
Go back to the last stash state and remove this stash
git stash pop
Delete all the stash
git stash clear
To take out a file modification from stash
git checkout <[email protected]{n}> -- <file_path>
Show all tracked files
git ls-files -t
Show All untracked files
git ls-files --others
Show all ignored files
git ls-files --others -i --exclude-standard
Force deletion of untracked files

Can be used to delete a newly created file. If you do not specify a file name, all working untracked files are emptied. cleancommand, Note two points :

    1. After clean, deleted files cannot be retrieved.
    2. Does not affect tracked file changes, only untracked files are deleted
git clean <file_name> -f
Force removal of untracked directories

Can be used to delete the new directory, note : This command can also be used to delete untracked files. See the previous section for details

git clean <directory_name> -df
Rename Branch
git branch -m <new-branch-name>
Demonstrate a simplified commit history
git log --pretty=oneline --graph --decorate --all
To export a branch to a file
git bundle create <file> <branch-name>
To import a branch from a package

Create a new branch where git bundle create the contents of the command are exported

git clone repo.bundle <repo-dir> -b <branch-name>
Automatic stash before executing rebase
git rebase --autostash
From the remote repository, under ID, pull down a state to a local branch
git fetch origin pull/<id>/head:<branch-name>
Show the nearest tag of the current branch
git describe --tags --abbrev=0
Detailed presentation of changes in a row
git diff --word-diff
Clear .gitignoreFiles recorded in the file
git clean -X -f
Show all alias and Configs.
git config --list
Show ignored Files
git status --ignored
Commit history shows Branch1, but BRANCH2 no commit
git log Branch1 ^Branch2
Display GPG signature in commit log
git log --show-signature
Delete Global Settings
git config --global --unset <entry-name>
New and switch to the new branch, and this branch has no commit

Equivalent to saving changes, but rewriting commit history

git checkout --orphan <branch_name>
Show the contents of a file in any branch
git show <branch_name>:<file_name>
Clone down the specified single branch
git clone -b <branch-name> --single-branch https://github.com/user/repo.git
Create and switch to this branch
git checkout -b <branch-name>
Features to turn off ignore files
git config core.fileMode false
Show the commit of all local branches

The newest one is on top.

git for-each-ref --sort=-committerdate --format=‘%(refname:short)‘ refs/heads/
Find related content in commit log

Find by grep, Given-text: The field you want to find

git log --all --grep=‘<given-text>‘
Place the specified file of the staging area in the workspace
git reset <file-name>
Force push
git push -f <remote-name> <branch-name>
Add Remote repositories
git remote add origin <remote-url>

Git's odd skill

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.