Useful Git commands to get things done and some advice about using Git
Git is awesome. Version control is super useful as you can make changes, check who did them, revert them and much more. If you are a developer, there is no reason not to use version control. Powerful services like GitHub, Bitbucket and GitLab allow you to host your code on the cloud with version control. There are other version control systems like Team Foundation Server (TFS) and Subversion (SVN) but none of them can match the incredible popularity of Git.
Git is actually pretty simple to use and there are a lot of GUI tools which make things easier. Popular IDEs like IntelliJ and editors often include a graphical user interface for version control by default or by the use of plugins. However, if you are a beginner, you should rather learn to do it with the command line. Then, you will get a better understanding of how version control with Git works.
The following is not a full guide how to use Git: there are lots of free and great tutorials available (e.g. by Atlassian, the company behind Bitbucket). Instead, this is a personal Git cheat sheet with useful commands I often or occasionally need in software development projects.
Check git status (e.g. to see which files you changed)
Abort a commit (local changes are not lost)
git reset −−soft HEAD^
Reset local repository and apply newest version from remote repository
- git fetch −−all
- git reset −−hard origin/branch_name
Show all branches (local and remote)
git branch -av
Delete local branch (remote branch will not be affected)
git branch -d branch_name
Apply changes from develop branch to feature branch (Git flow)
- git checkout feature/cool-feature
- git merge develop
Apply one commit from develop branch to feature branch without conflicts
- git checkout feature
- git cherry-pick -x your_commit_SHA1
Show git commit logs (history)
Track local branch with remote branch (when you have the issue that Git cannot match your local branch with the remote branch)
git branch -u origin/branch_name
Some basic reminders how to use Git properly
- Use purposeful commit messages: “Fixed bug” is not really saying much. Which bug? How can I reproduce this? Anyone who stumbles over this commit should not have to wonder what this commit does. Therefore, you should aim to formulate proper commit messages. If you use a ticket system like Jira, you can include the ticket number so commits can be matched to stories or bugs in your ticket system.
- Prefer small commits: this may be personal preference but I like to keep my commits short and purposeful. Basically, a commit should ideally do one thing (e.g. implement one feature). Do not implement multiple features and commit them together as this makes commits harder to understand. By keeping your commits small (but functional), one can better understand what this commit actually does.
- Do not push sensible data: sensible data like user credentials should not be pushed into a repository! If you commit it once, then it will be available in the history.
- Do not push unnecessary files to the repository: some files or folders do not need to be pushed. Examples: some editor related files or node_modules folder when using NPM package manager. Create a .gitignore file and specify which files and folders should not be tracked by Git. As long as these files have not been pushed prior, Git will ignore files and folders specified in .gitignore.
Thank you for reading this article about useful commands and tips for Git. As you have seen, Git is a powerful tool which immensely facilitates collaboration in software development projects. Do you know any other useful tricks when it comes to Git? I plan to update this article whenever I learn of new useful tricks so let me know in the comments if you have further suggestions.