My git cheatsheet
I had created this cheatsheet while started learning with git. Sharing this now, for the new learners out there.
Git has become a basic skill in the software industry nowadays. Here’s my cheat sheet about the most used commends and how I use git.
If you’re starting out to learn git, I would heavily recommend the blogs and getting your hands dirty with a small project.
https://rogerdudler.github.io/git-guide/
https://git-scm.com/book/en/v2
To branch out from master:
git checkout master && git checkout -b <branch-name>
To pull a branch
git checkout master && git fetch && git checkout <branch-name>
To stage files
git add <files/folder/ . >
Check the diff between master and the branch with
git diff <branch1> <branch2>
Confirm what files have changed
git status
To commit
git commit -a -m "<commit message>"
To push to the remote
git push origin <branch name>
— Github have made these simple nowadays
To squash merge and push
Considering abc1234 is the branch you are working on.
1) git checkout master
2) git pull origin master (take latest master)
3) git checkout abc1234 && git merge master
Resolve all the merge conflicts if any
4) git checkout master && git merge --squash <branch-name>
5) git commit -a -m "abc1234: Some awesome feature"
6) git push origin master
This way even if you have multiple commits in your private branch, on the master branch this will all go as one single commit.
For the commit messages:
We use the convention of "<branch-name>: <one-line description of the changes>"
This is a small effort to save lots of time later in finding the details about the changes you made.
Additional tip:
Add your branch name as the JIRA ticket, clickUp ID or any project management tool’s identifier for the change so you can easily go to the complete context of the change.
To reset files back as if nothing changed from the remote branch
To overwrite your local files just like a remote branch
git fetch --all
git reset --hard <remote>/<branch_name>
For example, to reset with master branch:
git fetch --all
git reset --hard origin/master
How it works:
git fetch downloads the latest from remote without trying to merge or rebase anything.
Then the git reset resets the master branch to what you just fetched. The --hard option changes all the files in your working tree to match the files in origin/master.
Resolve conflicts beforehand of merging to master
git checkout master
git fetch
git pull origin master
git checkout <Other-branch-name>
git merge master
On conflicts
Open some merge tools or a code editor and resolve conflicts, then add and commit changes. When it comes to your changes you would what changes to exclude and include. But when in doubt, always include the recent master changes. I use VSCode to resolve merge conflicts.
If you successfully resolve a commit.
git add <conflicted file names>
git commit -m "merging to master"
git merge master
If the merging failed, you mixed a few things. To simply throw away the merge changes
git merge --abort
But why? Why resolve conflicts before merging. Can’t I do it directly while merging to master?
Yes, you can but there are a few benefits of doing beforehand.
In case you've made a mistake and committed it, you can easily undo this with confidence when you are resolving conflicts in your feature branch than the master. So it’s very important to resolve conflicts wrt master before merging.
Usually, the conflicts could be due to some new changes from a different person in your team, so it helps get a review with the other merge before damaging things on master.
If you have screwed your branch, all you have to is
git reset --hard
and start over again.
Create a local branch and upstream it
Reference: https://stackoverflow.com/a/6824219/3967709
First, you must create your branch locally
git checkout -b your_branch
After that, you can work locally in your branch, when you are ready to share the branch, push it. The next command pushes the branch to the remote repository and tracks it.
git push -u origin your_branch
Delete remote Branch
git push -d <remote_name> <branch_name>
git branch -d <branch_name>
Note that in most cases the remote name is the origin.
Delete Local Branch
To delete the local branch use one of the following:
git branch -d branch_name
git branch -D branch_name
Note: The -d option is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch. You could also use -D, which is an alias for --delete --force, which deletes the branch "irrespective of its merged status."
To revert back to a recent commit :
git diff recent_commit older_commit > abc.diff
git apply abc.diff
Or
git diff recent_commit older_commit | git apply
Tip for caution: Don’t remove commits where database migration files are involved. Even if migration files are to be unchanged. Do not touch the migration file that is in the master branch.
To revert a particular commit :
Clean way, reverting but keep in the log the revert:
git revert --strategy resolve <commit>
Reference: https://stackoverflow.com/questions/2938301/remove-specific-commit/11992215#11992215
Create a HotFix Branch:
In Fyle, we follow the git-flow way of creating emergency branches with particular commits added or removed.
git pull
Find the current version on production (versions to the deploy bot), say it’s vA.B.C, service_name being the repo in which you are creating this.
git checkout tags/service_name-vA.B.C
Add 1 sub-version with C and now let’s call it D. For example: if the version in prod is 2.123.4 . Then A.B.C is 2.123.4 and A.B.D is 2.123.5 .
git checkout -b bA.B.D
<commit-id> is the one which you want to add after this.
git cherry-pick <commit_id>
git push origin bA.B.C:bA.B.C
Pro-tip for pull requests: Keep the Pull Requests smaller so to help the reviewer and yourself. It is much easier to do a code review on 10 lines of code, over a couple of 100 lines change. It is easier for you to write a code when the scope is much reduced.
PS: This document is not exactly how you can play around with git. There is a world of possibilities in git through which we can achieve the same desired result.