Set git user information$ git config --global user.name "myusername"
$ git config --global user.email "admin@unixutils.com"
View git user configuration.$ git config --list
Initialize/Create a new repository$ git init
Show new/modified/staged files$ git status
Shows what changed in file(s)$ git diff
$ git diff myfirstfile.txt
Shows changes between two commitsgit diff
Add files to staging$ git add myfirstfile.txt
Commit files$ git commit -m "Add new test file to repo"
Shows all commits made to repository$ git log
Shows all commits from a specific author$ git log --author="author or email"
Shows all commits but list commit IDs only$ git log --oneline
Shows all commits with branches graphically$ git log --graph
Shows git log with custom output (Credits: Cameron McKenzie on youtube)
$ git log --graph --pretty="%C(yellow) HASH: %h %C(blue)Date: %ad %C(red) Message: %s "
Push commits to remote repo$ git push
Create new git branch$ git branch testbranch
Switch to a different branch$ git checkout testbranch
Create a new branch and checkout in same commandgit checkout -b testbranch
Push commit to specific branch$ git push origin testbranch
(origin being the name of repository and testbranch being the name of the branch)
Pull from a specific branch and merge the commits to main branch. If the testbranch is a localbranch, git pull can be skipped.$ git pull origin testbranch
(origin being the name of repository and testbranch being the name of the branch)
$ git merge testbranch -m "commit message for merge"
$ git commit
$ git push
Fetch changes but do not merge the changes$ git fetch origin master
(origin being the name of repository and testbranch being the name of the branch)
Pick a commit from a branch and applying it to another$ git cherry-pick
As an alternative to branch merge, one can rebase and get latest commits$ git checkout
(Gets all latest commits from master into featurebranch
$ git rebase master$ git pull --rebase
(performs a fetch + rebase)
Reset commits in git. Not suitable to run in remote repo, since history of commits are lost when reset is done.
soft reset does not delete the files of newer commits when it rolls back commits to old commit$ git reset --soft
hard reset does a rollback to specified old commit and also destroys files associated new commit.$ git reset --hard
A new commit to revert the commits to the previous one$ git revert HEAD
A new commit to revert the commits to a specific commit$ git revert
List all branches$ git branch -a
Stash changes modified files$ git stash
Stash all files, including untracked ones$ git stash -a
List stashed changes$ git stash list
Restore stashed changes$ git stash apply
(restores stash but does not clear stash)
Delete stashed changes$ git stash drop
(clears stash)
Restore stashed changes and throw away stash$ git stash pop
Git stash with a message. Works only on tracked files.$ git stash save "some stash message"
Restore stash from list of stashes using index number, example, 1 being the index$ git stash apply stash@{1}
in latest versions: $ git stash apply 1
OR$ git stash pop stash@{1}
See changes in a specific stash$ git stash show stash@{1}
Rename branch$ git branch -m <old branch name> <new branch name>
Delete branch (can be done only after switching to a different branch)$ git branch -d <branch name>