Kyleten

Kyleten

A running around noob, just wondering and wandering.

01 Getting Started with Git One

Preface#

Git is currently the most popular version control software in the market. Instead of learning it from scratch and step by step, I prefer to start using the software first and then further study it. If you already have a basic understanding of Git and want to further study it, you can read the book ProGit on the Git official website. If you prefer to follow a book for systematic learning, you can also read ProGit and after reading it, you will have a thorough understanding of how to use Git. For readers who share the same thoughts as me, this article is written for you.

Installation#

PlatformInstallation Method
WindowsInstall Git Bash official website
Mac / LinuxUse package manager

Windows can also be installed using package management tools, such as Scoop

Workflow#

Although there are already many graphical interfaces on the market, the author still recommends using CLI.

User Configuration#

To commit code to a remote repository using Git, you need to configure your username and email. Here, we use global configuration --global.

# The configuration file of Git, .gitconfig, is similar to TOML
git config --global user.name "kyleten"
git config --global user.email "[email protected]"
# The following is the gpg configuration, which can be ignored for now
# git config --global user.signingkey xxxxxxxxx!
# git config --global commit.gpgsign true

# Personal preference, change the default branch to main
git config --global init.defaultbranch main

The preview of the .gitconfig file is as follows:

[user]
name = kyleten
email = k[email protected]
# The following is the gpg configuration, which can be ignored for now. It will be explained in a future article
# signingkey = xxxxxxxx!

[init]
defaultbranch = main

# [commit]
# gpgsign = true

Initialize Repository#

Now let's start building our first personal repository.

git init . # If you want to use the current directory as the git repository
# git init firstRepository # Create a new repository named firstRepository

Track Documents#

Put some files into the repository.

# README.md
this is a tutorial for Git

# a.txt
aaa

# b.txt
bbb

The current repository directory is as follows:

.
├── a.txt
├── b.txt
└── README.md
  • Use the git status command to check the file status.
# Output
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        README.md
        a.txt
        b.txt
        

nothing added to commit but untracked files present (use "git add" to track)

Currently, a.txt and b.txt in the directory are in an untracked state. Changes to files in this state will not be recorded or committed.

  • Use git add filename to track the files.

  • Use git commit -m "comment" to commit the changes.

# Output
[main (root-commit) af31137] init
 3 files changed, 3 insertions(+)
 create mode 100644 README.md
 create mode 100644 a.txt
 create mode 100644 b.txt

Committing is similar to taking a snapshot of the current file directory and recording it in the local git repository.

Next Section Preview#

Branches & Merging#

Mermaid Loading...

Remote Repository#

Push the local repository to the remote repository, fetch from the remote repository to the local repository, and merge.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.