DEV Community

Cover image for A List of Essential Git Queries
Chaitanya Chaturvedi
Chaitanya Chaturvedi

Posted on

A List of Essential Git Queries

1) How to submit a pull request?

A) Fork the Repository

Go to the repository where you want to contribute. Click on the fork button at the top right corner to fork the repository. This will create a copy of that repository under your GitHub user account.

B) Clone the Repository to your system

After you fork the repo you will be automatically redirected to the copy of that repo under your user account. If not go to the repositories section under your user account and look for the same repo as the one you forked.
Now click at the green button named as code at the top right corner. Copy the https link.
Now open a terminal and go to the desired location on your system where you want to clone the repo. Now enter git clone and paste the copied URL.
$ git clone <URL>

C) Create a new branch

Now navigate to the cloned repo using terminal like cd repo_name. Now you are at the master branch of the cloned repo. We leave the master branch as it is and create a new branch where we will do the desired work on the repo. So to create a new branch enter git checkout -b new_branch_name. This will create the repo and switch you to the created repo. Now do the desired changes here.

D) Push the repo

After you have finished committing changes its time to push those changes. Now to push the repo to the GitHub server enter
$ git push -u origin new-branch_name

E) compare & pull request

Now go the the forked repo under your user account and there you will see a green button at the top right corner saying compare & pull request. Click on that and then you will be taken to open a pull request page. There you enter title and description to their respective fields and then click on create pull request to create a pull request.

2) How to undo last commit?

To undo the last commit Enter
$ git reset --soft Head~
You can use git log --oneline to see previous commits`

The Hierarchy of Head commits is as follows

$ git log --oneline
3fad532 Last commit (HEAD)
3bnaj03 Commit before HEAD (HEAD~1)
vcn3ed5 Two commits before HEAD (HEAD~2)

3) How to revert a specific commit that has been pushed?

use git log to find the commit. Then do
$ git revert <commit id>. But this does auto commit. So if you just want the files and no auto commit then do
$ git revert -n <commit id>

4) How to rollback to a specific commit?

$git reset --soft <commit id> will rollback to the desired commit id leaving your local code and local history exactly the same.
$git reset --hard <commit id> will rollback to the desired commit id making your local code and local history the same as it was at that specific commit. But then it would fail if you try to push as it has been detached from the head now.

5) How to switch branches without committing?

stage the changes by git stage . Then do
$ git stash This will save your changes and make your directory clean. Now switch branches. To see which stashes you have stored enter git stash list

$ git stash list
stash@{0}: WIP on master: 049d078 Create index file
stash@{1}: WIP on master: c264051 Revert "Add file_size"
stash@{2}: WIP on master: 21d80a5 Add number to log

use git stash apply to apply the most recent stash or specify older stashes by git stash apply stash@{2}

Thanks for reading :)
Stay tuned there is more to come.

Also Follow me on Twitter and Instagram I post informative content daily.

Top comments (2)

schmitzel76 profile image
Patrick Schmitz

Using standard conventions, origin points to the repository you've cloned, and upstream points to the repository you have forked from. Origin is created automatically when cloning, and upstream should be created manually.

In your example, you let upstream point it to the cloned remote repository, which makes origin and upstream reference the same remote repository, which can lead to confusion. You could just remove the section about adding the upstream remote as you don't use it in the examples anyway.

chaitanya4vedi profile image
Chaitanya Chaturvedi • Edited

Thank you for the correction. I can't remember why I added that but as you pointed out it was incorrect and unnecessary at the same time. Thank You.