Git Command - Cheatsheet
pinned
- accidentally push unwanted files and want to remove from git history
- revert after rebase/merge
- undo commit as new commit
- compare between two commits
common
- set email and name
- clone
- push
- push existing local repo
- show git commit version
- reset to online repo origin
- delete file and folder
- reset local credential
- resolving issue "fatal: refusing to merge unrelated histories" - 1
- accidentally commit unwanted files
- credential with GITHUB_ACCESS_TOKEN
-
set email
git config --global user.email "your_email@gmail.com" -
set username
git config --global user.name "your_username"
-
clone online repo
git clone https://github.com/username/repo_name.git
-
go to repo directory
cd `repo_name`
- git add --all
- git commit -m "
messages" - git push
-
create branch
git checkout -b master
-
add to git
git add . -
commit
git commit -m "`messages`" -
adding remote repository
git remote add origin `https://github.com/username/repo_name.git` -
show remote rpository
git remote -v
-
push commit to repository
git push -u origin master -f
- git log
- git log --abbrev-commit
the safest way without losing commits :)
>git log --abbrev-committo show the commit versions
- git revert --no-commit
commit_version..HEAD
- git fetch origin
- git reset --hard origin/master
-
reflog
git reflog --pretty=short --date=iso
-
reset to HEAD target (find ur target commit after
git reflog)git reset --hard HEAD@{2}or
git reset --hard "HEAD@{2}"
- git rm
./folder/file.jsx
- git rm
./folder/folder-r
git config --local credential.helper ""bypass husky precommit & prepush (AT YOUR OWN RISK: if you need to push do it ASAP without getting stuck waiting for husky rules haha)
git commit -m "message" --no-verifygit push --no-verifyIf you encounter the "fatal: refusing to merge unrelated histories" error even when attempting to pull changes from the remote repository, you can try the following steps to resolve the issue:
-
Fetch the remote repository's changes without merging them:
git fetch --all
-
Create an empty commit to establish a common root for the branches:
git commit --allow-empty -m "Empty commit" -
Merge the remote branch into your new branch using the --allow-unrelated-histories flag:
git merge origin/main --allow-unrelated-histories
-
Resolve any merge conflicts if they occur. Use a text editor or Git tools to manually resolve conflicts in affected files.
-
Commit the changes
git commit -m "Merge unrelated histories" -
Push the changes to the remote repository:
git push origin your-branch
git reset --soft HEAD^example
.envfile
- add filename to
.gitignorefileoptional : if the file may exist under certain conditions, and want to prevent it from being tracked to git
- untrack and remove file from commit
git rm -r --cached .env
- commit changes
git add . && git commit -m "remove unwanted files"
- remove file from all git commit history
all branches version
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch .env" HEADgit filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch .env" -- --all - push to origin to implement removed file to remote git history
git push --force
- clear the backup
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
access url
https://github.com/USERNAME/REPO_NAME/compare/SHORT_HASH..SHORT_HASH
Caution
not to be imitated at zero risk of tolerance
- access Settings > Developer Settings > Fine-grained tokens
- hit
Generate new token - fill the
Token name, and setExpiration - set
Repository accesstoOnly select repositories, then select target repo - add permissions repositories with hit
+ Add permissions, find end check atContents, and keepContentaccess withread-only - hit the
Generate token(it will popping up confirmation info, justGenerate token), copy the generated token, ex:github_pat_XXXXXXLOOONG_TEXT_NO_SPACE - implement access token to clone like
git clone https://<GITHUB_ACCESS_TOKEN>@github.com/<USERNAME>/<REPOSITORY>.git