banner
Zein

Zein

x_id

Best Practices for Git

image

git config#

git config --global user.name "xj"
git config --global user.email "[email protected]"
git config --global credential.helper store

View:
git config --global --list
git config --global user.name
git config --global user.email

Parameters: (Local)
--global Global configuration, effective for all repositories
--system System configuration, effective for all users

Create local Repository#

mkdir learn-git
cd learn-git
git init

git clone https://github.com/ekmett/ad.git
echo "remote-repo" >> [README.md](http://readme.md/)
git init
git add [README.md](http://readme.md/)
git commit -m "first commit"
____If a repository already exists, execute the following; otherwise, execute both above and below
git branch -M main                                   # Rename the current branch to main
git remote add origin [[email protected]](mailto:[email protected]):xj/repo.git     # Link to remote repository [email protected]:xj/repo.git; and name it origin
git push -u origin main:main                         # Push the local "main" branch to the remote repository's main branch

git remote -v                                        # View the currently linked repositories
git remote set-url origin <new_path>                 # Reset the repository address
git remote remove origin                             # Delete the repository

gitflow#

The main branch: only accepts merges from hotfix/release, direct push modifications are not allowed; each merge generates a version number x.x.x (major version, minor version, patch version)
hotfix: branches off from main, merges back to main after fixing
dev: branches off from main/hotfix, merges to release after development is complete
release: branches off from dev, merges to main after testing is complete
feature-xx: branches off from dev, merges to dev after development is complete

image

Each time a bug is fixed, add the modification record to the staging area, and finally commit it to the local repository in one go, recorded as a version information
Can be understood as 3 directories (not actually): the local repository is the release directory

image

git status View file status
git log View commit version records
git log —oneline

git diff    View differences between the staging area and working directory
git diff HEAD   View differences between the working directory and current version
git diff -- cache   View differences between the staging area and current version
git diff 59cf93 59cf712 Compare differences between two versions
or git diff HEAD~1 HEAD file3.txt Compare differences of file3 between two versions

git ls-files View the staging area

Commit#

**git add** fileName
git add .                            // Track all untracked files in the current directory

**git commit filename -m "This is the update description"
git commit** -am "Description"          Staging + Commit

git push                            Use this command after ssh linking;
git push remote_repo xvjin_dev

git checkout --   Revert to the most recent commit state

Delete#

git rm file.txt                       Use ls and git ls-files to find that it has been deleted from both the working directory and the staging area
git commit -m "Delete"

git clean -f                             Forcefully clear untracked files

git rm -r dir Recursively delete everything under a directory
git rm --cached file.txt Remove from the cache

Update#

**git pull** origin main                                                        Pull and merge the main branch of the origin repository
git fetch origin main                                                       Pull

git pull mylibrary main --allow-unrelated-histories    The first time pulling a remote branch to local may require

Branch Management#

git branch xvjin-dev         Create a branch
git switch xvjin-dev          Switch branches
git checkout xvjin-dev          Switch branches (old)

git merge xvjin-dev          Merge xvjin-dev into the current branch, and the current branch will have an additional commit record; xvjin-dev still exists after merging, deletion must be done manually
(dev)>>git rebase main    Rebase the current dev branch onto the target branch main; this kind of merge forms a linear history, which is cleaner

git branch -d xvjin-dev     Delete the branch after merging
git branch -D xvjin-dev     Force delete an unmerged branch

git branch -a                             View all branches
git remote show origin           View origin repository update records
git log                                    View local repository update records

Merge Conflicts#

1)git diff          View differences
2)Manually modify the files that failed to merge
3)Submitting the modified files will automatically complete the merging of conflict files

git merge --abort   Abort the merge

Rollback to a certain version#

git reset --soft HEAD^                                       Roll back to a certain log version in the repository, keeping the working directory and staging area content
git reset --hard HEAD~1                                           Roll back to a certain log version in the repository, not keeping the working directory and staging area content
git reset --mixed 59cf9334cf957                               Roll back to a certain log version in the repository, keeping the working directory, not keeping the staging area content

git log --oneline --grahp --decorate --all                Graphically view version number hashes, used for the above rollback

git reflog View historical operations

git checkout -- filename Discard modifications in the working directory, using cache data to overwrite the current working directory

ssh Configuration#

OpenSSH Core Operations | GitHub SSH Connection_哔哩哔哩_bilibili

Create a local repository by pulling from remote based on ssh protocol; or when pushing to a remote repository: configure ssh keys

1)Specify to generate a 4096-bit rsa (or ed25519) type key id.rsa in ./.ssh, press enter to name the key file; the default key file is id_rsa; a public key file id_rsa.pub will also be generated

ssh-keygen -t rsa -b 4096 -f ./.ssh/id.rsa
ssh-keygen -t ed25519 -C "<Label: usually write github email>"

2)Then: Find and copy the public key from the .ssh directory to the remote repository settings; finally: configure the .ssh/config file (on Windows at C:\Users.ssh) (Linux at)
vi ./config:

IdentityFile ~/.ssh/private_key_file          # Automatically read the private key pair from the private key path when ssh is requested

#github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id.rsa

This means that when connecting to the host github.com, the preferred authentication method is public key authentication, and the local private key path is ~/.ssh/id.rsa
3)Test

ssh -T git@host (the above host)          // Test

Git LFS#

git lfs version    # View

After installation, initialize Git LFS in your Git repository:

git lfs install

Use Git LFS to track large files. Suppose you want to track all PDF files, you can use the following command:

git lfs track "*.pdf"

Add and commit changes
Next, you need to add the changes to your Git repository and commit:

git add .gitattributes
git add <large_files>  # Those large files you want to track
git commit -m "Track large PDF files with Git LFS"

.gitignore#

Ignore certain files when committing to the repository: Note that sensitive information files involving identity, passwords, tokens, keys, etc. must not be committed

echo *.o > .gitignore

image

image

License#

https://cloud.tencent.com/developer/article/1921909

image

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.