Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 4523

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 27, 20242024-11-27T12:45:08+00:00 2024-11-27T12:45:08+00:00

5 Git commands you should know πŸš€

  • 61k

In this article, I will discuss some useful git commands we may need in our day-to-day work. Git reset (soft and hard), revert, rebase, rebase -i and log.

To illustrate these different commands and for simplicity, I will use the same git project.

My example is very straightforward. I initialized a git project and added files from 1.txt to 10.txt, and each file is added in a separate git commit. 1.txt is in commit 1, 2.txt is in commit 2 and so on.

8d679fa (HEAD -> master) 10
e7535c7 9
916c4d5 8
eb15d0d 7
94669d1 6
8300f81 5
617590b 4
f0b1c87 3
a62c707 2
2ad16a7 1

Git reset.

Git reset is a powerful command when we have to remove something we did in a previous commit. There are two types of reset hard and soft.

soft:
We use a git soft when we want to remove a set of commits but unstage all the changes there. In other words, you still have these commit contents, but they are unstaged, so you have the choice to remove them or update them and stage them for a new commit.

Example:

git reset 8300f81 will unstage commits from 10 to 6, and the head is on the specified commit 8300f81

hard:
Reset hard does not offer the possibility to stage our reset changes; instead, it will remove the commits and all their content explicitly.

Example:

git reset 8300f81 --hard will remove commits from 10 to 6 and remove related content as well. The head is now on 8300f81.


git revert

Do not get confused by reset and revert, there are differences between them.
Imagine a situation where you make some changes, and then you figure out that these changes are worthless now, but you may need them in the future. What would you do?
You could revert them. Revert makes it possible to remove unwanted changes, but at the same time, it keeps the commit containing these changes. Revert creates automatically a revert commit that does not contain unwanted changes.

Example:

Given that you are not satisfied with commit 617590b and at the same time you want to keep it in history.
So you reverted that commit: git revert 617590b
It opens a terminal screen where you specify your revert message. Once done, the head will be on the new revert commit, and the old commit will remain in your git history.

e6371ff (HEAD -> master) Revert "4"
8d679fa 10
e7535c7 9
916c4d5 8
eb15d0d 7
94669d1 6
8300f81 5
617590b 4
f0b1c87 3
a62c707 2
2ad16a7 1


git rebase

Rebase is similar to merge, but it works differently. Instead of creating a merge commit, it “rewrites” history by changing the base of your branch from one commit to another. In this way, it maintains a flat history and eliminates ambiguity. The following is the most evident illustration that I have discovered illustrating the distinctions between Rebase and Merge.

Merge vs Rebase

Merge vs Rebase


git rebase -i

Interactive rebase is a magical command when it comes to making changes in git commit history, like editing commit messages, combining commits together, or removing a commit(Example below).
git rebase -i 8300f81 (commit 5). Will open your terminal with the commit list from commit 10 to commit 6. In front of each commit you can choose between multiple options like pick, reword, fixup, squash, drop. Once you are satisfied with your choice, the terminal will be closed automatically, and your changes will be applied.

Rebase-i

Interactive rebase screen example


git log

This command gives you all your git commit history, commit hash and message. Useful to keep track of your project or whenever you need to use a commit hash in one of your git commands.
Multiple flags could be used with log like –oneline (for each line print a single commit), –all (all branches)

git log --oneline --all


Conclusion

Throughout the article, I tried to simplify what these different commands do and what their benefits are. I hope the examples are clear and help you choose the git command you need.

I think, in addition to the usual git commands (status, add, commit, pull and merge)reset, revert, log and rebase are handy for everyday work and make you more confident with git.

Thanks for reading !

gitprogrammingtutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.