Common commands and usage methods of version control tool Git

overview

In actual development work, it is inevitable to use version control tools for collaborative development, and Git is one of the most common version control tools. Although many companies now have their own version control tools, most of them are based on Git for some customized operations, and Git commands are still applicable. The initialization of the Git warehouse can refer to this article: Android Studio uses Git and connects to the remote library (GitHub) . This article mainly records some Git commands commonly used in the development process.

basic command

  1. Create branch
    git branch <branch-name>: Create branch
    git checkout <branch-name>: Switch branch
    git checkout -b <branch-name>: Create and switch branch
    For example, create a new branch and switch branch directly in the Terminal window of Android Studio:
    new branch
  2. View status
    git status: You can view the status of the current warehouse
    git status
  3. View the latest submission information
    git show: You can view the detailed information of the latest submission, including the submitter, submission information, submission date, file differences, etc.
    git show
  4. View the submission corresponding to the code
    In AndroidStudio, right-click and select Annotate with Git Blameto view the submission corresponding to each line of code:
    git blame
    AS view submission information
  5. Obtain the hash value corresponding to a certain submission
    Each submission will have its own unique hash value, which is also the basis for subsequent rollback codes. The way to view the hash value is as follows:
    In the window mentioned in point 4, right-click to view the hash value corresponding to the submission:
    view hash
  6. Delete branch
    git branch -D <branch-name>: delete the corresponding branch.

submit code

Git can be divided into workspace, temporary storage area, and version library. Simply put, the code that has not been modified by Git is in the workspace, the code modified after the command is executed is addin the temporary storage area, and committhe code after the command is executed is in the temporary storage area. in the repository. Common commands in the process of submitting code are as follows:

Submit the code to the staging area

git add .: Submit all modified but unsubmitted codes and files to the temporary storage area;
git add *: Submit all modified but unsubmitted codes and files to the temporary storage area; the difference
git add .with git add *: git add .will untrackadd all local files to the temporary storage area area, and will be .gitignorefiltered according to; git add *will prompt the content that has been ignored, but will not directly join.
git add <file-name>: Add the specified file to the temporary storage area. This command is generally used in daily development.
git add

Submit the code to the repository

git commit -m <commit message>: Submit the code modification in the temporary storage area to the repository, where the submission information is commit messagethe content.
git commit: Many companies will have their own commit messagetemplates. In this case, you can create a new templatefile and put it in the specified directory. git commitAfter execution, the template will be automatically loaded. Just submit after editing. It should be noted that if commit messagenewlines are involved, it is best to use git bash instead of the Terminal window that comes with Android Studio.
git commit -m

I want to add and modify the same submission

git commit --amend: This command is suitable for appending changes to the latest commit. It should be noted that the modification on the code and commit messagethe modification on the above can be appended with this command.

fallback code

The code has been modified and I want to roll back the modification of the workspace

git restore <file-name>: This command is applicable to files that have been modified but not submitted to the temporary storage area. Use this command to roll back the modification of the corresponding file to the latest commit.
git restore

Executed the add operation and want to return to the workspace

git restore --staged <file-name>: This command can roll back the code and files submitted to the temporary storage area to the work area.
git restore --staged

Executed the commit operation and want to undo the modification

git reset --hard HEAD^: This command is used to roll back the latest commit, where one ^means a commit, that is git reset --hard HEAD^^, roll back the last two commits, git reset --hard HEAD^^^and roll back the latest three commits.
git reset --hard <revision number>: Roll back the code on the branch as of this commit. Typically used for operations that require batch rollback.
git revert <revision number>: revertcommand can also be used to roll back code into the corresponding commit. It should be noted that revertwhen submitting a new version, revertthe content of the required version is reversely modified, and the version will be incremented without affecting the previously submitted content.
It should be noted that the Terminal window that comes with Android Studio cannot recognize ^symbols, and commands involving this symbol are best executed in git bash.

Execute the commit operation and want to roll back to the temporary storage area

git reset --soft HEAD^: Another scenario is that we have already submitted the code and want to modify the fallback code before resubmitting, but we don’t want to modify it from scratch. In addition to using git commit --amend, you can also use git reset --soft HEAD^the command to roll back the latest submission from the repository to the staging area.
git reset --soft

pick code

git cherry-pick <revision number>: First switch to the target branch, and then cherry-pick a specific commit on a certain branch to the target branch. Git will also prompt if there is a code conflict, and you need to manually resolve the conflict before performing cherry-pick.

postscript

Common commands will continue to be updated in the future, and everyone is welcome to expand the common commands of Git together.

Guess you like

Origin blog.csdn.net/weixin_46269688/article/details/129967537