- vừa được xem lúc

Maybe you missed these 12 awesome Git commands

0 0 16

Người đăng: Anhkolamgidauanhthe

Theo Viblo Asia

In today's development landscape, Git has become an indispensable tool for developers at every level, from newcomers to seasoned professionals. It's a tool we rely on daily, but have you ever wondered about the lesser-known Git commands that could enhance your workflow?

Let's explore 12 Git commands that might just surprise you with their usefulness and efficiency.

1. Git stash

Git stash is useful commands that help you in Git workflows with many branches and many people effectively. In the real world, have ever seen a case that you are developing on the branch and your leader said “hey man, I need your urgent fix on another branch”, how to save your current version code locally but not make commit?

//save unchange commits
git stash //restore commits
git stash pop 

image.png Cre: Scaler

2. Git diff

In case you need to compare between un-saved changes and the previous commit, git diff is the command you should know in using Git with commands besides using GUI or Git extension.

git diff

3. Git rebase

Rebasing is a powerful besides on Merge feature in Git with the same purpose but with different methods. The most awesome feature that git rebase brings is keeping the Git tree clean, straight and avoiding “the diamond shape” in the Git graph.

//checkout branch featuire
git switch feature //rebase feature into main
git rebase -i main feature

image.png Cre: ByteByteGo

4. Git cherry-pick

The git cherry-pick command is used to apply a specific commit from one branch to another. This can be useful when you want to selectively bring in changes from one branch to another without merging the entire branch.

git cherry-pick <commit-hash>

5. Git log

With git log, you can show all commit logs in Git including commit hash, commit message, commit author, and the date that the commit was committed.

git log

6. Git commit —amend

If you ask how can I change my commit information, the git commit --amend is your answer. You can changed information of that commit including: message and author.

git commit --amend -m "New commit message" --author="Author Name <email@example.com>"

7. Git tag

Tagging versions in Git using the git tag command is a common practice to mark significant points in your repository, such as releases or milestones. You can tag commits with version numbers or any other meaningful identifier.

git tag -a v1.0 -m "Release version 1.0" <commit-hash>

8. Git checkout .

In case you need to clean up all changes locally or add code to show logs on the server for tracing errors and hot fixing, git checkout . is the powerful command that you should know.

git checkout .

9. Git reset

git reset is a powerful command in Git that allows you to reset the current HEAD to a specified state. It's commonly used to undo changes, move the HEAD to a different commit, or unstage changes.

If you want to move the HEAD to a specific commit but keep the changes in your working directory and staging area, you can use soft reset. If you want to completely undo the last commit and discard the changes from that commit, using git reset hard.

//soft reset
git reset --soft <commit-hash> //hard reset
git reset --hard HEAD^

10. Git revert

git revert is a command used to create a new commit that undoes the changes made by a specific commit or commits. Unlike git reset, which alters the commit history, git revert maintains the history by creating a new commit that undoes the changes introduced by previous commits.

git revert <commit-hash>

11. Git remote —prune

The git remote --prune command is used to remove any remote-tracking references that no longer exist on the remote repository. Over time, these references can become outdated if branches are deleted or renamed on the remote repository.

git remote prune origin

With this command, you can sync up locally with your remote git workspace.

12. Git filter-branch

git filter-branch is a powerful but potentially dangerous command in Git that allows you to rewrite the commit history of a repository. It's typically used to rewrite commits to apply various filters or transformations.

This command can be useful for tasks such as removing sensitive data from the history, rewriting commit messages, or splitting a repository into multiple repositories.

For example, in case you need to rewrite author of commit history in the repository, you can using this command followed in below script.

#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="your_old_email"
CORRECT_NAME="your_new_name"
CORRECT_EMAIL="your_new_email" if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]; then export GIT_COMMITTER_NAME="$CORRECT_NAME" export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]; then export GIT_AUTHOR_NAME="$CORRECT_NAME" export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

Appendix

This blog was written by the self-author from multi-platforms:

Bình luận

Bài viết tương tự

- vừa được xem lúc

Closure trong Javascript - Phần 2: Định nghĩa và cách dùng

Các bạn có thể đọc qua phần 1 ở đây. Để mọi người không quên, mình xin tóm tắt gọn lại khái niệm lexical environment:.

0 0 66

- vừa được xem lúc

Var vs let vs const? Các cách khai báo biến và hằng trong Javascript

Dạo này mình tập tành học Javascript, thấy có 2 cách khai báo biến khác nhau nên đã tìm tòi sự khác biệt. Nay xin đăng lên đây để mọi người đọc xong hy vọng phân biệt được giữa let và var, và sau đó là khai báo hằng bằng const.

0 0 47

- vừa được xem lúc

VueJS: Tính năng Mixins

Chào mọi người, hôm nay mình sẽ viết về Mixins và 1 số vấn đề trong sử dụng Mixins hay ho mà mình gặp trong dự án thực. Trích dẫn từ trang chủ của VueJS:.

0 0 41

- vừa được xem lúc

Asset Pipeline là cái chi chi?

Asset Pipeline. Asset pipeline là cái chi chi. . Giải thích:.

0 0 72

- vừa được xem lúc

Tạo data table web app lấy dữ liệu từ Google Sheets sử dụng Apps Script

Google Sheets là công cụ tuyệt vời để lưu trữ bảng tính trực tuyến, bạn có thể truy cập bảng tính bất kỳ lúc nào ở bất kỳ đâu và luôn sẵn sàng để chia sẻ với người khác. Bài này gồm 2 phần.

0 0 280

- vừa được xem lúc

Học Deep Learning trên Coursera miễn phí

Bạn muốn bắt đầu với Deep Learning nhưng không biết bắt đầu từ đâu? Bạn muốn có một công việc ở mức fresher về Deep Learning? Bạn muốn khoe bạn bè về kiến thức Deep Learning của mình. Bắt đầu từ đâu.

0 0 50