Use of basic git commands

      One of the more popular version control is git, which is mainly used for version control of open source repositories, of course, you can also use it in your own projects. There are many git-based code hosting platforms on the Internet, such as github, gitee (code cloud), and gitlib. If the company has an intranet, we can also build a git server ourselves to manage and package the code.

      Today we mainly talk about the use of git commands through gitee. Commonly used git commands mainly include the following:

1. New code base

    # Create a new Git repository in the current directory

    $ git init

    # Create a new directory and initialize it as a Git repository

    $ git init [project-name]

    # Download a project and its entire code history

    $ git clone [url]

Two, placement

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

    # Display the current Git configuration

    $ git config --list

    # Edit the Git configuration file

    $ 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]"

3. Add / delete files

    # Add the specified file to the staging area

    $ git add [file1] [file2] ...

    # Add 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 .

    # Before adding each change, it will ask for confirmation

    # For multiple changes to the same file, you can implement multiple submissions

    $ git add -p

    # Delete the workspace file and put this deletion into the staging area

    $ git rm [file1] [file2] ...

    # Stop tracking the specified file, but the file will remain in the workspace

    $ git rm --cached [file]

    # Rename the file and put this renamed into the staging area

    $ git mv [file-original] [file-renamed]

4. Code submission

    # Submit the staging area to the warehouse area

    $ git commit -m [message]

    # Submit the specified file in the staging area to the warehouse area

    $ git commit [file1] [file2] ... -m [message]

    # Submit the changes in the workspace since the last commit, directly to the warehouse area

    $ git commit -a

    # Display all diff information when submitting

    $ git commit -v

    # Use a new commit, replacing the previous commit

    # If the code does not have any new changes, it is used to rewrite the commit information of the last commit

    $ git commit --amend -m [message]

    # Redo the last commit and include the new changes in the specified file

    $ git commit --amend [file1] [file2] ...

5. Branches

    # list all local branches

    $ git branch

    # list all remote branches

    $ git branch -r

    # List all local and remote branches

    $ git branch -a

    # Create a new branch, but still stay in the current branch

    $ git branch [branch-name]

    # Create a new branch and switch to it

    $ git checkout -b [branch]

    # Create a new branch pointing to the specified commit

    $ git branch [branch] [commit]

    # Create a new branch and 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 the 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 the specified branch to the current branch

    $ git merge [branch]

    # Select a commit to merge into the current branch

    $ git cherry-pick [commit]

    # delete branch

    $ git branch -d [branch-name]

    # delete remote branch

    $ git push origin --delete [branch-name]

    $ git branch -dr [remote/branch]

6. Label

    # list all tags

    $ git tag

    # Create a new tag in the current commit

    $ git tag [tag]

    # Create a new tag at 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 information

    $ git show [tag]

    # Submit the specified tag

    $ git push [remote] [tag]

    # Submit all tags

    $ git push [remote] --tags

    # Create a new branch, pointing to a tag

    $ git checkout -b [branch] [tag]

7. View information

    # show files with changes

    $ git status

    # Display the version history of the current branch

    $ git log

    # Display the commit history and the files that have changed with each commit

    $ git log --stat

    # Search commit history, by keyword

    $ git log -S [keyword]

    # Display all changes after a commit, each commit occupies one line

    $ git log [tag] HEAD --pretty=format:%s

    # Display all changes after a commit, its "commit description" must match the search criteria

    $ git log [tag] HEAD --grep feature

    # Display the version history of a file, including file name changes

    $ git log --follow [file]

    $ git whatchanged [file]

    # Display each diff related to the specified file

    $ git log -p [file]

    # Display the last 5 commits

    $ git log -5 --pretty --oneline

    # Display all submitted users, sorted by the number of submissions

    $ git shortlog -sn

    # Show who modified the specified file and when

    $ git blame [file]

    # Display the code difference between the staging area and the working area

    $ git diff

    # Display the difference between the staging area and the last commit

    $ git diff --cached [file]

    # show the difference between the workspace and the latest commit of the current branch

    $ git diff HEAD

    # show the difference between two commits

    $ git diff [first-branch]...[second-branch]

    # show how many lines of code you wrote today

    $ git diff --shortstat "@{0 day ago}"

    # Display metadata and content changes for a commit

    $ git show [commit]

    # Display files that have changed in a commit

    $ git show --name-only [commit]

    # Display the content of a file when a submission is made

    $ git show [commit]:[filename]

    # Display the last few commits of the current branch

    $ git reflog

    # Pull code from the local master to update the current branch: branch is generally master

    git rebase [ branch ]

Eight, remote synchronization

    # Download all changes from the remote repository

    $ git fetch [remote]

    # show all remote repositories

    $ git remote -v

    # Display information about a remote repository

    $ git remote show [remote]

    # Add a new remote repository and name it

    $ git remote add [shortname] [url]

    # Fetch changes from remote repository and merge with local branch

    $ git pull [remote] [branch]

    # Upload the local specified branch to the remote warehouse

    $ git push [remote] [branch]

    # Force push 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

9. Cancellation

    # Restore the specified file in the staging area to the workspace

    $ git checkout [file]

    # Restore the specified file of a commit to the staging area and workspace

    $ git checkout [commit] [file]

    # Restore all files in the staging area to the workspace

    $ git checkout .

    # Reset the specified file in the staging area, consistent with the last commit, but the workspace remains unchanged

    $ git reset [file]

    # Reset the staging area and workspace to be consistent with the last commit

    $ git reset --hard

    # Reset the pointer of the current branch to the specified commit, and reset the staging area, but the workspace remains unchanged

    $ git reset [commit]

    # Reset the HEAD of the current branch to the specified commit, and reset the staging area and work area at the same time, consistent with the specified commit

    $ git reset --hard [commit]

    # Reset the current HEAD to the specified commit, but keep the staging area and workspace unchanged

    $ git reset --keep [commit]

    # Create a new commit to revoke the specified commit

    # All changes in the latter will be offset by the former and applied to the current branch    

    $ git revert [commit]

    # Temporarily remove uncommitted changes and move in later

    $ git stash

    $ git stash pop

10. Others

    # Generate a release-ready tarball

    $ git archive

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324406015&siteId=291194637