DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

Cover image for Beginner๐ŸŒฑ Git Commands
Shahid Siddiqui
Shahid Siddiqui

Posted on

Beginner๐ŸŒฑ Git Commands

Hi ๐Ÿ‘‹๐Ÿป
If you just begin your Git Journey and confused๐Ÿ˜• with Git Commands ,Then this is for you โ˜บ๏ธ!

Environment setting for Git Bash

If you don't know Git Bash or if you haven't used git in your machine ๐Ÿ’ป , follow these steps to get started:

1.Download Git Bash for your device ๐Ÿ’ป Git Bash

2.Open Git Bash by clicking the right mouse button ๐Ÿ–ฑ๏ธ for Windows user. Then set up your name๐Ÿ“› and email๐Ÿ“ง ID:

$ git config --global user.name "<Your_Name>"

$ git config --global user.email "<Your_Email>"
Enter fullscreen mode Exit fullscreen mode

3.Go to GitHub and create your GitHub account using your email.
Make sure ,to use the same email๐Ÿ“ง ,you have used earlier.

At this instance there are two ways to go furthur:

1๏ธโƒฃFirst

4.Go to your desired folder๐Ÿ“ your you want to keep all your your GitHub repository๐Ÿ˜ณ.

clone a remote repo. via URL

         git clone URL
Enter fullscreen mode Exit fullscreen mode

You may get URL from a online GitHub repository by Clicking on Code
Button ๐ŸŒ› of Your repository.

2๏ธโƒฃSecond

4.Go to a folder๐Ÿ“ which you want to make as a GitHub repository๐Ÿ˜ณ.

Initalize a existing directory as a git repo.

        git init
Enter fullscreen mode Exit fullscreen mode

That's all for setting ,Now begin your Code

๐ŸŽ‰๐ŸŽ‰Commands๐ŸŽ‰๐ŸŽ‰

Commands

1 :

Below will add your modified file.

           git add myFile
Enter fullscreen mode Exit fullscreen mode

When you want stage your changes for myFile file.

           git add .    Or    git add -A
Enter fullscreen mode Exit fullscreen mode

When you want stage your All modified file.

2 :

Below will restore your file.

           git reset myFile
Enter fullscreen mode Exit fullscreen mode

unstage a file while retaining the changes in working directory

Below will show all commits with all details like author and their email.

            git log
Enter fullscreen mode Exit fullscreen mode

Below will show commits in compact way!,Only show commit hash and message

            git log --oneline
Enter fullscreen mode Exit fullscreen mode

3 :

           git revert <commit hash>
Enter fullscreen mode Exit fullscreen mode

If you want roll back your commit,You may use above command.
you can get commit hash of all of your commit by entering command: git log

4 :

Below will show your current status.

             git status
Enter fullscreen mode Exit fullscreen mode

If you haven't staged or add it ,It will show like this

 On branch master
   Changes not staged for commit:
   (use "git add <file>..." to update what will be committed)
   (use "git restore <file>..." to discard changes in working directory)
    modified:   abcd.md

   no changes added to commit (use "git add" and/or "git commit -a")
Enter fullscreen mode Exit fullscreen mode

If you have staged your file. Then, It will show you staged modified file for your next commit.Like this:

 On branch master
  Changes to be committed:
   (use "git restore --staged <file>..." to unstage)
    modified:   abcd.md
Enter fullscreen mode Exit fullscreen mode

5 :

            git push origin <branch>
Enter fullscreen mode Exit fullscreen mode

When you want to push your final changes to remote repository in particular branch.By default ,you have master branch ,so you may
write as:

           git push origin master
Enter fullscreen mode Exit fullscreen mode

6 :

            git pull
Enter fullscreen mode Exit fullscreen mode

When you want to pull all changes from remote repository in your current local branch.

7 :

When you want to create branch

           git branch branch1
Enter fullscreen mode Exit fullscreen mode

create a new branch name branch1 at the current commit

When you want to see all branches

           git branch
Enter fullscreen mode Exit fullscreen mode

It will list your branches. And a * will appear next to the currently active branch like this:

       PS D:\winter plan> git branch
       * branch1
         master
         one
Enter fullscreen mode Exit fullscreen mode

When you want to switch branch

           git checkout branch1
Enter fullscreen mode Exit fullscreen mode

switch to another branch1 and check it out into your working directory

8 :

Merge a branch into another or main

        git merge branch1
Enter fullscreen mode Exit fullscreen mode

merge the branch1 history into the current one

When you want to switch branches either you need to commit your modified changes Or you may temporarly stored modified and tracked file.

          git stash
Enter fullscreen mode Exit fullscreen mode

above command Save modified and staged changes and below will discard the changes from top of stash stack

          git stash drop
Enter fullscreen mode Exit fullscreen mode




Happy Gitting ๐Ÿ’ฅ๐Ÿ’ฅ

Top comments (0)

๐ŸŒš Browsing with dark mode makes you a better developer.

It's a scientific fact.