git

Summary of 15 Useful Git Commands for Your Project

thangtd0101

thang td

Posted on April 9, 2024

Summary of 15 Useful Git Commands for Your Project

Introduction

Git is a distributed source code management software that helps manage changes and history of the source code.

Synthesis of 15 git commands

1. Git clone: git clone <remote_repo>

This command clones a repository from GitHub/GitLab/Bitbucket to your local machine.

For newcomers joining a project, this command is essential

2. Git init: git init

This command helps you initialize source code into a repository, and git will manage this source code.

3. Git fetch: git fetch

Used to retrieve files/branches added or changed in the remote repository to the local repository.

4. Git merge: git merge

Used to merge a branch into the current branch.

5. Git pull: git pull

Combination of git fetch and git merge.

Typically, developers use git fetch to check for added/changed files/branches before pulling.

6. Git branch: git branch <branch_name>

Create, delete, list, rename branches.

7. Git checkout: git checkout <branch_name>

Switch to another branch.

With git branch, you only create a new branch but won't automatically switch to it. Use git checkout -b <branch_name> to create and switch to a new branch.

8. Git status: git status

Check the status of the source code to see if any files are added/modified/deleted.

9. Git add: git add

Stage files.

Use git add . to add all files that have been added/modified/deleted.

10. Git log: git log

List all commits including commit hash, committer, commit date, and commit message.

Use git log --oneline to display only commit hash and commit message.

11. Git revert: git revert

Create a new commit with the commit you want to revert.

Useful when you've committed a file, but that file is faulty, missing, or you want to change a feature and need to revert that commit.

12. Git reset: git reset <commit_hash>

Similar to git revert but with git reset, you won't create a new commit. Instead, it removes new commits and reverts to the commit you want to reset.

git reset <commit_hash>: reset commit and keep files in the index (working space)

git reset --soft <commit_hash>: reset commit and keep files in the stage

git reset --hard <commit_hash>: reset commit and delete files that have been added/modified/deleted

13. Git rebase: git rebase

Similar to git merge, git rebase also helps merge branches, but instead of creating a new commit and inserting commit orders based on commit time, with git rebase, commits are brought to the top of the branch flow.

Git rebase has many useful features such as changing commit messages to other messages, merging commits.

14. Git stash: git stash

If you encounter a situation where you are developing on the current branch but need to switch to another branch, you won't be able to switch if there are changes to files in the current branch.

To switch to another branch, you need to commit these changes, but you don't want to commit yet because the feature isn't fully developed.

At this point, use git stash. It will stash your files, and you can comfortably switch to another branch. When you return to the old branch, you can apply the stash and continue developing.

15. Git tag: git tag <version>

For projects released in versions, tags are used to manage each version as well as the changelog of each version.

Conclusion

Finally, I have synthesized the 15 most commonly used Git commands. Additionally, you can explore more commands to apply to project development. Thank you for reading.

πŸ’– πŸ’ͺ πŸ™… 🚩
thangtd0101
thang td

Posted on April 9, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related