This is the 10th day of my participation in the August More Text Challenge. For details, see:August is more challenging

This article is all about git base operations, focusing on commit rollback and other things to help commit code, and ensuring that the commit code block runs smoothly 😄😄😄

Viewing Basic Information

  • Remote viewing

    Git remote show // Check the host name git remote -v // Check the associated remote connectionCopy the code
  • Account information

    git config user.email
    git config user.name
    Copy the code

Branch operation

  • Creating a Local Branch

    Git checkout -b < branch name > // For example git checkout -b shengjingyinCopy the code
  • Creating a Remote Branch

    Git push < host name > < local branch name > git push < host name > < local branch name > For example, git push Origin dev3:ayin. My local branch is dev3, but MY remote branch is ayinCopy the code
  • Switching a Local branch

    Git checkout < branch name >Copy the code
  • Deleting a Local Branch

    Git branch -d < branch name >Copy the code
  • Deleting a remote branch

    Git push < host name > --delete < branch name >Copy the code
  • Branch state

    Git branch <-a> // check for local branch + remote branchCopy the code

Develop the delivery process normally

Down a schematic diagram from the Internet, I feel quite complete, involving how to communicate between the work area, temporary storage area, local warehouse and remote warehouse (from right to left, I intended to find a left to right emM, but can not find it, so uncomfortable 😫)

1. Communication between work area and temporary storage area

The workspace commits to the staging area

Normal submission, the following three points:

  • Adds one or more changed files to the staging area

    git add [file1] [file2] ...
    Copy the code
  • Adds the specified directory to the staging area, including subdirectories

    git add [dir]
    Copy the code
  • Adds all files in the current directory to the staging area

    git add .
    Copy the code

The staging area returns to the workspace

To return the contents of the staging area to the workspace, use the following methods:

  • Rolls back the specified file in the staging area

    git reset HEAD rainbow.txt start.txt
    Copy the code
  • Rollback all files in the staging area

    git reset HEAD *
    Copy the code
  • Rolls back a class of files in the staging area

    git reset HEAD *.txt
    Copy the code

2. Communication between temporary storage area and local warehouse

The staging area is submitted to the local repository

  • Commit the staging area to the local repository

    git commit -m 'message'
    Copy the code
  • Submit the files specified in the staging area to the warehouse district

    git commit [file1] [file2] -m [message]
    Copy the code

The local repository is back to the workspace

Code that has been committed to the local repository, that is, code that has been committed. If you want to undo previous changes, you need to perform a version rollback, and the rollback file is returned to the workspace instead of the staging area.

  • Roll back to the previous version

    Git reset HEAD^ // Previous version git reset HEAD^ // Previous version git reset HEAD^^^ ^ // Previous version git reset <commit-id> // Back to the specified versionCopy the code
  • Roll back to the specified version

     git logGit reset <commit-id> git reset <commit-id> git reset <commit-idCopy the code

    Tips: You can use the git log or git reflog command to view the commit history of git and get the commit id

3. Communication between local warehouse and remote warehouse

Push to the remote repository

  • The local branch and the remote branch have been connected

    Git push -f = git push -f = git push -fCopy the code
  • The local branch is not connected to the remote branch

    At this time, if you push, the following information will be prompted. The meaning of remote association is not established, and the code for establishing association has been prompted

    Git push --set-upstream origin devCopy the code

Example Roll back the remote commit record

What should we do when we commit to the remote, but feel that the commit information is wrong, or we have committed the local test code to the remote? Here’s how to roll back a remote commit record in two steps:

The method I provided here is not the only method, if there is a better method, please reply in the comments section, thanks ~

  • Example Roll back the local repository to the specified version

    Git reset <commit-id> // return to the specified versionCopy the code
  • Continue to push

    Git push -f // git push -fCopy the code
  • Effect: You can see that the submission record from 1 minute ago is gone

The summary of this chapter

This section mainly introduces the development of the code, how to create a branch, submit a whole process of the code, on the back the way I believe leaders are also there are other ways, I this offer is just the normal development of a normal operation, nothing strange novelty, if you have a better way to welcome comments reply, To prevent others from falling into my trap 😂😂😂

In the next section, I’ll continue with some of git’s more advanced operations: how to resolve merge conflicts, temporarily store code, and so on