Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Basic introduction to Git (3) basic operation of Git

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

There are two ways to obtain a Git repository:

The first is to import all files into Git under an existing project or directory

The second is to clone an existing Git repository from a server

Initialize the Git repository:

Git init # enables Git to manage the current directory (initialization)

1. This command will create a subdirectory called .git, which contains all the necessary files in your initialized Git repository, which are the backbone of the Git repository.

2. If you initialize the Git repository in a non-empty directory for version control, you should start tracking these files and submitting them

Git add * .py # tracks all .py files in the current directory (put the files in the staging area)

Git commit-m 'version1' # submit-m specify additional information (submit the file to the Git repository)

Clone an existing warehouse

If you want to get an existing Git repository, you need to use the git clone command. Git clones almost all the data on the Git warehouse server, rather than just copying the files needed to complete your work.

When the git clone command is executed, every version of every file in the remote Git repository will be pulled down by default

If the disk of your server is broken, you can use any cloned client to rebuild the warehouse on the server.

Git clone [url]

Example: git clone https://github.com/libgit2/libgit2

This creates a directory called libgit2 under the current directory, initializes a .git directory under this directory, pulls all data from the remote warehouse and puts it into the libgit2 directory

Git clone https://github.com/libgit2/libgit2 mylibgit

This will perform the same operation as the previous command, except that the name of the repository created locally becomes mylibgit.

Each file in the working directory has only two states: tracked or untracked

Tracked files refer to those files that are under version control and were recorded in the last snapshot. After working for a period of time, their status may be unmodified, modified or placed in a temporary storage area.

All files in the working directory except tracked files are untracked files, which neither exist in the record of the last snapshot nor placed in the staging area

The life cycle of the Git file:

After editing some files, Git marks them as modified because you have modified them since they were last submitted

We put these modified files in the temporary storage area, and then commit all the temporary changes

Check the current file status

Git status # View the status of all files in the Git repository

Example: git status

On branch master # Git branch currently used (branch name: master)

Nothing to commit, working directory clean # has no files to submit (that is, the current directory has not changed since the last commit)

Example: echo'My Project' > test.py

Git status

On branch master

Untracked files: # indicates that there are files or directories under the working directory for tracking

(use "git add..." To include in what will be committed)

Test.py # Files or directories displayed as tracked

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

Untracked files mean that Git does not have them in previous snapshots, and Git will not automatically include them in the scope of tracking unless you explicitly tell it, "I need to track this file."

Track new files

Git add test.py

Run the git status command again at this time, and you will see that the test.py file has been tracked and is in a temporary state:

Git status

On branch master

Changes to be committed: # indicates that there are changes to be submitted

(use "git reset HEAD..." To unstage)

New file: test.py # new files (that is, files that do not exist in previous snapshots)

Temporarily save the modified file

Git commit-m 'version-1' # submit the previous action

Echo'#! / usr/bin/env python' > test.py # modify the contents of the test.py file

Git status

On branch master

Changes not staged for commit: # unsubmitted follow-up changes

(use "git add..." To update what will be committed)

(use "git checkout--..." To discard changes in working directory)

Modified: test.py # modified file or directory

Git add test.py # put the modified file in the scratch area

Git status

On branch master

Changes to be committed: # indicates that there are changes to be submitted

(use "git reset HEAD..." To unstage)

Modified: test.py # modified file or directory

Now that the file is in temporary storage, it will be recorded in the warehouse the next time it is submitted. At this point, if you change the contents of the file in test.py

Echo'# coding:utf-8' > > test.py # modify the contents of test.py file

Git status

On branch master

Changes to be committed:

(use "git reset HEAD..." To unstage)

Modified: test.py

Changes not staged for commit:

(use "git add..." To update what will be committed)

(use "git checkout--..." To discard changes in working directory)

Modified: test.py

Now that the test.py file appears in both the staging area and the non-staging area, in fact Git only temporarily stores the version you used to run the git add command.

Therefore, files that have been revised after running git add need to rerun git add to temporarily store the latest version

Git add test.py

Git commit-m 'version-2' # submit updates (if you do not use the-m option, a default editor will appear for you to enter updated tag information)

Git add is a multi-function command that can be used to track new files, put tracked files in a temporary storage area, and mark conflicting files as resolved when merging.

Git status-s:

The output of the git status command is very detailed, but its language is somewhat cumbersome. Use the git status-s command to get a more compact format of output

The newly added untracked file is preceded by? Marking

Files newly added to the staging area are preceded by an A tag

The modified file is preceded by an M mark.

Example: M test.py

M test.py

M has two possible locations, the M on the right indicates that the file has been modified but has not yet been placed in the temporary storage area, and the M on the left indicates that the file has been modified and placed in the temporary storage area.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 231

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report