alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

[1092] Git Tutorial

References

  1. Git Tutorial - W3School
  2. An Intro to Git and GitHub for Beginners (Tutorial)


Using Git with Command Line

git --version
git version 2.30.2.windows.1

Configure Git

git config --global user.name "w3schools-test"
git config --global user.email "test@w3schools.com"

Creating Git Folder

mkdir myproject
cd myproject

Initialize Git

git init
Initialized empty Git repository in /Users/user/myproject/.git/

Git Adding New Files

We check the Git status and see if it is a part of our repo:

git status
On branch master
No commits yet
Untracked files:
(use "git add ..." to include in what will be committed)
index.html
nothing added to commit but untracked files present (use "git add" to track)

Files in your Git repository folder can be in one of 2 states:

  • Tracked - files that Git knows about and are added to the repository
  • Untracked - files that are in your working directory, but not added to the repository

Git Staging Environment

One of the core functions of Git is the concepts of the Staging Environment, and the Commit.

As you are working, you may be adding, editing and removing files. But whenever you hit a milestone or finish a part of the work, you should add the files to a Staging Environment.

Staged files are files that are ready to be committed to the repository you are working on. You will learn more about commit shortly.

For now, we are done working with index.html. So we can add it to the Staging Environment:

git add index.html

Git Add More than One File

You can also stage more than one file at a time.

Now add all files in the current directory to the Staging Environment:

git add --all
or
git add -A
or
git add .

Using --all instead of individual filenames will stage all changes (new, modified, and deleted) files.

git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: README.md
new file: bluestyle.css
new file: index.html

Git Commit

Since we have finished our work, we are ready move from stage to commit for our repo.

git commit -m "First release of Hello World!"
[master (root-commit) 221ec6e] First release of Hello World!
3 files changed, 26 insertions(+)
create mode 100644 README.md
create mode 100644 bluestyle.css
create mode 100644 index.html

The commit command performs a commit, and the -m "message" adds a message.

The Staging Environment has been committed to our repo, with the message:
"First release of Hello World!"

Git Commit Log

To view the history of commits for a repository, you can use the log command:

git log
commit 09f4acd3f8836b7f6fc44ad9e012f82faf861803 (HEAD -> master)
Author: w3schools-test
Date: Fri Mar 26 09:35:54 2021 +0100
Updated index.html with a new line
commit 221ec6e10aeedbfd02b85264087cd9adc18e4b26
Author: w3schools-test
Date: Fri Mar 26 09:13:07 2021 +0100
First release of Hello World!

Git Help

If you are having trouble remembering commands or options for commands, you can use Git help.

There are a couple of different ways you can use the help command in command line:

  • git command -help -  See all the available options for the specific command
  • git help --all -  See all possible commands

 Let's go over the different commands.


Git -help See Options for a Specific Command

Any time you need some help remembering the specific option for a command, you can use git command -help:

New Git Branch

git branch hello-world-images

Now we created a new branch called "hello-world-images"

Let's confirm that we have created a new branch:

git branch hello-world-images
* master

We can see the new branch with the name "hello-world-images", but the * beside master specifies that we are currently on that branch.

checkout is the command used to check out a branch. Moving us from the current branchto the one specified at the end of the command:

git checkout hello-world-images
Switched to branch 'hello-world-images'

Now we have moved our current workspace from the master branch, to the new branch

Switching Between Branches

git checkout master
Switched to branch 'master'

Merge Branches

git checkout hello-world-images
Switched to branch 'hello-world-images'

 

posted on   McDelfino  阅读(1)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2021-01-15 【523】python存储变量方法
2021-01-15 【522】深度学习超参数调节
2019-01-15 【348】通过 Numpy 创建各式各样的矩阵
2019-01-15 【347】将jupyter notebook嵌入博客园
2018-01-15 【290】Python 常用说明
2018-01-15 【290】Python 模块(自定义包)
2018-01-15 【290】Python 函数
点击右上角即可分享
微信分享提示