@manhng

Welcome to my blog!

Git Commands That Saved My Life At Least Once

August 13, 2021 14:44

Git Commands That Saved My Life At Least Once (edit)

Usage:

git clean -d -f -f
git checkout dev-feat-01
git pull

git checkout dev
git pull

git merge dev-feat-01 --no-ff

1. Get or set user and email variables

git config user.name
git config user.email
git config --system user.email
git config --global user.email
git config --local user.email

2. How to change the author (email) of one or more commits

git commit --amend --no-edit --author="New Name<new_email@free.com>"
git push --force-with-lease

3. Add new changes to the last commit

git commit --amend

4. Remove a commit from the git log

git reset --hard HEAD^

But let’s say your git log is as below:

* 97c8db67 fix: commit 3 is correct as well
* 69e04606 feat: commit 2 is correct
* 9f130ee1 fix: commit 1 needs to be removed

If you need to remove the 9f130ee1 git-sha (“commit 1 needs to be removed”), you could do the interactive rebase:

git rebase -i HEAD~3

Where 3 is the number of commits that you are going to look for before choosing which you would like to remove. Then a text editor will be open with this content:

pick 9f130ee1 fix: commit 1 needs to be removed
pick 69e04606 fix: commit 2 is correct
pick 97c8db67 fix: commit 3 is correct as well

5. Editing an old commit message

git rebase --interactive HEAD~3

6. How to do a rebase from the default branch

git pull --rebase origin main

This will basically:

  1. undo your local commits
  2. pull commits from the remote
  3. add your local commits back

If there are any git conflicts you will be asked to solve them before you push.

7. Reverting local file back to original version in a different branch

git checkout origin/master FILE_PATH_AND_NAME

8. Reverting/Aborting local commit

git reset HEAD~1 (Windows)

git reset HEAD^ (Unix)

9. Showing file remote history

git diff FILE_COMMIT_HASH FILE_PATH

10. Discarding unnecessary  files

git checkout -- .

11. Reverting local commits

git fetch

git reset --hard origin/REMOTE_BRANCH_NAME
git clean -d -f -f

12. Showing committed files that your will push to the remote

git diff --stat --patch origin REMOTE_BRANCH_NAME
git log --oneline 5a984958..49a7891a

References:

http://tugrulaslan.com/list-of-life-saving-git-commands/

https://betterprogramming.pub/7-git-commands-that-saved-my-life-once-or-forty-two-times-c76cc94244de

Git Handbook

March 24, 2021 16:20

Git Handbook (edit)

Advanced Git - @manhng (HAY HAY HAY)

Git Commands - @manhng

Duplicating a repository - @manhng

---- Delete local branch
git branch -d develop-test

---- Delete remote branch
git push origin --delete develop-test

---- Loại bỏ những thay đổi đã được commit của bạn
git reset --hard

git checkout -b develop-new-branch
git add .
git commit -m "Comment"
git push --set-upstream origin develop-new-branch

git checkout -b dev (tạo nhánh mới và chuyển sang nhánh đó)
git checkout -b dev master (tạo nhánh mới từ nhánh gốc master và chuyển sang nhánh vừa tạo đó)
git add . commit -m "Update Login API" (gộp 2 lệnh trên 1 dòng lệnh)
git log --all --oneline (kiểm tra lịch sử commit)

git checkout dev
git merge login (merge fast-forward)

git checkout dev
git merge login --no-ff (merge non fast-forward)

Hotfix

1) Tạo nhánh mới từ nhánh gốc master
git checkout -b fix-xxx master

2) Bạn merge nhánh hotfix vào cả hai branches master và dev, phần sửa lỗi sẽ xuất hiện ở cả hai branches, giúp history không bị rẽ nhánh bất ngờ.

git checkout master
git merge fix-xxx --no-ff

git checkout dev
git merge fix-xxx --no-ff

Cách phân nhánh và chia việc trong nhóm với Git – Ehkoo

Kết luận

Chúng ta có thể tóm tắt bài này lại như sau:

  • Dự án được chia thành nhiều nhánh, bao gồm master, dev và có thể có staging
  • Các nhánh tính năng được chia ra từ dev, phát triển độc lập, được rebase trước khi merge lại vào dev
  • Rebase có thể thay đổi một chút history, hoặc squash lại thành một commit duy nhất
  • Merge có thể là fast-forward hoặc non-fast-forward
  • dev sẽ được merge vào master mỗi khi triển khai. Trường hợp có staging, dev sẽ được merge vào staging, và staging sẽ được merge vào master.
  • Các nhánh hotfix sẽ được chia ra từ master, sau đó merge --no-ff vào master  dev

Dĩ nhiên bài viết này chỉ mang tính tham khảo, vì mỗi team mỗi công ty sẽ có những cách làm riêng.

Using Git on the command line

September 26, 2020 09:43

Using Git on the command line (edit)

GitHub: Scheduler with Quartz

git init

notepad README.md

git add README.md

git branch -M master

git remote add origin https://github.com/manhng1983/Quartz.git

git commit -m "first commit"

git push -u origin master

...

git commit -m "second commit"

git push -u origin master

...

git commit -m "n commit"

git push -u origin master

...

git push -u origin master

GitHub: Unit Testing with MSTest in ASP.NET MVC 5

D:\manh83vn>md UnitTesting

D:\manh83vn>cd UnitTesting

D:\manh83vn\UnitTesting>git init
Initialized empty Git repository in D:/manh83vn/UnitTesting/.git/

D:\manh83vn\UnitTesting>notepad README.md

D:\manh83vn\UnitTesting>git add README.md

D:\manh83vn\UnitTesting>git remote add origin https://github.com/manhng1983/UnitTesting.git

D:\manh83vn\UnitTesting>git commit -m "first commit"
[master (root-commit) 49607c8] first commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README.md

D:\manh83vn\UnitTesting>git branch -M master

D:\manh83vn\UnitTesting>git push -u origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 208 bytes | 208.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/manhng1983/UnitTesting.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

D:\manh83vn\UnitTesting>git add *

D:\manh83vn\UnitTesting>git commit -m "second commit"
[master 56271d6] second commit
590 files changed, 553190 insertions(+)

D:\manh83vn\UnitTesting>git push -u origin master
Enumerating objects: 707, done.
Counting objects: 100% (707/707), done.
Delta compression using up to 8 threads
Compressing objects: 100% (678/678), done.
Writing objects: 100% (706/706), 49.18 MiB | 5.08 MiB/s, done.
Total 706 (delta 280), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (280/280), done.
To https://github.com/manhng1983/UnitTesting.git
49607c8..56271d6 master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

D:\manh83vn\UnitTesting>

Git Commands

August 14, 2017 22:57

Git Commands (edit)

Git - Học nghiêm túc một lần (Phần 1)

https://viblo.asia/p/git-hoc-nghiem-tuc-mot-lan-phan-1-OeVKBo6JZkW

Git - Học nghiêm túc một lần (Phần 2)

https://giaphiep.com/blog/git-hoc-nghiem-tuc-mot-lan-phan-2-25505

Git - Những câu lệnh hữu dụng

https://viblo.asia/p/tap-hop-nhung-cau-lenh-git-huu-dung-dWrvwWr2vw38

Git - Những câu lệnh cơ bản

https://jusfunny.wordpress.com/2016/11/09/cac-lenh-git-co-ban-ma-toi-hay-dung/

In this tutorial, we will go through basic Git commands step by step and see how to use in the project. Also, we will put the code in the cloud using GitHub. It is assumed Git is already installed and configured on your machine. I am using Windows 10 for this post, but the same Git commands can be applied on Linux/Ubuntu.

1. To create a new local repository:

Let’s create a directory for repository.

mkdir repo
cd repo

use following command to create repository

git init

It creates a .git directory that contains all the Git-related information for your project.

2. Create new file file1.txt and file2.txt in repo directory and run following command to check status.

git status

Status command displays a list the files you’ve changed and those you still need to add or commit.

3. Adding files:

Run following command to add both files:

git add file1.txt file2.txt

Add command adds one or more files to staging (index).

4. Commit:

After staging files, we can commit them into Git. Run following command to commit:

git commit -m "First commit"

-m for commit message.

you can use -a to commit any files you’ve added with git add, and also commit any files you’ve changed since then.

git commit -a

Note: it is dangerous. let’s say you opened a file and changed it by mistake. if you add -a to your commit, all files would be committed and you would fail to notice possible errors.

You can use both -a and -m as well

git commit -am "My commit message"

5. Further Commits:

Let’s modify file1.txt after first commit. Now to check the changes from the last commit, run following command:

git diff

If you want to have a look at the changes to a particular file, you can run git diff <file>.
Let’s commit the changes

git commit -am "Second commit"

6. To show log:

To check the history of your project, run the following command:

git log

To view the details of a particular commit:

git show <hash>

Where <hash> is the hex number associated with the commit. you do not need to copy the whole string, and the first 5-6 characters are enough to identify your commit. As in the screenshot, only d9f8 is used.

7. To put code on remote server:

You could create a project on GitHub, GitLab, or BitBucket and push your existing code to the repository. Conveniently, a remote to which you have write access is called the origin.

Run following commands to add a remote origin and then push the code to the origin.

git remote add origin https://github.com/techbrij/gitsample.git
git push -u origin master

Push command is used to send changes to remote repository.

Git Commands: Step By Step Guide (Part 2)

1. Checkout a repository:

To create a working copy of a local repository by running the command

git clone /path/to/repository

when using a remote server, your command will be

git clone username@host:/path/to/repository

In our case, as it is public Github repo so simple link is used

2. Branching:

Let’s create a new branch “branch1” and switch on it

git checkout -b branch1

Modify file2.txt manually then check status and commit it.

git status
git commit -am "Branch1 first commit"

To push the branch to your remote repository, so others can use it:

git push -u origin branch1

To list all the branches in your repository and to know what branch you’re currently in, run following command:

git branch

To switch from one branch to another:

git checkout <branchname>

3. Merging:

Before merging, you can preview the changes using git diff command:

git diff master branch1

If everything looks okay then use following command to to merge branch1 into your active branch (e.g. master)

git merge branch1

4. Update your code:

To update your local repository to the newest commit from the remote repository, run following command:

git pull

5. Undo Local Changes:

If you want to sync local code with remote code and drop all your local changes and commits then run following command:

git fetch origin
git reset --hard origin/master

6. Reset Last commit only:

If you do wrong commit by mistake and want to remove all changes in last commit, run following command

git reset --hard HEAD~1

Conclusion:

In this tutorial, we have covered basic Git commands related to branching and merging.
Here is Oliver Steele’s image of how all it all fits together:

Hope, It helps. Enjoy Git !!!

Categories

Recent posts