Essentially, Git logs changes to text, but by definition is a version control system. Chances are you’re already using Git in one way or another: because of its distributed nature, it’s the de facto standard for code versioning, as opposed to centralized Apache Subversion (SVN).

Install git

To check if You have Git installed, run it from a terminal:

$ git version
git version 2.27. 0.rc1.windows1.
Copy the code

If not, follow the instructions at git-scm.com/downloads. Mac users can install it using brew: brew install git.

Configure git

We just need to configure a few things

git config --global user.name "I planted the sun."Git config --global user.email liuqinglin_cc@163.Git config --globalDefaultbranch main # defaultbranch name, compatible with GitHubCopy the code

You can use the following command to view the current global configuration

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

Git stores the configuration in plain text. If you want to modify it directly, you can edit the global configuration directly in ~/.gitconfig or ~/.config/git/config. As the command suggests, removing –global extends the scope of these commands to the current folder. But to test this, we need a repository.

Create a new repository

A repository is just a folder with everything we want to keep track of. Run the following command to create:

mkdir gitexample && 
cd gitexample && 
git init
# gitexample git:(main)
Copy the code

This command creates a.git folder within the gitExample folder. This hidden.git folder is the repository: all local configurations and changes are stored there.

change

Create something in the repository:

echo "Hello, Git " >> hello.txt
Copy the code

Run Git status to see the newly created untraced files.

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)
Copy the code

As prompted, we add the file:

git add .
Copy the code

If we don’t want all the files to be added we can use it

git add hello.txt 
Copy the code

If you now check the state of your version library, you will see that files have been added (aka staged), but not yet committed.

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

To record these changes, let’s 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
Copy the code

Git commit -m

is a short command that you can use to open the editor (mainly Vim) and provide a detailed commit description.

Check the submission record:

git log

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

Create a branch

There are many situations where it’s useful to have a separate initial version of the code: for example, when testing features you’re unsure about, or to avoid code conflicts when working together. That’s what a Git branch is all about: it grew from a specific point in history.

To create a branch, run Git branch NAME, and to switch branches, run Git checkout NAME. Or simply

Git checkout -b dev # Switched to a new branch named "devnew branch 'dev'
# gitexample git:(dev)
Copy the code

We make some changes in the hello.txt file and commit the changes:

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

Now, switch to the main branch:

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

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

git diff dev # diff --git a/hello.txt b/hello.txt # index 360c923.. b7aec52100644
# --- a/hello.txt
# +++ b/hello.txt
# @@ -1.3 +1 @@
# Hello, Git
# -
# -Hello, Git Branch
# (END)
# type ":q" to close
Copy the code

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(+)
Copy the code

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.
Copy the code

Because the file was changed 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
Copy the code

There is also a command to view the changes separately:

git diff --ours # :q to close 
git diff --theirs #:q to close
Copy the code

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

git merge --abort
Copy the code

And re-launch the merge with a “theirs” policy, which means that in the event of a conflict we will use what the incoming branch insisted 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(-)
Copy the code

The opposite of this policy is “ours”. To merge the two changes together, you need to edit them manually (or use git mergeTool).

View a list of all branch runs

git branch # type :q to close
#  dev
# * main
Copy the code

Finally, delete the branch run:

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

Reset the branch

Branches “grow” from a point in Git history, and Rebase allows you to change that point. Let’s create another branch and add some changes to hello.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
Copy the code

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"
Copy the code

Reset the changes we made in the main to Story branch:

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

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

ls
# changes.txt hello.txt   story.txt
Copy the code

Note: Do not rebase branches that others may have used, such as the main branch. Also, keep in mind that every historical operation on a remote repository requires 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 repository (private or public).

Assuming the repository name is “example”, run the following command (change your username).

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

You can refresh the page to see the main branch file. To push all local branches to the remote repository, run.

git push --all origin
Copy the code

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

Run this command locally to get remote changes.

git checkout main &&
git pull
Copy the code

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
Copy the code

You can now use the following commands to check, apply, or discard the changes.

git stash list
# stash@{0}: WIP on main: 92354c8 Update changes. TXT git stash popCopy the code

You can apply a specific repository with the Stash number, git Stash pop 0, or undo with the Git Stash drop 0.

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

git restore .
Copy the code

Manage committed changes

Once you create a commit, the changes are saved in your local Git history. As mentioned earlier, all changes that affect remote history require git push –force. Keep this in mind with all of the following commands.

We start by editing the final submission information.

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

How about we reset everything back to the beginning?

To find the ID submitted for the first time, 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
(END)
# type ":q" to close
Copy the code

Now run this to reset the repository, but keep all changes out of cache.

git reset --soft COMMIT # e.g. a07ee27
Copy the code

Instead, you can do a hard reset, using git reset –hard COMMIT to delete all changes. There are several other ways to reset, which you can read about in the Git documentation.

The alias

Most of the time, you only need to use a few commands (checkout, add, commit, pull, push, and merge, primarily), but there are some that you might want to use “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 alias I often use is Git Tree, which prints a nice 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`
Copy the code

Another useful alias is to remove all merged branches.

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

You can see it’s prefixed with “!” This allows us to use any command, not just git commands.