Skip to content

mohsenshafiei/git-commands

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Git-Commands

Useful commands that we can use in our project

Add a file to the next commit
git add <file>
Delete a file in the next commit
git rm <file>
Rename a file in the next commit
git mv <file>
Stage commits in hunks interactively
git add -p
Save un-commited work
git stash
List changes
git stash list
Show contents of stash
git stash show stash@{0}
Apply the last stash
git stash apply
Apply a specific stash
git stash apply stash@{0}
Keep untracked files
git stash --include-untracked
Keep all files (even ignored ones!)
git stash --all
Name stashes for easy reference
git stash save 'WIP: making progress in foo'
Start a new branch from a stash
git stash branch <optional stash name>
Grab a single file from a stash
git checkout <stash name> -- <file name>
Remove the last stash and applying changes
git stash pop
Remove the last stash
git stash drop
Remove the nth stash
git stash drop stash@{n}
Remove all stashes
git stash clear
Lightweight tag
git tag <tag-name>
Annotated tag
git tag -a <tag-name> -m 'your message for tag'
List all the tags in a repo
git tag
List all tags, and what commit they are pointing to
git show-ref --tags
List all the tags pointing to a commit
git tag --points-at <commit>
Looking at the tag, or tagged content
git show <tag-name>
No Fast-Forward Merging
git merge --no-ff
Enabling ReReRe (Reuse Recorded Resolution)
git config rerere.enabled true

use --global flag to enable for all projects

What changes since yesterday
git log --since='yesterday'
git log --since='2 weeks ago'
Log files that have been moved or renamed
git log --name-status --follow -- <file>
Search for commit messages that match a regular expression
git log --grep <regexp>

can be mixed with other git flags

Show commit and contents
git show <commit>
Show file changed in commit
git show <commit> --stat
Look at a file from another commit
git show <commit>:<file>
un-staged changes
git diff
staged changes
git diff --staged
Which branches are merged with master, and can be cleaned up?
git branch --merged	master
Which branches are not merged with master yet?
git branch --no-merged master
Overwrite the working area file with the staging area version from the last commit
git checkout -- <file_path>
Checkout a file from a specific commit
git checkout <commit> -- <file-path>
Restored a deleted file
git checkout <deleting-commit>^ -- <file_path>
Clear your working area by deleting untracked files.
git clean
to see what would be deleted
git clean --dry-run
To see what files and directories be deleted
git clean -d --dry-run
To clean your working area by deleting untracked files and directories
git clean -d -f
Move head to the last commit in repository area
git reset --soft
Move head to the last commit in repository area and copy files to stage
git reset --mixed
Move head to the last commit in repository area and copy files to stage and Working area
git reset --hard
Undo a git reset with ORIG_HEAD
git reset ORIG_HEAD
The safe reset is revert
git revert <commit>
Make changes to the previous commit
git commit --amend
Interactive rebase with shortcut
git rebase -i <commit_to_fix>^
Rebase Options
  • pick
    • keep this commit
  • reword
    • keep the commit, just change the message
  • edit
    • keep the commit, but stop to edit more than the message
  • squash
    • combine this commit with the previous one. stop to edit the message
  • fixup
    • combine this commit with the previous one. keep the previous commit message
  • exec
    • run the command on this line after picking the previous commit
  • drop
    • remove the commit (tip: if you remove this line, the commit will be dropped too!)
If Rebasing goes wrong
git rebase --abort
Viewing Remotes
git remote -v
Add upstream remote
git remote add upstream <repository_path>
To checkout a remote branch, with tracking
git checkout -t origin/feature
Tell git which branch to track the first time your push
git push -u origin feature
pull down all changes but doesn't change your local
git fetch
pulling down changes and change local
git pull
Pull, Fetch Cheat
git pull = git fetch && git merge
To see commits which haven't been published upstream yet
git cherry -v
Push One Tag to the remote
git push <tag_name>
Push tags to remote
git push --tags
Danger Zone: Never use this
git push -f
use ORIG_HEAD to undo merges
git reset --merge ORIG_HEAD
to find out about changes to a file
git blame <filename>
use the diff-filter flag on log to see all commits where the file was deleted:
git log —diff-filter=D -- <deleted_file>
git blame using the parent commit
git blame <commit>^ -- <deleted_file>
Git blame by lines
git blame -L1,5 -- <file>
To find which commit introduced a bug
git bisect start <BAD_SHA> <GOOD_SHA>
Local setting per repository
git config <setting> <value>
Global setting apply to all repository
git config --global <setting> <value>
PR commit messages template

This pull request closes issue #number

You can add a .github/ folder for your project to specify templates for pull requests, issues, and contributing guidelines.

  • contributing.md
  • issue-template.md
  • pull-request-template.md