preface

Because Jiuxin has been working as an independent developer before, he is not good at collaborative development, and he still uses Git commands independently. The most commonly used three commands are as follows:

  • git clone
  • git commit
  • git push origin master

After entering the new company, I have made up a lot of Git commands. Today we will talk about the common Git commands at work.

Here’s a good site to practice Git commands before reading:

Learn Git BranchingAZ

And some learning sites:

Git Documentation liao Xuefeng’s Git Tutorial

directory

1. Basic reading

1. What is Git?

For this question, we can take a look at the official documentation:

Git is a free and open source distributed version control system designed to handle everything from small to very large Git projects with Speed and Efficiency. Git is a free and open source distributed version control system dedicated to handling any project, small or large, efficiently and quickly.

2. Concepts in Git

Workspaces, staging areas, and version libraries

It’s important to know about workspaces and repositories in Git:

  • Workspace: Simply put, the directory that is visible to our current computer.
  • Repositories: Anyone who has ever used Git knows that there is a hidden directory inside.gitThis is what we call a version library, which is divided into staging areas and branches.git addCommand to submit files from the working directory to the staging area,git commitCommand to commit files from staging to git repository.
The process of Git

File status

If we want to see the status of our workspace and staging files, we can use the git status command, such as my recent Flutter project:

wangjiedeMacBook-Pro:flu_pro wangjie$ git status
warning: unable to access '/Users/wangjie/.config/git/attributes': Permission denied
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
  new file: lib/main.dart  Changes not staged for commit:  (use "git add <file>..." to update what will be committed)  (use "git checkout -- <file>..." to discard changes in working directory)   modified: lib/HelloWorld.dart  Untracked files:  (use "git add <file>..." to include in what will be committed)   .metadata / /... omit Copy the code

Among them:

  • Changes to be committed: Files in the staging area.
  • Changes not staged for commit: Workspace file has not been saved to staging area.
  • Untracked files: refers to files that are created for the first time and have not been updated.

In Android Studio, it’s even more convenient:There are lots of colors:

  • white: Files that have been committed but have not changed.
  • blue: Something has changed since commit
  • Forgive the green: Files in the staging area.
  • yellow: files that do not require version control.

branch

Branching is an essential role in multi-player collaborative development, as there are at least two branches in our development process:

  • Official version branch: a branch of the online code version that needs to run stably.
  • Branch of the development thread: the development branch of the requirements for the next release.

In addition to these branches, there may be branches to fix bugs, etc.

2. Basic commands

1. Temporary storage area

Add to the staging area

Specific command: Add all files

git add .
Copy the code

If you are adding a file

Git add < file path >Copy the code

Command description: Adds files from the workspace to the staging area.

The IDEA or the AS:In IDEA or AS, when you add a new file after Git initialization, you will be prompted:Alternatively, right click on file > select ‘Git’ > select ‘Add’ and ‘Add to. Gitignore’ to Add to staging and configure ignore files respectively:Joining the staging area means that the file is being added to version control. Configuring to ignore files means that they are not added to version control.

Undo workspace changes

Often, you make changes to a file in many places, but you don’t want to delete them one by one.

Undo workspace code changes.

Specific commands

git checkout -- file
Copy the code

Be sure to add –, otherwise it will become a switched branch (more on that below).

The IDEA or the ASAS makes it easy to see what code a file has changed. To do this, select “Version Control” > Select a specific file > Click DiffThe differences are obvious, and you can click the corresponding button to undo the changed codeBy accidentgit diffThe operation to be performed explains…

Undo changes to the staging area

The other option is to use git add and commit the code to the staging area and the previous command will no longer work.

Undo code changes to the staging area.

Specific commands

git reset HEAD file
Copy the code

This command is rarely used in AS or IDEA scenarios

2. Version library related

Commit code

Command description To add a file from the staging area to the repository

Specific commands

Git commit -mCopy the code

Animation model What happens in the sandbox model after you enter Git commit? A new version record is generated:

IDEA or AS, I generally create a Change List for corresponding requirements, and the specific process is AS follows:

Select “Version Control” > select a Change List > right click to pop up the selection box > Select “New Changelist”To commit, select “Commit” and enter “Commit Message”

Cancel the submission

A common scenario is that you commit your code using git commit, but it hasn’t been committed to a remote branch yet, and then you quickly realize that the commit is causing major problems and you want to undo the commit.

This command is used to undo the version library when it is not uploaded to a remote branch.

Specific commands

git reset HEAD~1
Copy the code

HEAD represents the current HEAD node, HEAD~1 represents the previous node of the current node, explained later.

Animation model

The IDEA or the ASIn AS, open version commit history firstLet’s say I want to go back tofirst commitReset Current Branch to Here… “, and a pop-up box pops upThe differences between the four choices:

  • Soft: The current file remains unchanged (but you can base it ongit diffTo find out what changes have happened to the file), the commits in the staging area are also retained.
  • MIxed: The current file does not change, the staging commit record will not be retained.
  • Hard: The current file is rolled back to the selected version. Any current changes are not saved. Use with caution.
  • Keep: You can manually select what to save when your workspace or staging area changes, similar to conflict situations.

To view changes

People aren’t machines, and it’s not uncommon to forget what you’ve changed.

The compare workspace command compares the differences between the current workspace and the previous version

Without entering the file path, the default compares the current workspace to the previous version, and the command line tells you what class changes are made. I’m not going to go through that.

IDEA or AS has been introduced, not to repeat ~

3. Branch

See the branch

Specific commands

git branch
Copy the code

Command description View local branches

Command result

The IDEA or the ASClick on the red part in the lower right corner to display the local and remote branches and switch branches.

Create a branch

Command describes how to create a branch.

Specific commands

git branch <name>
Copy the code

The branch name must be added.

Animation effects

Click “New Branch” to complete the IDEA or AS in the same place AS the Branch view button.

Switch branch

Command to switch branches

Specific commands

git checkout <name>
git checkout -b <name>
Copy the code

Git checkout is also used to undo workspace changes. Git checkout is also used to create and switch branches.

Animation effects In this picture, not only do we switch branches, but after we switch, we commit once, so the two branches are no longer in the same version.

The IDEA or the ASSame as looking at branchesTo view the current Version of each branch, click Version Control > Log.

Merging branches

When the next version of code is iterated and ready to go live, you can merge branches.

Command to merge branches.

Specific commands

git merge <name>
Copy the code

Animation effects As you can see, inC2 ε’Œ C3The merge produces a new versionC4.masterThe branch goes straight to itC4.

Git merge git merge git merge git merge

Specific commands

Git rebaseCopy the code

Animation effects You can see the current branchbugFixVersion to point toC3He was missing, and he wasmasterOne is generated below the branchC3'Although I can’t see itC3'It’s made up of the originalC2 ε’Œ C3Merged, but the new commit record is more linear and concise.

Git merge and Git rebase

  • Merge: Retain the previous submission order.
  • RebaseThe resulting commit history is more linear and looks more concise. Most of the team’s choices.

The IDEA or the ASChoose Git: Branch name > Select a branch > Select Rebase or Merge

Remote command

1. Update the code

When working in a team, pull the latest code before uploading it.

The pull command describes the latest code

Specific commands

git pull <origin> <localBranch name >Copy the code

Git remote add

git remote add

Animation effects As you can see, after the remote code is pulled down, if your code has already been submitted, it will firstmergeCode, then switch the current branch to the latest version.

The IDEA or the AS“VCS” > “Git” > “Remotes”You can manually add it by selecting the Add button in the lower left corner.

The drop-down code is even simpler. Click the button in the upper right corner, and the first drop-down will generate a popoverOn the left side of theUpdate TypeI won’t tell you much about the one on the rightClean working tree before updateThere are two options, the second is not in Git, and IDEA also said that it will be removed later, we do not need to know, chooseUsing StashIs a step in the right direction.

Using Stash corresponds to the Git Stash in Git command. Before merging two branches, we need to commit the changed code, but our current development is not finished, so we don’t want to commit for no reason and dirty our commit record. In this case, we can use Git Stash. It will temporarily store the code in the workspace, then empty it, merge the code in the remote branch, and finally restore the code in the workspace.

To deal with conflictBut in general collaborative development, reasonable distribution of work in the case of occasional conflict, selectionGit for Android StudioWhen a conflict occurs, the following pop-up box will appearUnless you know which code to use, Merge… , and then another popbox will appearIt can be clearly seen from the figure that the frame is divided into three parts in the middle left:

  • Left: native code
  • Chinese: the result of the merger
  • Right: remote code

If you want to select the code on the left, click the “>>” button on the left. Instead of selecting the “X” button, click the “Apply” button in the lower right corner after confirming the code.

2. Upload the code

In addition to downloading the latest code, we also need to upload our own changes.

Command to upload the latest code.

Specific commands

Git push <origin>Copy the code

Animation effects The local branch is on the left and the remote branch is on the right.

The IDEA or the ASNormally when we commit code, we can chooseCommit and PushThat is, when you submit your codePush.If you don’t want to beCommitWhen he was in the hospital.Push, you can also select “VCS” in the menu bar to select separatelyPush.

3. Remote progression

Git push origin master = git push origin master = git push origin master

Specific commands

Git push <origin>Copy the code

The same is true of updated commands

Git push <origin>Copy the code

It might be a little abstract to look at it directly, but let’s just look at the picture.

Animation effects This time, relative references are tried, i.efoo^Point to thefooThe branch points to the previous version of the version, so remote repositorymasterBranches are only updatedfooThe branch points to the previous version of the version.

Advanced command

1. Relative references

It’s not always convenient to use a Hash to revert a version. If you want to revert a version to a previous version, you have to look up the Hash value of the version and then use it to revert.

Previous version

Command to specify the previous version of the current version.

Specific commands

HEAD^
Branch ^Hash value ^Copy the code

HEAD, branches, and Hash values are fine here, as long as the version library is determined.

Git checkout -b bugfix master^ git checkout -b bugfix master^

Animation effects This is a separationHEADThe command willHEADDetach into the presentmasterThe branch points to the previous version of the version.

N versions

This command describes how to roll back to the first N versions of a specified version.

Specific commands

HEAD~2
Branch ~ nHash value ~ nCopy the code

The other commands are similar to the previous one and are not repeated.

Additional updates

You’ve just committed a piece of code and suddenly you find an error. You don’t want to create another commit for that.

This command describes how to update the code of the last COMMIT to avoid generating a commit record again.

Specific commands

git commit --amend
Copy the code

Animation effects As you can see from the animation, the original version libraryC2Gone and replaced byC2', they are actually the same version, corresponding to the same ID.

The IDEA or the AS Select “Amend Commit”. As for “sign-off Commit”, you rarely need it.

conclusion

The above commands can be used for daily development, but for some complex commands, check again when using. If you have some frequently used commands, feel free to add them in the comments section.

It has been a month since I entered the new company, and I finally recovered my rhythm. I can output articles normally in the future.

advertising

I am nine heart, the new Internet code farmers, if you want to advance and learn more dry goods, welcome to pay attention to my public number received my latest article.

Citation:

“πŸ› Git Common Operation summary” “About Android Studio using Git summary” “Liao Xuefeng Git tutorial”