Common git commands

GIT

what is git

  • is a source code management tool
  • In a project, everything written by a developer is source code
  • Does the source code need to be managed?
  • Make the source code traceable, mainly record what has changed each time, and who led the change
  • Human maintenance is more troublesome
  • GIT is a tool written by the father of Linux to maintain and manage Linux source code.
  • svn vss tfs hs used a lot before GIT

  • https://guides.github.com/

install git

  • git command line tool
  • A client software based on the git command line (provides an interface to manage source code)

GIT command operations

  • Initialize a local warehouse
    ...shell
    cd current project directory
    git init //Initialize a local warehouse
    ...

It is to add a .git folder to the local folder to record all project change information

  • View the change status of the local repository

git status
is used to view the status of the local repository.
For the first time, it shows the files that are not tracked in the root directory.

git status -s //-s parameter, output a brief change log

  • Add local staging (hosted) files

git add
can add an untracked file to the tracking list
git add . Add all files. Files
of this nature similar to node_modules do not need to be tracked

  • Add local ignore list

Add a .gitignore file to the root of the code folder.
This file is used to describe which files are ignored

  • Commit hosted folder changes to local repository

git commit
submits local changes to the local warehouse folder for archiving,
generally after there is an overall change in a small unit

  • Contrast differences

git diff
can be used to compare the current state with the state in the repository

  • commit log

git log
can view the commit log

  • revert to the specified version

Common git commands

  1. New code repository
    ```shell

    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]
```

  1. configure
    the shell

    Display the current Git configuration

    $ git config --list

Edit the Git configuration file

$ git config -e [--global]

Set user information when submitting code

$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"
```

  1. add/delete file
    ```shell

    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, multiple submissions can be implemented

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

  1. code commit
    ```shell

    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

Show 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] ...
```

  1. branch
    ```shell

    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 that branch

$ 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 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 into 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]
```

  1. Label
    ```shell

    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]
```

  1. View information
    ```shell

    Show files with changes

    $ git status

Display the version history of the current branch

$ git log

Display the commit history, and the files changed with each commit

$ git log --stat

Search submission 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, whose "commit description" must match the search criteria

$ git log [tag] HEAD --grep feature

Displays the version history of a file, including file renames

$ git log --follow [file]
$ git whatchanged [file]

Displays every diff related to the specified file

$ git log -p [file]

Show last 5 commits

$ git log -5 --pretty --oneline

Show all users who have submitted, sorted by number of submissions

$ git shortlog -sn

Displays who modified the specified file and when

$ git blame [file]

Show the difference between the staging area and the working area

$ git diff

Show the difference between the staging area and the last commit

$ git diff --cached [file]

Show differences between the workspace and the latest commit of the current branch

$ git diff HEAD

Show differences 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]

Show files that changed from a commit

$ git show --name-only [commit]

Displays the content of a file at a commit

$ git show [commit]:[filename]

Show the last few commits of the current branch

$ git reflog
```

  1. Remote sync
    ```shell

    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 repo 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 are conflicts

$ git push [remote] --force

push all branches to remote repository

$ git push [remote] --all
```

  1. undo the
    ``` shell

    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 work area

$ git checkout [commit] [file]

Restore all files in the staging area to the workspace

$ git checkout .

Reset the specified file in the staging area, which is 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 at the same time, but the work area 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 working area 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
```

  1. other

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

Guess you like

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