Skip to main content

git commands

  • See git logs with graph but in compact fashion,

    git log --oneline --graph -n 5

  • Hard reset last 3 commits

    git reset --hard HEAD~3

  • Revert committed revisions

    git revert --no-commit 03d013024..HEAD
    git revert -n 03d013024..HEAD
    03d013024 is the commit, needs to be present.

  • Delete Branch locally

    git branch -d <branch_name>

  • Git Fetch all Tags

    git fetch --all --tags --prune

  • List Tags

    git tag --list 'v*'
    git tag

  • Checkout based on tag

    git fetch --tags git checkout -b product-0.1234 v0.1.2502-46 git checkout v0.1.2502-46 -b prod-na-s135

  • Checkout remote branch

    git fetch origin git branch -a git checkout -b fix-failing-tests origin/fix-failing-tests

  • Copy One or two files/folders from one branch to other

    • Assuming you are in another branch than dev. Copy One or two files from one branch to other.

      git checkout dev -- path/to/your/file.

    • Copy folder from one branch to other.

      git checkout dev -- path/to/your/folder

    • Copy files and folder from commit hash of another branch.

      git checkout <commit_hash> <relative_path_to_file_or_dir>

  • Remove all commits before a commit, Hard reset a commit

    git reset --hard <prev-commit> git push origin HEAD --force

  • Rename Branch

    git branch -m <NEW_NAME>

  • Git Stash

    git stash save "add style to our site"
    git stash list
    git stash pop 'stash@2'
    git stash apply 'stash@0'

  • Apply specific stash

    git stash apply 'stash@1'

  • Enter new password for git
    To fix this on macOS, you can use. A username and password prompt will appear with your next Git action (pull, clone, push, etc.).

    git config --global credential.helper osxkeychain

    For Windows, it's the same command with a different argument:

    git config --global credential.helper wincred

  • Cherry pick with commit

    git cherry-pick 2a9c3ba

  • Cherry pick without commit

    git cherry-pick --no-commit 2a9c3ba
    git cherry-pick -n 2a9c3ba

  • Merge commit

    git rebase -i HEAD~2 (if last two commits)
    git push origin +branch

  • Merge master changes to branch

    git pull origin master

  • Check upstream branch

    git branch -vv

  • Show current branch

    git branch –show-current

  • Set upstream branch

    git push --set-upstream origin <branch>
    git push --set-upstream origin djadeja/TestBDD
    git branch -u origin/feature/feature-123