DEV Community

sai bhargav
sai bhargav

Posted on

Github - A Developer World

whenever you hear this word 'github' from a developer or your friends so a question pops up in your head 'what is github' 
Enter fullscreen mode Exit fullscreen mode

What is Github?

Github is a provider of internet hosting for software development and version control using Git. It offers the distributed version control and source code management functionality of Git, plus its own features. so normally it helps developers to learn and contribute their work in this is the work of github and you might heard the word repository

What is repository?

A repository contains all of your project's files and each file's revision history. You can discuss and manage your project's work within the repository.
so this is repository it helps us to keep record of our work.

what are alternatives of github?

Top 5 Alternatives To GitHub
Google Cloud Source Repositories.
so these are the alternatives which I explored which quite well function but they are less popular mostly in github most of the opensouce contribution are done. Now you quite well know about github and its uses now you might be curious what are the commands in github.

What are top git commands are used always?

Top Git Commands for Developers :

1.Git Init
2.Git Clone
3.Git Branch
4.Git Checkout
5.Git Add
6.Git Commit
7.Git Push
8.Git Pull
9.Git Diff
10.Git Stash
11.Git Status
12.Git Log
13.Git Merge

Let's jump in and look at each of them in detail.

  1. Git Init

The git init command is usually the first command you’d run in any new project that is not already a Git repository (also commonly called repo).

It can be used to convert an existing, un-versioned folder into a Git repository. Also, you can use it to initialize an empty repo.

Using Git Init Command

cd into the directory you want to initialize.

Then, run this command.

$ git init

This will transform the current directory into a Git repository. A .git sub-directory will be added. This will allow you to start recording multiple versions of your project.

Note: Running git init on an already initialized directory will not override any of your settings.

  1. Git Clone

The git clone command is used to download the source code from a remote repository (like GitHub, Bitbucket, or GitLab).

Using Git Clone Command

$ git clone (https://url-of-the-repository)
When you clone a repo, the code is automatically downloaded to your local machine.

As a convenience, the downloaded version is linked to the origin (the repository from where you downloaded). I say this is a convenience because when you’re collaborating on the same project, you’d want to push your changes (more on that below) to a single repo.

But sometimes, people wouldn’t want to have this link. If your use case is similar, run

$ git remote rm origin
This will disassociate the downloaded current repository from the origin.

  1. Git Branch

Branch is one of the most important functionalities of Git. This allows teams to work on the same code base in parallel. It enables teams to create Git Workflows and make their workflows more efficient.

Say, you’re working on “Feature A”. And your teammate is working on “Feature B”. By creating a separate branch for each feature, both of you can work on the same code base in parallel without having to worry about conflicts (at least while you’re writing the code).

You can use this command to create a new branch, view existing branches, or delete a branch.

Using Git Branch to create a new branch

$ git branch (branch-name)
This command will create a new branch only in your local system. If you want this to be visible to all the members in the repo, you’ll still have to push the branch.

To push a newly created branch, run git push -u (remote) (branch-name)

Command to view all branches

$ git branch


$ git branch --list
Git command to delete a branch

$ git branch -d (branch-name)

  1. Git Checkout

A mistake I often made when I first started learning git commands was forgetting to switch to the new branch I just created. Yes, after you create a branch, you’ll have to switch to it with another command. That’s where the Git Checkout command comes in.

Using Git Checkout Command

$ git checkout (branch-name)
This will automatically switch you to the branch name you mentioned in the command.

However, when switching from one branch to another, you need to keep two things in mind:

If you made some changes in the previous branch, you will have to first commit and push them (I’ll cover this command below) to your remote repo.
The branch you want to switch must be present in your local system. If not, you can pull them (covered below).
If you’re as lazy as I am, I’m sure you’d want one single command that will both create a new branch and automatically switch to it.

The below command does exactly that.

$ git checkout -b (branch-name)

  1. Git Add

Every time you create a new file, delete it, or make a change, you’ll have to tell Git to track it and add it to the staging area. Otherwise, the files you made changes to wouldn’t be added when you try to push your changes.

Using Git Add Command

$ git add (file-name)
This command will add only a single file to your next commit. If you want to add all the files to which changes were made, you can use

$ git add -A
It’s important to remember that using git add will not make any changes in the remote repository. Your changes will be recorded only when you commit them.

  1. Git Commit

Think of Git Commit command like a checkpoint in your development process.

It’s commonly used to save your changes. Maybe after completing a specific work item assigned to you in your agile tool.

Every time you commit your code changes, you’ll also include a message to briefly describe the changes you made. This helps other team members quickly understand what was added, changed, or removed.

Using Git Commit Command

$ git commit -a
This will commit all the changes in the directory you’re working in. Once this command is run, you’ll be prompted to enter a commit message.

Alternatively, you can enter the commit message in the command itself and skip the additional step where you’ll be prompted to enter the commit message.

To do this, run the following git command:

$ git commit -am “(commit-message)”
Note: The git commit command saves the changes only in your local repository. It does not push to the remote origin and make your changes accessible for others to collaborate.

  1. Git Push

To make all your committed changes available to your teammates, you’ll have to push them to the remote origin.

Using Git Push Command

$ git push (remote) (branch-name)
It’s important to remember that git push command will upload only the changes you’ve committed.

  1. Git Pull

Of course, you’d want to have the latest updates from teammates as well!

The git pull command allows you to fetch all the changes that your teammates pushed and automatically merge them into your local repo.

Using Git Pull Command

$ git pull (remote)
In many cases, you will run into conflict because you had changed a line in a file that another teammate added. In such cases, you need to resolve the conflicts manually.

  1. Git Diff

Git Diff is my go-to command when I want to quickly see the difference between my current branch and another branch (usually the branch I’m merging into).

Using Git Diff Command

$ git diff
This will show you any uncommitted changes in your local repo.

To compare two branches

$ git diff branch1..branch2
This will show all the file differences between the two branches.

To compare a file from two branches

$ git diff branch1 branch2 ./path/to/file.txt
This command will show a comparison of the changes made to file file.txt across the branches branch1 and branch2.

  1. Git Stash

Git Stash temporarily shelves your work, so you can switch to another branch, work on something else, and then come back to this at a later time.

It’s perfect if you need to work on something else and you’re midway through a code change, but aren’t ready to commit the code.

Using Git Stash Save Command

$ git stash save “(stash-message)”
This will stash your changes with the message you entered. This can be helpful when you want to come back and restore your stash, especially when you have several stashes.

However, this will only stash your tracked files that you added using git add. If you want to include the untracked files as well, run

$ git stash save -u
Using Git Stash List Command

When you want to view all the stashed code, you can view them using this command. Once you stash your code, git will assign a stash id, so you can restore a specific stashed code later.

For example:

$ git stash list
This might show the following

stash@{0}: On master: Stashed with message1
stash@{1}: On master: Stashed with message2
Using Git Stash Apply Command

This will automatically restore and apply the topmost stash in the stack.

$ git stash apply
If you want to restore a specific stash that you want to apply, using the above example, you can simply run git stash apply stash@{1}.

Note: When you use git stash apply, the stashed version will be applied to your current working branch. However, it will not delete the stash from the stack.

Using Git Stash Pop Command

To automatically also delete the stash from the stack, the git stash pop command is used.

If you want to do it for a specific stash in the stack, run git stash pop stash@{0}.

  1. Git Status

When you’re feeling a bit lost with what’s happened in your repo (yes, it can happen) the Git Status command can tell you all the information you’ll need to know.

Using Git Status Command

$ git status
This can give you information such as:

Your current branch
Whether your current branch is up to date
If there’s anything in the branch that needs to be committed, pushed, or pulled.
If you have any files that are either staged or not staged.
And if you have any files that are created, modified, or deleted.

  1. Git Log While git status gave you nearly all the information you’d have needed, it wouldn’t give you the information about the commit history for the repository.

This is where the git log command comes into the picture.

Using Git Log Command

$ git log
This displays the entire commit history. If your commit history is large, it’ll show only a portion of it and you can hit [space] to scroll or type q to quit.

If you want to view only the last 3 commit history, you can use the following command: git log -n 3.
To condense the commit history into a single line and view them, run git log --oneline. This is the easiest way to get a high level overview of all the commit history. It might still be a bit too much if you’ve got a lot of commits.
If you want to view the commit history by a specific author, run git log --author"(author-username)".

  1. Git Merge Once you’re done with development inside your feature branch and tested your code, you can merge your branch with the parent branch. This could be either a develop branch or a master branch depending on the git workflow you follow.

When running a git merge command, you first need to be on the specific branch that you want to merge with your feature branch.

Let’s see how to use the git merge command with an example.

Using Git Merge Command

Imagine you’re currently in your feature branch called feature1 and you’re ready to merge it to the develop branch.

We must first switch to the develop branch using the checkout command.

$ git checkout develop
Before merging, you must make sure that you update your local develop branch. This is important because your teammates might’ve merged into the develop branch while you were working on your feature. We do this by running the pull command.

$ git pull
If there are no conflicts while pulling the updates, you can finally merge your feature1 branch into the develop branch.

We do this by using the git merge command followed by the branch name that we want to merge into our current branch.

this is enough i will write more on next blog if i write here it will be overwhelming so thankyou for keeping up with me so lets meet on next blog.

Top comments (0)