Take you to master the GIT commands commonly used in work with examples in 7 minutes


Author: Valeria
Translator: front end Xiaozhi
Source: medium

Dream, dry goods, wechat search[move to the world]Pay attention to this dish washing wisdom who is still washing dishes in the early morning.

This article GitHubhttps://github.com/qq449245884/xiaozhiIt has been included. There are complete test sites, materials and my series of articles for the interview of front-line large factories.

In essence, GIT can record changes in text, but it is defined as a version control system. You may have used git in one way or another: because of its distributed nature, it is the de facto standard for code versioning, as opposed to centralized Apache Subversion (SVN).

Install Git

To check whether Git is installed, run on the terminal:

$ git version
git version 2.27.0.rc1.windows.1

If not, followhttps://git-scm.com/downloadsInstructions on.
Mac users can use brew to install it:brew install git

Configure Git

We just need to configure something

Git config -- global user.name "front end wit" & & # your name
git config --global user.email  [email protected] &&# your email
Git config -- global init.defaultbranch main # default branch name, compatible with GitHub

You can view the current global configuration with the following command

git config --global --list
# Type ":q" to close

Git stores the configuration in plain text. If you want to modify it directly, you can directly~/.gitconfigor~/.config/git/configEdit the global configuration in.

As the order suggests, remove--globalThis expands the scope of these commands to the current folder. But to test this, we need a repository.

Create a new repository

The repository is just a folder with everything we want to track. Create by command:

mkdir gitexample && 
cd gitexample && 
git init
# gitexample git:(main)

This command is ingitexampleA folder has been created.gitfolder. This hidden.gitA folder is a version Library: all local configurations and modifications are stored here.


Create something in the repository:

echo "Hello, Git " >> hello.txt

functiongit status, we will see the newly created untraceable file.

git status
# On branch main
# No commits yet
# Untracked files:
#  (use "git add <file>..." to include in what will be committed)
#   hello.txt
# nothing added to commit but untracked files present (use "git add" to track)

According to the tips and suggestions, we add files:

git addd . 

If we don’t want to add all the files, we can use

git add hello.txt 

If you check the status of the version library now, you will see that the file has been addedstaged), but not yet submitted.

git status
# On branch main
# No commits yet
# Changes to be committed:
#  (use "git rm --cached <file>..." to unstage)
#   new file:   hello.txt

To document these changes, we submit it.

git commit -m "Add hello.txt"
# [main (root-commit) a07ee27] Adds hello.txt
# 1 file changed, 2 insertions(+)
# create mode 100644 hello.txt

git commit -m <MESSAGE>Is a short command that you can usegit commitOpen the editor (mainly VIM) to provide a detailed description of the submission.

Check submission records:

git log

# Author: qq449245884 <[email protected]>
# Date:   Sat Jul 17 14:57:24 2021 +0800
#    Add hello.txt

Create branch

In many cases, it’s useful to have a separate initial code version: for example, avoid code conflicts when testing functions you’re not sure about, or when working together. That’s what the GIT branch means: it grew from a specific point in history.

To create a branch, rungit branch NAME, to switch branches, rungit checkout NAME。 Or simply

Git checkout - B dev # switches to a new branch called "dev"
# Switched to a new branch 'dev'
# gitexample git:(dev)

We areHello.txtChange something in the file and submit the change:

echo "\nHello, Git Branch" >> hello.txt &&
git commit -am "Change hello.txt"

Now switch to the main branch:

git checkout main &&
cat hello.txt
# Switched to branch 'main'
# Hello, Git

As you can see, the contents of the file are still the same as before. To compare branches, we can run.

git diff dev
# diff --git a/hello.txt b/hello.txt
# index 360c923..b7aec52 100644
# --- a/hello.txt
# +++ b/hello.txt
# @@ -1,3 +1 @@
# Hello, Git
# -
# -Hello, Git Branch
# (END)
# type ":q" to close

We made changes in the main branch:

echo "\nHi from Main Branch" >> hello.txt &&
git commit -am "Change hello.txt from main"
# [main 9b60c4b] Change hello.txt from main
# 1 file changed, 2 insertions(+)

Now let’s try to combine these changes.

git merge dev
# Auto-merging hello.txt
# CONFLICT (content): Merge conflict in hello.txt
# Automatic merge failed; fix conflicts and then commit the result.

Because the file was modified twice in the same place, we had a conflict. Look at this file

cat hello.txt
<<<<<<< HEAD
Hello, Git

Hi from Main Branch
Hello, Git
>>>>>>> dev

There is also a command to view the changes individually:

git diff --ours # :q to close 
git diff --theirs #:q to close

You can manually edit the file and submit changes, but let’s imagine that we only want one version. Let’s start by suspending the merger.

git merge --abort

And restart the merge with the “theirs” policy, which means that in the event of a conflict, we will use what the incoming branch adheres to.

git merge -X theirs dev
# Auto-merging hello.txt
# Merge made by the 'recursive' strategy.
# hello.txt | 5 +----
# 1 file changed, 1 insertion(+), 4 deletions(-)

The opposite of this strategy is “ours”. Combining these two changes requires manual editing (or usinggit mergetool)。

View a list of all branch runs

git branch # type :q to close
#  dev
# * main

Finally, delete the branch run:

git branch -d dev
# Deleted branch dev (was 6259828).

Reset branch

The branch “grows” from a certain point in Git history,rebaseAllow to change this point. Let’s create another branch andhello.txtAdd some changes to the.

git checkout -b story &&
echo "Once upon a time there was a file">>story.txt &&
git add story.txt &&
git commit -m "Add story.txt"
# Switched to a new branch 'story'
# [story eb996b8] Add story.txt
# 1 file changed, 1 insertion(+)
# create mode 100644 story.txt

Now let’s go back to the main branch and add the changes:

git checkout main &&
echo "Other changes" >> changes.txt &&
git add changes.txt &&
git commit -m "Add changes.txt"

Reset us inmainreachstoryBranch changes:

git checkout story &&
git rebase main
# Successfully rebased and updated refs/heads/story.

You can see that the new file created in the main branch is added to thestoryBranch.

# changes.txt hello.txt   story.txt

Note: do notrebaseBranches that others may have used, such as the main branch. Also, remember that every historical operation on the remote repository needs to force these changes to take effect.

Remote repository

If you haven’t, please create a GitHub account, log in and create a new empty warehouse (private or public).

Suppose the name of the repository is“example“, run the following command (change to your user name).

git remote add origin [email protected]:USERNAME/example.git &&
git push -u origin main

You can refresh the page and see the files of the main branch. To push all local branches to the remote warehouse, run.

git push --all origin

We edit something on GitHub: just click on any file and pencil icon. Add a line of any text you want, and then press “submit changes”.

Run this command locally to get remote changes.

git checkout main &&
git pull

Manage uncommitted changes

If you want to save your local changes for later use, you can usegit stash

echo "Changes" >> hello.txt &&
git stash

Now you can use the following commands to check, apply, or discard these changes.

git stash list
# [email protected]{0}: WIP on main: 92354c8 Update changes.txt
Git stash pop # apply changes
Git stash drop # undo modification

You can use the stash number, i.egit stash pop 0To apply a specific repository, orgit stash drop 0To undo.

If you want to discard all local changes, just restore the version library to the last submitted changes, please run.

git restore .

Manage submitted changes

Once you create a commit, the change is saved in the local git history. As mentioned earlier, all modifications that affect remote history requiregit push --force。 Keep this in mind for all the following commands.

Let’s start by editing the final submission.

git commit --amend # type :wq to save and close
# Press "i" to edit, "Esc" to stop editing

How about we reset everything to the beginning?

To find the ID of the first submission, run this command and scroll (down arrow) to the end.

git log --abbrev-commit
# commit a07ee27
# Author: Your Name <[email protected]>
Date:   Sun Jul 11 11:47:16 2021 +0200

    Adds hello.txt
# type ":q" to close

Now run this to reset the repository, but keep all changes from being cached.

git reset --soft COMMIT # e.g. a07ee27

On the contrary, you can also perform a hard reset withgit reset --hard COMMITTo delete all modifications. There are several other reset methods that you can use fromGit documentLearned in.


Most of the time, you only need to use a few commands (mainly checkout, add, commit, pull, push and merge), but some commands may be “just in case”.

One way to store this information isgit aliases。 To configure an alias, simply set it in the configuration. For example, an alias I often use isgit tree, it prints out a beautiful history log in the form of a tree.

git config --global alias.tree 'log --graph --decorate --pretty=oneline --abbrev-commit'
# Try it with `git tree`

Another useful alias is to delete all merged branches.

git config --global alias.clbr '!git branch --merged | grep -v \* | xargs git branch -D' 

You can see that its prefix is“!“, this allows us to use any command, not just the GIT command.

~After that, I’m Zhi, who wrote it on Saturday. I’m going to brush the dishes. The bones are white!

Original text:https://dev.to/valeriavg/mast…

The bugs that may exist after code deployment cannot be known in real time. Afterwards, in order to solve these bugs, we spent a lot of time on log debugging. By the way, we recommend a useful bug monitoring toolFundebug


The article is continuously updated every week. You can search “Daqian world” on wechat to read and urge more for the first time (one or two articles earlier than the blog). This article GitHubhttps://github.com/qq449245884/xiaozhiI have collected many documents and I have welcomed Star and perfect. You can review the test points and pay attention to the official account.welfare, you can see the benefits, you know.

Take you to master the GIT commands commonly used in work with examples in 7 minutes