[Embedded] git tool basic command study notes | Embedded Linux


Preface

Reference: Feiling Embedded ElfBoard ELF-1 Software Learning Manual
chatGPT

1. Basic git instructions

First, we create a new folder named git and create a main.c in it. Later, we will use this C file as an example for git management:

Insert image description here
First check whether the git version on the ubuntu virtual machine is the latest:

sudo apt-get install git

Great, it’s already up to date:
Insert image description here
start command:

git init

git init is a Git command used to create a new Git repository in the current directory . This command will generate a hidden folder .git in the current directory, which is used to store the configuration information and version history of the Git warehouse. After executing this command, you can start using Git for version control.
We can use ls-a to see the hidden files of .git:

ls -a

Insert image description here
Then we add username and email:

git config --global user.name "XXX"
git config --global user.email "XXX"

The git config --global user.name "XXX" and git config --global user.email "XXX" commands are used to set the global configuration of the Git user, where "XXX" are your username and email respectively. These two configuration information will be used for author information every time you submit code.
Insert image description here

Then add main.c to the staging area of ​​the git repository:

git add main.c

Insert image description here
The git add main.c command is used to add the file main.c to the Git staging area. After executing this command, the current status of main.c will be recorded in the Git staging area , waiting for further commits. This is done to incorporate the changes to the file into the next commit. After executing git add, you can use the git commit command to commit these changes. Note, this is just the temporary storage area, it has not been submitted yet.

At this time we can use the command to check the status changes of the temporary storage area :

git status

Insert image description here
The git status command is used to display the status of the working directory and staging area.
The screenshot means: you are currently in a new Git repository and on the master branch . Some files have been added to the staging area , including a new file main.c. If you want to unstage a file, you can use the git rm --cached command.
Next, you can execute the git commit command to commit these changes. After executing git commit, Git will ask you to enter information related to this submission, such as submission message, etc.

Next we submit:

git commit -m "instial program"

git commit -m "instial program" is a command used to commit code changes to the Git version control system. This command contains two main parts:

git commit: This section tells Git that you want to create a new commit that saves the current changes to version history.

-m "instial program": This is an option -m used to specify a commit message or description on the command line. In this example, the commit message is "instial program", which describes the changes made by this commit or the purpose of the commit. The "xxx" part in the double quotes in the git commit -m "xxx" command is the commit message you need to fill in. This message should briefly describe the changes or features you added in this commit. Such commit messages can help you and other team members more easily understand the purpose of each commit, and also make it easier to track and manage code changes in version history. Therefore, please replace the "xxx" part with the actual change content or relevant information.
Insert image description here
Let's check the status again:
Insert image description here
in the output now, it shows that the branch you are currently on is master, and prompts "nothing to commit, working directory clean", which means that your working directory is clean and there is nothing uncommitted. Change.

We can also use the following command to view commit record information:

git log

The git log command is used to display version commit history. When you run this command, details about each commit are displayed, including the committer, commit date, commit hash, and commit message.
Insert image description here
The output here means: your git log output shows a commit with hash dd73feaa05ff6f7e5b3b52b4e878c2101aab1272, author elf_li, commit date Mon Jan 8 12:04:00 2024 +0800, and the commit message is "instial program"

2. git branch management

2.1. Some basic commands

Use the following command to view the local branch :

git branch

Insert image description here
Currently on the master branch

Check the status:
Insert image description here
Now let’s modify the file: (Create a new main1.c and delete main.c) ( Now there are changes in the workspace )
Insert image description here

git rm main.c
git add main1.c

Delete the file named main.c, and then add the file named main1.c to the staging area.
Insert image description here
Then we submit:

git commit -m "delete main.c_add main1.c"

Insert image description here
This paragraph is about a Git commit. It means that you made a commit in which a file named main.c was deleted and a new file named main1.c was created. This commit affected a total of two files, deleted 5 lines of content, and changed the file's schema.

It is worth noting that this error is often reported here:
error: bad index file sha1 signature
fatal: index file corrupt.
Usually I can successfully run the command again, but I still get an error the first time I type the command. I don’t know why.

Then take a look at this command:

git show

The git show command is used to display detailed information about one or more Git objects, including committed changes, author, commit time, etc. If you run git show from the command line, it will display the latest commit details.
Insert image description here

git show --stat

The git show --stat command is used to display summary information of commits and statistics of changes. It displays brief statistics for each modified file, including the number of lines inserted and deleted.
Please add image description

git log 

Command is used to display commit history.
Insert image description here
There are two commits, each including the commit hash, author, date, and commit message. The first commit removed the main.c file and added the main1.c file, while the second commit was the initializer.

git show dd73feaa05ff6f7e5b3b52b4e878c2101aab1272

git show [commitid] to view the specific modifications of a certain submission.
Insert image description here
You can also add --stat:

git show dd73feaa05ff6f7e5b3b52b4e878c2101aab1272 --stat

Insert image description here

2.2. Time backtracking

git reset --hard is a Git command used to roll back your working directory, staging area, and current branch to the specified commit or branch. This operation will discard all uncommitted changes and history.

git reset --hard dd73feaa05ff6f7e5b3b52b4e878c2101aab1272

The git reset --hard dd73feaa05ff6f7e5b3b52b4e878c2101aab1272 command will roll back both the workspace and the staging area to the specified commit dd73feaa05ff6f7e5b3b52b4e878c2101aab1272, and discard all subsequent commits.
The effect of this command is very strong. It will return all changes in your working directory and staging area to the state of the specified commit. Before using this command, make sure you understand its impact, as it permanently discards all changes made after the specified commit.
The following is the experimental effect:
Insert image description here

2.3. Create a new branch, delete a branch, and rename a branch.

The command to create a new branch and switch to it is as follows:

git checkout -b test

The following are the experimental results:
Insert image description here
rename the branch:

git branch -m test project_test

Insert image description here
Delete branch:

git branch -d test

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_46274756/article/details/135454029