GIT novice boarding notes

1. Submit modifications to the local warehouse

1. Initialize the current directory as a workspace

git init

When executed, a hidden folder. Git is generated in the current directory, which contains git's current configuration, etc.

2. Modify user.name and user.email configurations

git config --global user.name "Your name or nickname"
git config --global user.email "Your mailbox"

3. Manually add exclusion files.gitignore;

#Exclude.a files
*.a


#Include lib.a files separately
!lib.a


#Exclude only dir folders in the directory where.gitignore files are located
/dir


#Exclude all files under folder dir
dir/


#Exclude all.txt files in the first level directory under folder dir
dir/*.txt


#Exclude.txt files for all subdirectories under folder dir
dir/**/*.txt


4. Add Files to Staging Area

- Add all modifications

git add .

Add Individually

git add <FileName>

5. Delete staging area files

git rm --cache <FileName>

6. Submit staging area to local warehouse

git commit -m "Submit instructions..."

2. Restoration of Historical Versions

1. View submission history

git log
commit c7ab70f58f9f2f3cd095a8355dfef2a57d74cb8b
Author: XXX <XXX@xxx.com>
Date:   Sat Aug 14 10:28:15 2021 +0800

    Update Makefile

commit ac1055a582e120ee1a8da255eceab2a28e8ebd17
Author: XXX <XXX@xxx.com>
Date:   Fri Aug 13 14:39:59 2021 +0800

    first commit

commit e5e4b3da68784e47311910dfcdf4c50ac5ded94e
Author: XXX <XXX@xxx.com>
Date:   Tue Jun 8 10:12:20 2021 +0800

    Initial commit

2. reset and revert restored versions

  • reset---- Return version
git reset ac1055a582e120ee1a8da255eceab2a28e8ebd17

git log view history

commit e5e4b3da68784e47311910dfcdf4c50ac5ded94e
Author: XXX <XXX@xxx.com>
Date:   Tue Jun 8 10:12:20 2021 +0800

    Initial commit

Only the first record remains

  • revert - Redo Historical Version
git revert ac1055a582e120ee1a8da255eceab2a28e8ebd17

git log view history

commit 671a232b2097942832a05ede8616e217a6c08671
Author: XXX <XXX@xxx.com>
Date:   Sat Aug 14 10:29:55 2021 +0800

    Revert "first commit"
    
    This reverts commit ac1055a582e120ee1a8da255eceab2a28e8ebd17.
    
    recovery

commit c7ab70f58f9f2f3cd095a8355dfef2a57d74cb8b
Author: XXX <XXX@xxx.com>
Date:   Sat Aug 14 10:28:15 2021 +0800

    Update Makefile

commit ac1055a582e120ee1a8da255eceab2a28e8ebd17
Author: XXX <XXX@xxx.com>
Date:   Fri Aug 13 14:39:59 2021 +0800

    first commit

commit e5e4b3da68784e47311910dfcdf4c50ac5ded94e
Author: XXX <XXX@xxx.com>
Date:   Tue Jun 8 10:12:20 2021 +0800

    Initial commit

History will continue

3. Branch Processing

  • Local Branch Management

1. View Branches

$ git branch
* master

1. Create Branches

$ git branch dev
$ git branch
  dev
* master

2. Switching Branches

$ git checkout dev 
Switched to branch 'dev'
$ git branch
* dev
  master

Create and switch together

$ git checkout -b dev
Switched to a new branch 'dev'
$ git branch 
* dev
  master

3. Branch Merge

  • Switch to the dev branch and create the test.txt file
  • Add the test.txt file to the staging area
     git add test.txt 
  • Submit staging area files
    git commit -m "add test.txt"
  • Switch back to the master branch, use ls to view the file, find that there is no test.txt file, switch to the dev branch, use ls to view the file, test.txt exists. Create a new file in the current branch and submit, this file only exists in the current branch, if git add is not submitted, switch to another branch this file will be carried over.

  • Switch to master branch and use git merge to merge dev branch into master branch
    $ git merge dev
    
    ".git/MERGE_MSG" 9L, 272C written                                                                                                                                                                                      
    Merge made by the 'recursive' strategy.
     test.txt | 0
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100755 test.txt
    Looking at the file using ls, we found a test.txt file under the master branch

  • Remote Branch Management

1. Add a remote warehouse and name it origin

$ git remote add origin http://192.168.1.15/XXX/test.git

2. View remote warehouses

$ git remote 
origin

3. Switch to a branch that needs to be submitted locally

# git checkout dev

4. Submit to remote branch dev

git push Remote warehouse name Remote warehouse branch

$ git push origin dev

If an error occurs, bind the local dev branch to the remote dev branch

$ git branch --set-upstream-to=origin/dev dev 
Branch dev set up to track remote branch dev from origin

Recommended tutorials: Git Tutorial - Liao Xuefeng's official website

Tags: git

Posted on Mon, 20 Sep 2021 11:56:30 -0400 by Headache