Git Cheatsheet

  1. Git global setup
git config --global user.name "openHacking"
git config --global user.email "openHacking@126.com"
  1. Create a new repository
git clone https://github.com/openHacking/xhack.git
cd xhack
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
  1. Push an existing folder
cd xhack
git init
git remote add origin https://github.com/openHacking/xhack.git
git add .
git commit -m "Initial commit"
git push -u origin master
  1. Set up local account to push an existing folder
git init
git config --local user.name "openHacking"
git config --local user.email "openHacking@126.com"
git add .
git commit -m "init"
git branch -M main
git remote add origin https://openHacking@github.com/openHacking/xhack.git
git config --local credential.helper store
git push -u origin main
  1. Push an existing Git repository
cd xhack
git remote rename origin old-origin
git remote add origin https://github.com/openHacking/xhack.git
git push -u origin --all
git push -u origin --tags
  1. Edit origin url
git remote set-url origin https://openHacking@github.com/openHacking/xhack.git
  1. Delete all commit history in git

Deleting the .git folder can cause problems in the git repository. If you want to delete all commit history, but keep the code in its current state, you can do it safely as follows:

# Try running
git checkout --orphan latest_branch

# add all files
git add -A

# Commit changes
git commit -am "commit message"

# delete branch
git branch -D master

# Rename the current branch
git branch -m master

# Finally, force an update of the repository
git push -f origin master
  1. Only push tag
# check latest tag
git describe

# generate new tag
git tag -a release-v0.0.0 -m "msg"

# push tag
git push origin release-v0.0.0
  1. Push commit with tag
git add .
git commit -m "msg"
git pull
git describe
git tag -a release-v0.0.0 -m "msg"
git push --follow-tags
  1. git undo commit
git reset HEAD .
  1. new branch

For example, create a new dev branch

git checkout -b dev
  1. merge branch

How to merge dev branch to main branch?

Submit the code of your own dev branch before merging

# 1. Enter the branch to be merged (if the dev branch is merged into main, enter the main directory)
git checkout main
git pull

# 2. Check whether all branches have been pulled down
git branch -a

# 3. Use merge to merge dev branch
git merge dev

# 4. View the status after the merger
git status

# 5. If there is a conflict, resolve the conflict through the IDE

# 6. After resolving the conflict, submit the conflict file to the staging area
git add .

# 7. The result after submitting the merge
git commit -m "fix conflict"

# 8. Submit the local warehouse code to the remote warehouse
git push
  1. .gitignore not working

Commit all changes before doing this

git rm -rf --cached .
git add .
git commit -m ".gitignore is now working"
git pull
git push
  1. Count the number of lines of code

Use ls-files to count the number of lines of code in the tracked files in the git repository

# 1. The number of all code lines in the current repository, including resource files
git ls-files | xargs wc -l

# 2. Count the specified directory, support wildcards
git ls-files src/utils src/controller src/*.css | xargs wc -l

# 3. Exclude certain files or folders
git ls-files | grep -Ev 'lib|.png|.ttf' | xargs wc -l
  1. git new branch and commit

View local branch

git branch

View all remote branches

git branch -r

view all branches

git branch -a

Update all remote branch information

git fetch origin

Only create new branches

git branch dev

switch to branch only

git checkout dev

Create and switch branches

git checkout -b dev

Establish a mapping relationship between local branches and remote branches

git checkout -b local [branch_name] origin/[remote_branch_name]

delete local branch

git branch -d dev

Commit push branch

git add .
git commit -m "new"
git push origin dev

Delete a remote branch (a colon in front of the branch name means delete)

git push origin :dev

clone branch code

git clone -b [branch_name] [repository_address]
  1. git commit specification
feat: new features
fix: modify the problem
refactor: code refactoring
docs: Documentation modification
style: code format modification, note that it is not css modification
test: test case modification
chore: Other modifications, such as build process, dependency management.
pref: Modifications for performance improvement
build: changes to the project build or dependencies
ci: modification of CI
revert: revert previous commit
  1. Delete a commit record Will not affect subsequent commits
1. git log # Get commit information
2. git rebase -i [commit-id] # commit-id is a commit before the commit to be deleted
3. Edit the file, change the word pick before the commit to be deleted to drop, save the file and exit
4. git log # Check whether the record has been deleted
5. git push origin HEAD --force # Force push to remote warehouse
6. Go to the webpage to check the submission record