Git study notes

Git study notes

Git study notes

Learning path oschina.gitee.io/learn-git-b...

1. Git commonly used commands

$ git clone/path/to/repository # Execute the following command to create a clone of the local repository $ git clone username@host:/path/to/repository # Repository on the remote server $ git pull # Update your local repository To the latest changes $ git branch -vv # View the remote branch corresponding to the local branch $ git remote -v # View the remote URL corresponding to the local copy code

2. Add and submit

$ git add <filename> # Add to the cache area $ git commit -m "Code submission information" Copy code

3. Push changes

$ git push origin master # Submit to remote warehouse copy the code

4. Branch

$ git branch branch name #new branch $ git branch -d branch name # delete branch $ git checkout branch name # switch to a branch $ git rebase branch name # similar to copying to a new branch $ git checkout C4 #point HEAD to C4 $ git branch -f name of a branch node name # forcibly move a node to copy the code

5. Update and merge

$ Git pull # update your local repository to the latest changes $ git Merge branch name # will be merged into the master branch branches copy the code

6. Label

$ Git log # ID acquisition submit copy the code

7. Replace local changes

$ git checkout - <filename> # Replace local changes # Discard all your local changes and commits, you can get the latest version on the server and point your local master branch to it: $ git fetch origin $ git reset - hard origin/master copy the code

8. Git commonly used commands

$ gitk # Built-in graphical git: $ git config color.ui true # Colored git output: $ git config format.pretty oneline # When displaying history, only one line of comment information is displayed: $ git add -i # Interactively Add files to the cache: copy the code

9. Command summary

1. Warehouse

# In the current directory to create a new Git repository $ git the init # Create a new directory, which is initialized to the Git repository $ the init [Project-name] git # downloading a project and its entire code history $ git clone [url] Copy the code

2. Configuration

# Display the current configuration Git $ git config --list # edit configuration files Git $ git config -e [--global] # Set the user information when submitting the code $ git config [--global] user.name "[name] " $ git config [--global] user.email " [Email address] " copy the code

3. Add/delete files

# Adds the specified files to the staging area $ git the Add [file1] [file2] ... # Adds the specified directory to the staging area, including subdirectories $ git the Add [dir] # Add all files in the current directory to the staging area $ the Add git. # before adding each change, will require confirmation # for many changes in the same file, can achieve sub commits $ git the Add -p # delete a workspace file and delete the staging area into $ RM git [file1] [file2] ... # stop tracking the specified file, but the file remains in the work area $ git RM --cached [file] # rename files, and will be renamed into the staging area $ git mv [file-original] [file-renamed] Copy code

4. Code submission

# Submitted to the staging area warehouse district $ git the commit -m [the Message] # submit the file to the designated staging area warehouse district $ git the commit [file1] [file2] ... -m [the Message] # submitted from the work area after the change of times commit, directly to the warehouse district $ git the commit -a # show all information submitted diff $ git the commit -v # using a new commit, commit the alternative # If the code does not have any new changes, it is used to rewrite the last time the information submitted commit $ git commit -m --amend [the message] # redo the last commit, and includes new changes specified file $ git commit --amend [file1] [file2] ... copy the code

5. Branch

# List all local branches $ git Branch # list all remote branches $ 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] # create a branch and switch to the branch $ git Checkout -b [branch] # create a new branch, point to specify the commit $ git branch [branch] [the commit] # Create a branch, established to track the specified remote branch relationship $ Git branch --track [branch] [Remote-branch] # switched to a specified branch, and updates the work area $ Git Checkout [branch-name] # switch to a branch $ Git Checkout - # establish trace relationships between existing branches and remote branches specified $ git branch --set-upstream [branch] [remote-branch] # consolidates branch Go to the current branch $ git merge [branch] # selected a commit, merge into the current branch $ git Cherry-Pick [the commit] # deleted branches $ git branch -d [branch-name] # delete remote branch $ git the Push Origin --delete [branch-name] $ git branch -dr [remote/branch] Copy code

6. Label

# List all tag $ git tag # to create a new tag commit in the current $ git tag [tag] # Create a tag in the specified the commit $ git tag [tag] [the commit] # Delete the local tag $ git tag -d [tag] # delete remote tag $ git the Push Origin: refs/Tags/[tagName] # View information tag $ git Show [tag] # submit the specified tag $ git the Push [remote] [tag] # submit all tag $ git the Push [remote] - Tags # Create a new branch, points to a Tag $ git Checkout -b [branch] [Tag] copy the code

7. View information

#Show changed files $ git Status # show version of the history of the current branch $ git log # show commit history, and changed files each commit $ git log - STAT # search your history based on keywords $ git log - S [keyword] # displays all changes after a commit, commit each occupy one line $ git log [Tag] = the HEAD --pretty format:% S # show all changes after a commit, which "commit" must match your search criteria $ git log [Tag] the HEAD --grep the Feature # show version history of a file, including file rename $ git log--follow [File] $$ Git whatchanged [File] # display every time the specified file diff-related $ git log -p [File] # show last 5 commits $ git log -5 --pretty --oneline # Display all user submitted, and press Submit Sort by number of times $ git shortlog -sn # Show who the specified file is and when it was modified $ git blame [file] # Show the difference between the temporary storage area and the work area $ git diff # Show the difference between the temporary storage area and the previous commit $ diff --cached git [File] # display the work area and the difference between the current branch latest commit $ git diff the HEAD # shows the difference between the two commits git diff [First-branch] ... [-SECOND, branch] # show today how many lines of code you write $ git diff --shortstat "@ {0} Day ago Member " # display a commit metadata and content changes $ git show [commit] # display a commit changes files $ git show --name-only [the commit] # display when a commit, the contents of a file $ git show [the commit]: [filename] # displays the current times recently filed branch $ git reflog copy the code

8. Remote synchronization

# Download remote repository of all changes $ git FETCH [Remote] # displays all remote repository $ git Remote -v # display information to a remote repository $ git Remote Show [Remote] # to add a new remote repository, and named $ git the Add remote [the ShortName] [url] # fetch change remote repository, and merge with the local branch $ git pull [remote] [branch] # Upload a local branch to a specified remote repository $ git the push [remote] [branch] # forcibly push current branch to a remote warehouse, even if there is a conflict $ git the push [remote] --force # push all the branches to the remote repository $ git the push [remote] --all copy the code

9. Withdraw

# 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 workspace $ git Checkout. # reset the staging area of the specified file, with the last commit consistent, but the work area unchanged $ git the rESET [file] # reset the staging area to the work area, and the last commit consistent $ git --hard the rESET # reset the current branch pointer for the specified commit, at the same time to reset the staging area, but the work area unchanged $ git the rESET [the commit] # reset the current branch HEAD specified commit, while temporarily reset deposit and working areas, consistent with the specified commit $ git the rESET --hard [commit] # reset the current HEAD to the specified commit, but keep the staging area and working area unchanged $ git the rESET --keep [commit] # Create a new commit to cancel the specified commit # All changes which the former will be canceled, and applied to the current branch $ Git Revert [the commit] Temporarily remove uncommitted changes and move in later $ git stash $ git stash POP copy the code

10. Actually step on the pit

# 1. Initialize the warehouse $ git init # 2. Check the remote warehouse to confirm that there is no, delete it $ git remote -v $ git remote rm origin # 3. Add to the staging area $ git add. # 4. Submit $ git commit -m "Description" # 5. Add remote warehouse $ git remote add alias link # 6. View remote warehouse $ git remote -v # 7. Pull warehouse $ git pull alias master # 8. Confirm that the remote and local warehouses are consistent and do not modify anything Save and exit $ git pull alias master --allow-unrelated-histories # 9. Push to remote warehouse $ git push alias master #10. Refresh the warehouse to see if it is successful! Copy code