Git Command - Cheatsheet
pinned
- accidentally push unwanted files and want to remove from git history
- revert after rebase/merge
- undo commit as new commit
common
- set email and name
- clone and push
- 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
- git config --global user.email "
your_email@gmail.com
" - git config --global user.name "
your_username
"
- git clone
https://github.com/username/repo_name.git
- 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-commit
to 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-verify
git push --no-verify
If 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
.env
file
- add filename to
.gitignore
fileoptional : 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
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch .env" HEAD
- 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