shell learning notes git related use

Keywords: Linux git

Excerpt from Chapter 6 warehouse management of Linux Shell script introduction

brief introduction

Git is already included in most Linux distributions. If it is not installed on your system, you can get it from yum (Redhat or SuSE) or apt get (Debian or Ubuntu)

$ sudo yum install git-all
$ sudo apt-get install git-all

Create a new git repository

If you are developing your own project, you can create a corresponding project warehouse. Warehouses can be created on local systems or on remote sites such as GitHub

All projects in git need a master folder for saving project files

$ mkdir MyProject 
$ cd MyProject

The git init command creates a subdirectory. git in the current working directory and initializes the git configuration file

$ git init

If you want remote users to have access to the warehouse

$ git update-server-info

Clone remote git repository

Clone from a known remote site, such as GitHub

$ git clone http://github.com/ProjectName

Clone from a site that requires a user name and password (possibly your own server)

$ git clone clif@172.16.183.130:gitTest clif@172.16.183.130's password:

Adding and submitting changes using git

The git add command adds changes in the working code to the staging area

This command does not change the contents of the warehouse. It just marks the change and adds it to the next submission

$ vim SomeFile.sh 
$ git add SomeFile.sh

You can also add multiple files at once

$ git add *.c

The git commit command can submit changes to the repository

$ vim OtherFile.sh 
$ git add OtherFile.sh 
$ git commit

The git commit command will open the EDITOR defined in the shell environment variable EDITOR, which contains the following pre generated
text

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Committer: ClifFlyntclif@cflynt.com
# On branch branch1
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
# modified: SomeFile.sh
# modified: OtherFile.sh

After you enter the comment information, your changes are saved in the local copy of the warehouse

You can shorten the input of the add/commit operation with the - a and - m options

  • -a: Add new code before submitting
  • -m: Specify a message without entering the editor
git commit -am "Add and Commit all modified files."

Creating and merging branches using git

Switch to the previously created branch

$ git checkout OldBranchName

You can use the option - b of checkout to create new branches

$ git checkout -b MyBranchName
Switched to a new branch 'MyBranchName'

The git branch command allows you to view branches

$ git branch 
* MyBranchName 
master

The current branch is highlighted by an asterisk (*)

Example

After creating a new branch, adding and submitting the changes, switch back to the starting branch, and then use the git merge command to merge the changes into the new branch

$ git checkout originalBranch 
$ git checkout -b modsToOriginalBranch 
# Edit, test 
$ git commit -a -m "Comment on modifications to originalBranch" $ git checkout originalBranch 
$ git merge modsToOriginalBranch

If the branch is no longer needed after merging, you can delete it with option - d

$ git branch -d MyBranchName

Share work results

Make patches

The format patch command will collect your changes and create one or more patch files. The patch file name consists of a number, a description, and. Patch

The format patch subcommand with the parent branch name as a parameter will generate a patch file for the current branch

$ git checkout master 
$ git checkout -b newFeature 
# Edit, add, and submit 
$ git format-patch master 
0001-Patch-add-new-feature-to-menu.patch 
0002-Patch-support-new-feature-in-library.patch

You can use the git log command to view all submitted logs in the warehouse

$ git log 
commit 82567395cb97876e50084fd29c93ccd3dfc9e558 
Author: Clif Flynt <clif@example.com> 
Date: Thu Dec 15 13:38:28 2016 -0500

Fixed reported bug #1

commit 721b3fee54e73fd9752e951d7c9163282dcd66b7 
Author: Clif Flynt <clif@example.com> 
Date: Thu Dec 15 13:36:12 2016 -0500

Created new feature

The GIT format patch command using SHA1 as a parameter has the following form

$ git format-patch SHA1
# You can use the complete SHA1 string in the command or only the beginning part that is not repeated
$ git format-patch 721b 
$ git format-patch 721b3fee54e73fd9752e951d7c9163282dcd66b7

# You can also identify a snapshot based on its distance from the current location, which can be selected through the options-#To achieve
# The following command generates a patch file for the most recent change on the main branch
$ git format-patch -1 master
# The following command generates patch files for the last two changes on the bleedingEdge branch
$ git format-patch -2 bleedingEdge

Option – check can test whether the patch is valid

$ git apply --check 0001-Patch-new-feature.patch 
error: patch failed: feature.txt:2 
error: feature.txt: patch does not apply

If you pass the – check test, you can apply the patch using the git apply command

$ git apply 0001-Patch-new-feature.patch

Push branch

The git push command can push branches to the mainline

$ git push origin MyBranchName

After modifying the existing branch, you may receive the following error message

  • -remote:error:Refusing to update checked out branch: refs/heads/master
  • -remote:error:By default, updating the current branch in a non-bare repotory

In this case, the change needs to be pushed to a new branch at a remote location

$ git push origin master:NewBranchName

The get fetch and git pull commands can download data from a remote location to a local warehouse

The repository to be cloned is named origin

$ get fetch origin

The following commands can get data from other developer repositories

$ get fetch Username@Address:Project

The git pull command gets and merges the changes into the working code

$ git pull origin 
$ git pull Username@Address:Project

Check git warehouse status

The git status command outputs the current status of the project. It will tell you which branch you are currently in, whether there are uncommitted changes and whether you are in sync with the origin repository ①

$ git status 
# On branch master 
# Your branch is ahead of 'origin/master' by 1 commit.
# 
# Changed but not updated:
# se "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in workingdirectory) 
# 
#modified: newFeature.tcl

# Your branch is ahead of 'origin/master' by 1 commit.

The following line describes that the document has been modified but has not been submitted

#modified: newFeature.tcl 
gitconfig --global user.name "Your Name" 
gitconfig --global user.email you@example.com

If the identity information used for submission is incorrect, you can use the following command to correct it

git commit --amend --author='Your Name <you@example.com>' 
1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 testfile.txt

View git history

The git log command can generate a report to help you understand a series of changes in the project

$ git log 
commit fa9ef725fe47a34ab8b4488a38db446c6d664f3e Author: Clif Flynt <clif@noucorp.com> 
Date: Fri Dec 16 20:58:40 2016 -0500 Fixed bug # 1234

Find bug s

The GIT blank command returns a list of submitted SHA, author, submission date, and the first line of submission information

$ git blame testGit.sh 
d5f62aa1 (Flynt 2016-12-07 09:41:52 -0500 1) Created testGit.sh 063d573b (Flynt 2016-12-07 09:47:19 -0500 2) Edited on master repo. 
2ca12fbf (Flynt 2016-12-07 10:03:47 -0500 3) Edit created remotely and merged.

The git bisect command finds the submission that caused the problem

The git bisect command requires two identifiers, one for the last known good code,

The other is for bad release. The bisect command finds an intermediate submission point between good and bad code for testing

# Pull the current (bug gy) code into the git repository 
$ git checkout buggyBranch

# Initialize git bisect 
$ git bisect start

# Mark current submission as bad 
$ git bisect bad

# Mark submissions without problems as good 
# Pull the intermediate submission point for testing
$ git bisect good v2.5 
Bisecting: 3 revisions left to test after this (roughly 2 steps) [6832085b8d358285d9b033cbc6a521a0ffa12f54] New Feature

# Compile and test 
# Mark as good or bad 
# Pull the next submission for testing 
$ git bisect good Bisecting: 1 revision left to test after this (roughly 1 step) [2ca12fbf1487cbcd0447cf9a924cc5c19f0debf9] Merged. Merge branch 'branch1'

The GIT bisect command can find out the intermediate version between good and bad versions. You can build and test this version, and then rerun git bisect to mark good or bad. Then git bisect finds another new intermediate version between good and bad versions

Snapshot label

git supports lightweight tags (only for snapshots) and annotation tags
The git tag is valid only locally. git push does not push labels by default. To send tags to the origin repository, you must add the option - tags

$ git push origin --tags

The git tag command includes options for adding, removing, and listing labels

$ git tag 
release-1.0 
release-1.0beta 
release-1.1

You can create a label in the current checkout by adding a label name

$ git tag ReleaseCandidate-1

Add SHA-1 specified for submission to git tag command

$ git tag ReleaseCandidate-1
# Add SHA-1 of the specified submission to the gittag command to label the submission

$ git log --pretty=oneline 72f76f89601e25a2bf5bce59551be4475ae78972 Initialcheckin fecef725fe47a34ab8b4488a38db446c6d664f3e Added menu GUI ad606b8306d22f1175439e08d927419c73f4eaa9 Added menu functions 773fa3a914615556d172163bbda74ef832651ed5 Initial action buttons

$ git tag menuComplete ad606b

# Option - a can annotate the label:

$ git tag -a tagWithExplanation 
# git opens the editor and creates annotations

# You can use the - m option on the command line to define information:

$ git tag -a tagWithShortMessage -m "A short description"

# If you use the git show command, the following information is displayed:

$ git show tagWithShortMessage

tag tagWithShortmessage Tagger: Clif Flynt <clif@cflynt.com> Date: Fri Dec 23 09:58:19 2016 -0500

A short description ...

# Option - d removes the label
$ git tag 
tag1 
tag2 
tag3 
$ git tag -d tag2 
$ git tag 
tag2 
tag3F

Submission information specification

  • The length of each line is about 72 characters. Separate paragraphs with blank lines.
  • The first line should be about 50 characters long and summarize the reason for this submission. The content should be specific enough, not general, so that users can see what they have done at a glance
  • Don't write it as Fix bug, or even Fix bugzilla bug #1234. Instead, write remove still messages that appear each April 1

Posted by apw on Tue, 30 Nov 2021 16:03:40 -0800