Git is a powerful version control system widely used by developers to manage their code repositories. Whether you’re a beginner or an experienced developer, understanding and utilizing Git commands effectively can significantly enhance your productivity and collaboration. In this blog post, we will explore some of the top useful Git commands that every developer should know.

- git init: The git init command initializes a new Git repository in your current directory. It sets up the necessary files and directories to start tracking changes to your project.
- git clone: To create a local copy of a remote repository, you can use the git clone command. It downloads the entire repository, including all branches and commit history, onto your machine.
- git add: Before committing changes to your repository, you need to stage them using the git add command. It allows you to specify which files or directories you want to include in the next commit.
- git commit: The git commit command records the changes you’ve staged in the repository’s history. It’s essential to provide a meaningful commit message that describes the changes made in the commit.
- git status: To check the status of your repository and see which files have been modified, added, or deleted, you can use the git status command. It helps you keep track of the changes before committing them.
- git branch: Git branches are useful for organizing and isolating different features or versions of your codebase. The git branch command allows you to create, list, delete, and switch branches.
- git merge: When you want to combine changes from one branch into another, you can use the git merge command. It integrates the specified branch’s commit history into the current branch.
- git pull: The git pull command fetches and merges changes from a remote repository into your current branch. It is typically used to update your local repository with the latest changes made by others.
- git push: To upload your local commits to a remote repository, you can use the git push command. It sends your changes to the remote repository, making them available to others.
- git log: The git log command displays the commit history of a repository. It shows information such as the commit hash, author, date, and commit message, allowing you to review the project’s history.
Conclusion: Git is an indispensable tool for developers, and mastering its commands can greatly improve your efficiency and collaboration. In this blog post, we covered some of the top useful Git commands, including initialization, cloning, staging, committing, branching, merging, and synchronizing changes. By understanding and utilizing these commands effectively, you’ll be well-equipped to manage and track your code repositories with Git. Happy coding!
Use case 1: Add new commit without changing message
git add [changed filed] // update your change
it commit --amend --no-edit // if u don't want to change a commit message.
git push --force-with-lease // overwrite previous message
Use case 2: Revert To A Specific Hash Commit
# Reset the index and working tree to the desired tree
# Ensure you have no uncommitted changes that you want to keep
git reset --hard 56e05fced
# Move the branch pointer back to the previous HEAD
git reset --soft "HEAD@{1}"
git commit -m "Revert to 56e05fced"
Git Command Line Best Practice at work
1. git add
2. git commit -m "[DE_1901025HSM-xxxxx]"
3. git commit --amend => be able to edit commit message.
4. git commit --amend --no-edit => if u don't want to change a commit message.
5. git pull
6. git pull --rebase (rebase your branch onto master)
7. Squashing:
7.1. git rebase -i HEAD~numOfCommits i.e. HEAD~3 (squash 3 commits)
7.2. pick - squash to commits.
7.3. Commit message.
7.4. git push --force
8. git push --force (to push operations that rewrite git history like --amend, --rebase)
9. git push
10. To remove commits: git reset --hard HEAD~numOfCommits i.e. HEAD~1 (remove 1 previous commit)
11. git restore . => to remove all un-staged changes.
12. git restore --staged files => to remove staged changes.
13. git clean -fd => remove untracked files.
14. Clean reflog
- git reflog expire --expire=now --all
- git fsck --unreachable
- git prune
- git gc --aggressive --prune=now
15. git checkout - => checkout the previous branch
Do's
1. Before opening a PR, we should check if your branch is way too behind the target branch then do a rebase.
2. Frequently update your dev branch with target branch by a rebase.
3. Resolve conflicts -> git pull (default: merge), don't use rebase.
4. Just from my experience.
4.1. When we are developing a feature, try to make meaningful commits (i.e. implement conditional check of global manipulation flag)
4.2. If you try out a completely new solution then don't amend, just make new commit so that you can easily come back if it doesn't work
5. Consider to squash to a minimum numOfCommits with meaningful message before opening a PR.
Don't's
1. Don't amend a commit that had a review comment.
2. Don't rebase if there is a review on your PR.
3. Only squashing after all approvals are granted.
