The 7-minute use case takes you through the git commands commonly used at work

Author: Valeria
Translator: Front-end wit
Source: medium

Git can essentially record text changes, 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 a de facto standard for code version control, as opposed to a centralized Apache Subversion (SVN).

Install git

To check if Git is installed, run at the terminal:

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

If not, follow https://git-scm.com/downloads Description on.
Mac users can install it with brew: 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 johndoe@example.com && # Your mailbox
git config --global init.defaultbranch main # Default branch name, compatible with GitHub

You can view the current global configuration with the following commands

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

The git stores the configuration in plain text, but if you want to modify it directly, you can do so directly at ~/. gitconfig or ~/. Edit the global configuration in config/git/config.

As the commands suggest, removing the--global s extends the scope of these commands to the current folder. To test this, however, 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 creates one in the gitexample folder. Git folder. This is hidden. The git folder is the version library: all local configuration and modifications are stored there.

change

Create something in the repository:

echo "Hello, Git " >> hello.txt

Run git status and we will see the newly created untracked 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)

As prompted, we add the file:

git addd . 

If we don't want all files added 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 added (also known as stage), but has not yet been 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 record these changes, we'll submit it 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 allows you to open the editor (mainly vim) with git commit to provide a detailed description of the submission.

Check submission records:

git log

# Author: qq449245884 <44924566884@qq.com>
# Date:   Sat Jul 17 14:57:24 2021 +0800
#
#    Add hello.txt
#

Create Branch

In many cases, it is useful to have a separate initial version of the code: for example, to avoid code conflicts when testing functionality you are unsure of or working together. This is what the git branch means: it starts at a particular point in history.

To create a branch, run git branch NAME, switch branches, run git checkout NAME. Or simply

git checkout -b dev # Switch to a new branch named "dev"
# Switched to a new branch 'dev'
# gitexample git:(dev)

We're in Hello. Change something in the txt file and commit the changes:

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 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 make 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 changes individually:

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

You can edit the file manually and submit the changes, but let's imagine that we only want one version. We'll start by aborting the merge.

git merge --abort

And restart the merge with the "theirs" policy, which means that in case of conflict, we will use what the incoming branch insists on.

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(-)

Contrary to this strategy is "ours". Merging these two changes together requires manual editing (or git 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

Branches "grow" from a point in git history, and rebase allows this point to be changed. Let's create another branch, and in hello. Add some changes to the txt.

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 the changes we made in the main to store branch:

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

You can see that new files created in the main branch are added to the story branch.

ls
# changes.txt hello.txt   story.txt

Note: Do not rebase branches that others may have used, such as the main branch. Also, keep in mind that each historical operation on a remote version library forces these changes to take effect.

Remote Repository

If you don't already have one, create a GitHub account, log in and create a new empty warehouse (private or public).

Assuming the name of the version library is "example", run the following command (change to your user name).

git remote add origin git@github.com: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 a remote warehouse, run.

git push --all origin

We edit something on GitHub: just click on any file and pencil icon. Add a line of whatever text you want and 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 use git stash.

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

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

git stash list
# stash@{0}: WIP on main: 92354c8 Update changes.txt
git stash pop # Apply changes
git stash drop # Undo Modification

You can use the stash number, git stash pop 0, to apply a specific repository, or git stash drop 0 to undo.

If you want to discard all local changes, simply restore the version library to the last committed changes, run.

git restore .

Manage submitted changes

Once you create a submission, this change is saved in your local git history. As mentioned earlier, git push --force is required for all modifications that affect remote history. Keep this in mind for all the commands below.

We 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 your first submission, run this command and scroll (down arrow) to the end.

git log --abbrev-commit
# commit a07ee27
# Author: Your Name <your@email.address>
Date:   Sun Jul 11 11:47:16 2021 +0200

    Adds hello.txt
(END)
# type ":q" to close

Run this now to reset the version library, but keep all modifications uncached.

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

Conversely, you can also do a hard reset, using git reset --hard COMMIT to delete all modifications. There are several other ways to reset, which you can do from git document I learned.

alias

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

One way to store this information is git aliases. To configure an alias, simply set it in the configuration. For example, one of the aliases I often use is git tree, which prints a beautiful history log as 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 git commands.

~Finish, I brush bowl wisdom, written this Saturday, to prepare to brush bowl, bone white!

Original: https://dev.to/valeriavg/master-git-in-7-minutes-gai

Communication

The article is continuously updated every week, so WeChat can search for "The Big Move World" for the first time to read and hurry up (one or two earlier than the blog), GitHub https://github.com/qq449245884/xiaozhi I have included, sorted out a lot of my documents, welcome to Star and perfect, you can refer to the reference point for review, and pay attention to the public number, the background reply benefits, you can see benefits, you understand.

Keywords: git svn Vue css

Added by rabab.orthalis on Mon, 20 Dec 2021 05:30:41 +0200