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. 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