Here is a collection of useful Git commands that I use and that could be helpful to you when working with Git in your daily work.
Git Reference
A git reference can be found here.
Setup
This is the name that will be associated when a commit is made in the version history
git config --global user.name “<firstname lastname>”
Set an email address which will be associated with your user
git config --global user.email “<valid-email>”
Add Git
git init
git add .
git status
git remote add origin "URL"
git commit -m "Initial commit"
git status
git push -u origin master
Commit
git status
git add .
git commit -m "COMMIT MESSAGE"
git status
git push -u origin master
Commit with Body and Subject
git commit -m "this is the subject" -m "this is the body"
Clone
git clone <URL>
Pull
Fetch and merge any commits from the tracking remote branch
git pull
Branch
Check which branch is checked out
git push -u origin <branch>
Create new feature branch
git checkout -b feature/test
Force create new branch
git checkout -b feature/test
Push local branch
git push -u origin <branch>
Checkout branch
git checkout <branch name>
Log
Show all commits in the current branch’s history
git log
Show all commits in the current branch’s history in one line
git log --oneline
Tree in Terminal
git log --graph --oneline --all
Diff
git diff
NOTE: a good tip is that you can always use the following buttons to exit or git help in the git diff
and git log
screen for example:
Type q to exit the screen. Type h to get help.
Reset to commit
git reset --hard <commit id>
Stop track a folder or file
git rm --cached <file>/<folder>
git commit -m "Removed file that shouldn't be tracked"
Stash
Save un-committed changes
git stash
List Stashes
git stash list
Delete all stashes
git stash clear
Drop specific Stash
git stash drop stash@<stash id>
View Stash changes
git stash show
View Stash full diff
git stash show -p
Merge the specified branch into the current one
git merge <branch>
Rebase
Apply any commits of current branch ahead of specified one
git rebase <branch>
Clear staging area, rewrite working tree from specified commit
git reset --hard <commit>
Cherry Pick
Run git log --oneline
to get a log of your commits history. The commit hash is what we need to start the cherry picking.
Checkout the branch where you want to cherry pick the specific commit/commits
.
Now we can cherry pick from one branch into our master branch
.
To cherry pick one commit - this will cherry pick the commit with the selected and add it as a new commit on the master branch. It will have a new (and different) commit ID in the master branch than the branch it was originally pushed into.
git cherry-pick <commit-hash>
To cherry pick more than one commit
git cherry-pick <commit-hash> <commit-hash>
If the cherry picking gets halted because of conflicts, resolve them by
git cherry-pick --continue
If you want to abort this step
git cherry-pick --abort
You can also cherry pick a merge
git cherry-pick -m 1 <hash>
Top comments (0)