DEV Community

Cover image for Get introduced with Git and GitHub
Shekhar Ranjan
Shekhar Ranjan

Posted on

Get introduced with Git and GitHub

Hello friends 👋 😃 , when we talk about git and github then, there are lot of confusion 😕 exist in our mind. Especially, when we talk about their commands. I also encountered with the same issue and mentors helped me and motivate me to face this and gave me some suggestions in order to overcome this. And thanks to the mentors, who suggested me to write an article about it. So, here I am writing about some concepts, which
are related to github and git and some commands(which you can use it in git bash, cmd or vscode terminal) too, which, I hope, would be very
helpful for everyone.

So, here we are discussing about following terms, which might be very important:

  • Fork
  • Branches
  • Push commit to a remote repository

Fork

When we fork a repository, in our case we fork the Twindle (I have learned a lot and still learning so many things from Twindle) repository then, we get a personal copy of the repository in our github account, where we are free to make changes without affecting the main project. When we do some changes in our own repository and want to implement these changes in the main project then, we raise pull request(PR). The authority of the main project review the changes and if they think it is good to include this change in the main project then, they add it( you everytime want this to happen ) otherwise, reject it( sorry, but it happens too ).

Branches

Branches are almost like a new copy of our code at the current state( it means you forked a repository, which becomes "master" version in your repository and then, you make a copy of your "master" version ), which can then be used to develop new code.For example, whenever we need to create a new feature, or rewrite any of our code, it's a good thing to create a new branch so that none
of our changes affect the "master" version of the code. This is important since it can be very difficult to revert code changes from memory, especially in complex systems. And also there are many changes happen in the "master" version of the main repository like in Twindle, people work on the code and raise PR requests. If we try to raise PR request from our "master" version then, the PR requests raised by the people will also get
added in our raised PR and this creates mess. So, to avoid these problems, we need to create a branch, raise PR request from that branch, this will avoid others' PR requests and when the PR request is approved then, that branch must be deleted.

First of all, when we are creating a branch using git bash then, we must assure that we are in the main branch as shown below:

git9

Here, in the above photo, "(main)" shows that we are in the main branch. Now, there are certain commands we should know and these are as follows:

  • To initialize a repository for a new or existing project,
      git init
Enter fullscreen mode Exit fullscreen mode
  • To create a branch from the current branch
      git branch <branch-name>
Enter fullscreen mode Exit fullscreen mode

this makes a copy of main branch,

git10

  • To see how many branches in the git or local,
    git branch
Enter fullscreen mode Exit fullscreen mode

git11

By seeing the photo, we also conclude that we are in the main branch because of the * .

  • To see all the branches (local as well as remote),
   git branch -a
Enter fullscreen mode Exit fullscreen mode

git12

From the above picture, we can see the local as well as remote branches(in red color).

  • To switch from current branch to another branch,
  git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

git13

Here, first we in main branch, see all branches in the local and then, we switch to another branch.

  • To delete the branch but first, you need to go into the master or main branch then,
  git branch -d <branch-name>
Enter fullscreen mode Exit fullscreen mode

git14

And again check how many branches are in your local.

Note: if it shows error: The branch is not fully merged then, please use,

  git branch -D <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • To add one or more files to staging(index) area,
    git add <file_name>
Enter fullscreen mode Exit fullscreen mode
  • To list the files you have changed and those still need to add or commit,
   git status 
Enter fullscreen mode Exit fullscreen mode
  • To remove files from staging area(unstage),
  git rm --cached <file_name>
Enter fullscreen mode Exit fullscreen mode
  • To commit changes to head(here, head means where you are),
  git commit -m "commit message" 
Enter fullscreen mode Exit fullscreen mode
  • To see the total commits you have done,
  git log
Enter fullscreen mode Exit fullscreen mode

Push commit to a remote repository

Commit means to save your changes in our local repository. When we do commit in the local repository then, we need to push this commit in remote
repository or our github account. We use git push to push commits to do this.

The git push command takes two arguments:

  • A remote name, for example, origin
  • A branch name, for example, branch1

    git push origin branch1

then, go to the github account where, we can see a branch of same name is created with the changes we did in the local. From that same branch that is,
branch1, raise pull request. This PR would be clean and without any mess. After approval or rejection, please remove that branch from remote and local.

Suggestions are welcome 😊 !!

Top comments (2)

Collapse
 
admiracod profile image
Mohammed Ali

Thanks a lot Shekhar, you made it shorter and easier to understand :)

Collapse
 
shekhar10feb profile image
Shekhar Ranjan

Your're welcome :)