alex_bn_lee

导航

[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 2025-01-15 12:37  McDelfino  阅读(23)  评论(0)    收藏  举报