Welcome everyone!
When I first started using Git, I have to admit, I was a bit intimidated. I had heard so much about it and knew it was an essential tool for any developer, but I didn’t know where to begin. I struggled with understanding the basic commands and couldn’t seem to wrap my head around the whole concept of version control.
I started by going through online tutorials, reading documentation, and experimenting with different commands. I would spend hours trying to understand the different branches, commits, and merge conflicts, but I was determined to make it work. And eventually, with a lot of practice and patience, it clicked.
I hope that by sharing my experience, I can help others who may be struggling with Git at the beginning. I know it can be overwhelming, but trust me, it’s worth it. With the right mindset, patience and resources, you too can master Git and improve your workflow.
This guide is designed for both beginners and advanced users who want to master the art of Git and improve their workflow. Whether you are a new developer just starting out with version control or an experienced professional looking to optimize your Git skills, this guide has something for everyone. We will cover essential Git commands, advanced techniques, and helpful hacks that will help you work more efficiently and effectively with Git. So let’s dive in and discover the full power of Git!
git status
git status --short
git status --porcelain
The --porcelain
option causes git status to display the status in a machine-readable format, which is intended to be easy to parse by scripts and other automated tools.
git log
git log --oneline
git log -n
git log --oneline -n
git log --patch
git log --stat
stats
: how many files were changed, and how many lines in those files were added and removed. It also puts a summary of the information at the end.
git log --shortstat
git log --graph
git log --no-merges
git log --pretty=reference
git log --pretty=short -u -L <start line>,<end line>:<file path>
code
within Git history
git log -S "CodeSnippet or searchString"
commit message
within Git history
git log --grep=<pattern>
git commit -m "your message"
git commit -m "Headline" -m "details"
git commit --reuse-message=<commit-sha>
// or
git commit -C <commit-sha>
git commit --reedit-message=<commit-sha>
// or
git commit -c <commit-sha>
git commit --ammend
git commit --ammend --no-edit
git branch --show-current
git branch
// or
git branch --list
git branch --remotes
git branch --verbose
// or
git branch -v
git branch --merged
git branch -b "branch name"
git branch -m "the renamed branch"
git branch --delete "branch name
git branch --delete --force "branch name"
// or
git branch -D "branch name"
git push origin --delete "branch name"
git checkout "branch name"
git checkout -
#### You can use also use git switch (git 2.23 and onwards)
git switch --create "branch name"
git switch "branch name"
git switch -
Git supports two types of tag: 1. lightweight tag, 2. annotated tag
git tag "tag name"
git tag --annotate "tag name"
// or
git tag -a "tag name"
git tag --annotate "tag name" <commit-sha>
git push origin 'tag name'
git push origin --tags
git tag --list
// or
git tag
git ls-remote --tags origin
git stash
git stash push -m "your message"
git stash --include-untracked
// or
git stash -u
git stash --all
git stash list
git stash pop
git stash pop <stash-head>
git stash apply <stash-head>
git stash drop <stash-head>
git stash clear
git stash show <stash-head>
git stash show --patch <stash-head>
git stash branch "branch-name" <stash-head>
(usable when reapplying stash might create conflict and you want to test you stash changes or make new branch)
git reset HEAD~1
git reset HEAD~n
--soft
: does not reset the index or working tree files--hard
: reset the index + working tree files--mixed
: reset the index but not working tree filesgit reset origin/<branch-name>
git reset --merge HEAD~1
git reset --merge
// or
git merge --abort
git revert <commit-sha>
git revert --edit <commit-sha>
git revert --no-edit <commit-sha>
git revert --no-commit <commit-sha>
git revert first-bad-commit^..last-bad-commit
git diff > fileName.patch
git diff --staged > fileName.patch
// or
git diff --cached > fileName.patch
git apply fileName.patch
git format-patch -n <sha>
git am < file.patch
git rebase <parent-branch>
git rebase --interactive <commit-sha>^
(here ^(caret) means parent to that commit)
git rebase --continue
git rebase --abort
git commit --fixup=<commit-sha>
git rebase --interactive --autosquash <commit-sha>
git bisect start
git bisect good sha-of-good-commit
git bisect bad sha-of-bad-commit
If bad commit is not provided, default is the last commit. Then Test your application and provide info about which state is good and which sate is bad.
git bisect good
git bisect bad
git bisect reset
git reflog
reference logs expiry time: default is 90 days
git config gc.reflogExpire <time>
// set for 90 days
git config gc.reflogExpire 90.days
// set never
git config gc.reflogExpire never
git config --get gc.reflogExpire
git config --unset gc.reflogExpire
gitk --all `git reflog | cut -c1-7`
git reset --hard HEAD~1
)
To get back on previous state
git reset --hard HEAD@{1}
git reflog
git cherry-pick <commit-sha | @HEAD{number}>
git commit --reuse-message=<commit-sha | @HEAD{number}>
// or
git commit -C <commit-sha | @HEAD{number}>
git commit --reedit-message=<commit-sha | @HEAD{number}>
// or
git commit -c <commit-sha | @HEAD{number}>
git reflog
git checkout -b "branch-name" <commit-sha | @HEAD{number}>
⚠️ Ensure that you are doing what you are supposed to.
git reflog expire
git reflog delete
This command causes data loss (use this at you own risk :bomb:)
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.