Skip to content

adhemukhlis/git-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 

Repository files navigation

git-cheatsheet

Git Command - Cheatsheet


table of content

pinned


common




set email and name

  1. git config --global user.email "your_email@gmail.com"
  2. git config --global user.name "your_username"

clone

  1. git clone https://github.com/username/repo_name.git
  2. cd repo_name

push

  1. git add --all
  2. git commit -m "messages"
  3. git push

push existing local repo

  1. create branch

    git checkout -b master
  2. add to git

    git add .
  3. commit

    git commit -m "`messages`"
  4. adding remote repository

    git remote add origin `https://github.com/username/repo_name.git`
  5. show remote rpository

    git remote -v
  6. push commit to repository

    git push -u origin master -f


show git commit version

by default with long hash

  1. git log

short hash version

  1. git log --abbrev-commit

undo commit as new commit

the safest way without losing commits :)
> git log --abbrev-commit to show the commit versions

  1. git revert --no-commit commit_version..HEAD

reset to online repo origin

  1. git fetch origin
  2. git reset --hard origin/master

revert after rebase or merge

  1. reflog

    git reflog --pretty=short --date=iso
  2. reset to HEAD target (find ur target commit after git reflog)

    git reset --hard HEAD@{2}

    or

    git reset --hard "HEAD@{2}"


delete file and folder

file

  1. git rm ./folder/file.jsx

folder

  1. git rm ./folder/folder -r

reset local credential

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:

  1. Fetch the remote repository's changes without merging them:

    git fetch --all
  2. Create an empty commit to establish a common root for the branches:

    git commit --allow-empty -m "Empty commit"
  3. Merge the remote branch into your new branch using the --allow-unrelated-histories flag:

    git merge origin/main --allow-unrelated-histories
  4. Resolve any merge conflicts if they occur. Use a text editor or Git tools to manually resolve conflicts in affected files.

  5. Commit the changes

    git commit -m "Merge unrelated histories"
  6. Push the changes to the remote repository:

    git push origin your-branch

accidentally commit unwanted files and want to back to unstaged area

git reset --soft HEAD^

accidentally push unwanted files and want to remove from git history

example .env file

  1. add filename to .gitignore file

    optional : if the file may exist under certain conditions, and want to prevent it from being tracked to git

  2. untrack and remove file from commit
    git rm -r --cached .env
  3. commit changes
    git add . && git commit -m "remove unwanted files"
  4. remove file from all git commit history
    git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch .env" HEAD
  5. push to origin to implement removed file to remote git history
    git push --force
  6. clear the backup
    git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published