Skip to content

adhemukhlis/git-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 

Repository files navigation

git-cheatsheet

Git Command - Cheatsheet


table of content

pinned


common




set email and name

  1. set email

    git config --global user.email "your_email@gmail.com"
  2. set username

    git config --global user.name "your_username"


clone

  1. clone online repo

    git clone https://github.com/username/repo_name.git
  2. go to repo directory

    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
    all branches version
    git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch .env" -- --all
  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

compare between two commits

access url

https://github.com/USERNAME/REPO_NAME/compare/SHORT_HASH..SHORT_HASH

credential with GITHUB_ACCESS_TOKEN

Caution

not to be imitated at zero risk of tolerance

  1. access Settings > Developer Settings > Fine-grained tokens
  2. hit Generate new token
  3. fill the Token name, and set Expiration
  4. set Repository access to Only select repositories, then select target repo
  5. add permissions repositories with hit + Add permissions, find end check at Contents, and keep Content access with read-only
  6. hit the Generate token (it will popping up confirmation info, just Generate token), copy the generated token, ex: github_pat_XXXXXXLOOONG_TEXT_NO_SPACE
  7. implement access token to clone like git clone https://<GITHUB_ACCESS_TOKEN>@github.com/<USERNAME>/<REPOSITORY>.git

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors