Labels

slider

Recent

Navigation

Top GitHub Commands list with Examples for better coding

Find here the top Github commands list with distributed version control systems for a better software development coding experience.
Top GitHub Commands list with Examples for better coding

Introduction

Hi readers! Often, I wish to make some useful blog posts for you and now I have brought significant information about Github commands list which usually helps professional developers a lot.

In professional software development industries, the programming code is written by teams of expert developers. In fact, at present, the MNCs have multiple teams as well as team members across the globe from remote locations. Here, the question arises how could be the task finished on time. The best and easiest approach to writing code in a single codebase and this method is popularly known as the Version Control Systems.

Version Control Systems

Version control systems enable us to write and control various releases and stages of a particular software product without really keeping multiple files or folders. Also, they tend to formulate development within a team more controllable with reduced trouble as developers don't possess trading folders, but instead transmit with an individual source where all the systems’ modifications are occurring and everything is of course saved.

Types of Version Control Systems

Two kinds of Version Control Systems are used in development such Centralized and Distributed version control systems.

1. Centralized Version Control Systems

Centralized version control systems refer to the codebase or the project dwells in one central location. It's also called a repository. For modification, people must use that repository and write their necessary code in it.

2. Distributed Version Control Systems

In distributed systems like Mercurial and git, things are much more adaptable,  because of distribution strategy. This means that everybody has working proofs of the repository that puts track of his/her modifications and the different releases done by him or by his team members.

What is Git hub?

Git hub is a free open source distributed version control system that is built to control everything from various sizes of projects with the fastest speed and efficiency.

How Git hub functions

Most of the version control systems calculate the differences in each file and find the sum of these obtained differences they can achieve whichever version is saved, but that’s not the actual approach Git works.

Git functions as a sequel of snapshots for your whole file system. Every time the alterations made in your files along with the whole system are captured by Git for future reference purposes.

In case, there’s a file that hasn't been changed by you, then it will keep a reference to the snapshot that is taken previously. Let's have a deeper dive into this powerful GIT version control system to know its all features.

GIT hub States

GIT has 3 states available for your files to be kept in while your work is going on with Git. The 3 states are: modified, staged, and committed.

Suggested Reading

Modified state

When a file in the working directory has been updated by you, it is then known as in the modified state, but these changes are not getting stored in the codebase.

Staged state

When you are going to stage the file, the changes made in the modification state are captioned to get saved in the successive commit.

Committed state

When you are going to commit the file, these changes are ultimately stored in your codebase.

Importance of repository and its types

In Git, you can run 2 types of repositories. Generally, you will have a local repository on your desktop or laptop, that is well defined in your local working directory, and a remote repository that gets stored on the cloud.

The local repository is more secure as it gets stored on your local machine safely, until and unless you wish to share your code with others.

Once you started using the remote repository, anyone with authorized access to your repository will be able to download the code from the remote repository to his/her local machine and will contribute to it.

Configuration of Git hub: Getting Started

To get started, just visit https://git-scm.com/ and get downloaded the right version for your existing operating system. As soon as you started running the installation, you need to open Git CMD or Git bash and CD ( change directory command) into your selected empty directory and run all these commands. Replace the “user name” with your git username and “username@example.com” with the email id of your git account.

$ git config --global user.name "username"

$ git config --global user.email - username@example.com

Here, the above ‘git config' is a command that needs your pc to change in config the file in the .git folder of the root of pc. --global is a flag you pass to inform git that this is your global git account and you will be using this account to build up git projects all over the pc. Also, you can use a git account for a particular directory. You can obtain details more about the starting setup and the crucial commands here as follows.

Significant Git Commands

1. GIT status - command

As soon as setup is installed, the GIT status command will be run to know or observe the real status.

Here, you’ll have such an error that seems something like this.

Here error message about

The above type of error message is genuine, as we have not initiated a ‘git repository' within our working directory.

2. GIT init - command

For the initialization of Git, we need to run the ‘git init’ command.

Now, if we will run the ‘git status' command again we will be able to view that we are on branch master (branches in a minute) and understand that it is the first commit, and nothing is there to commit, as presently the project is empty.

GIT Hub command Sample project

Further to understand the use of Git hun commands in a practical approach, let’s create a text file ‘learn.txt' and commence making some alterations to make a real-world project. We have to do such a task inside the visual studio code IDE/editor with C# language.

Now, when we are running the ‘git status' command, an untracked file will be found.

This indicates that we have some content in the modified state inside of the project and the same untracked file hasn't been added yet to the staging location.

3. git add

To add the files which are created and edited in the prior step to the staging location, we have to run the git command: git add learn.txt

Now, ‘Git status' is displaying that files are ready to get committed.

You can also add multiple files to the staging area by running the ‘git add' command and then run the ‘git add' command again to add all files into the modifying location. After this, just run the ‘git status' command again and you’ll see that the text file is labeled as changes to get committed.

4. git commit

Now in this sample project, for the final step, it's time for running the commit command to commit the above alterations. One crucial thing about this commit command though, it often comes with a message, since Git strives to execute best-level practices, and it is not hectic inside a unique project if modifications were being conducted without any word of explanation illustrated. Hence, in this way, we will be able to run the ‘git commit' command:

git commit -m “added learn.txt”

The above command line informs about modification of one file with 3 insertions, insertion of lines we’ve written previously.

Since all the observers read your commit messages, you must know how to write them perfectly: ‘Git Commit Messages

5. git push

Hope now, everything goes fine, but now if we wish to share the above project with any colleagues and bring the same project on board we have to position our project on the cloud to enable other teammates to access the same project. We’re not centralizing it, as everyone is getting his/her working copy, but a focal point is mandatory to pipeline the project modifications. Hence, it's to move and build up a repository on Github.

Now, start creating a new git repository with an appropriate description and a name, I’ll call it LEARN Git.

While the new repository’s page gets opened, you’ll have an option to push a current repository which is precisely what we expect to do.

We'll add local to the remote repository through the URL with the use of the following commands.

Command-1- git remote add origin [repository’s link]
 
Command-2 - git push origin master

The command-1 connects the remote repository to our local repository through the link and under the origin name, and in fact, the name can be changed by you. The command-2 pushes the master branch files to that origin repository. Hence, through the above two commands, our repository would get the text file that is created by us.

6. git clone

In this way, the code is pushed to one remote repository so that, anybody with access with your authorization to download the required code and start modifying it.

Two ways are there to fetch a remote repository to a local machine directory. The first way is to download a zip of the whole project and the second way is you can make a clone or duplicate the repository to any other location on your local system.

For downloading the repository with the complete previous details of the project and nearly every modification that was ever made by us, we need to utilize the following command.


git clone "URL link of the repository"

Just start navigating to the position where you wish to clone or make duplicate the repository and run the following command.

git clone "link of the remote repository"

You don’t have to write the whole command.

Now, find out the repository you prefer to clone (for example. On Github), click the green button named “Clone or download” and you can have two options either  downloading the zip or copying the Url and replacing it in the git command “

            git clone "link of the remote repository"

            repo download and clone options

Now let’s move into a very crucial part of Git. If you’re in work with a colleague on a single project and he makes some modifications to the repository on that same file.

I am opening another instance of Visual Studio C# code and will move to the specific directory where I made a clone of the project. Now, I’m changing the word ‘first’ to ‘modified’ and then repeating the same steps as earlier to push the project, i.e., add, commit, and push directly.

Note: We don't have to run the command ‘git remote add' as we have cloned the repository and already it’s remotely added.

7. git merge and git fetch

Usually, the command ‘git merge' brings the modifications in projects and merges them into our files. ‘Git fetches' command is used to fetch the changes first and then merge the same into our files.

Git permits us to observe the state of files at the time of an earlier commit. If you wish to head to an earlier commit without changing anything you can run the command ‘git checkout "commit hash"’ and pass the proper commit hash. You can see all past commits with hashes through the command ‘git log'.

Also, you can get all these by pressing the commits tab on the GitHub repository.

You can get the hash of your past commit copied from here and keep pasted in the command prompt.

git reset --hard "commit hash"

The above code enforces the remote repository project files to be in the state they were in, after that specific commit, nonetheless of what we have currently.

After running the above command successfully, a message such as ‘one commit is remaining' will be displayed. It means git counted this action as one more commit and some changes have been done by you in the remote repository.

Now, we can fetch the above changes without merging them into the directory and selectively merging by ourselves. To conduct a merge, we have to run the command ‘git fetch' as follows:

git fetch --all

The above command will fetch all the commits that we don’t have currently, but are not required to merge or not disturb our workflow.

8. Branches

Branches are well known as one of the most dominant features in Git.

For instance, if you work on software with various releases, the development of the various versions needn't proceed in the same pipeline. Branching is essentially diverting the flow of modifications in a particular direction.

One more simple example is a production followed by testing, and development branches, where production would only comprise up reliable releases, specific development commits would have to spread out to testing before they get merged with production and henceforth. It’s a much-required procedure in the software world.

Hence, Git is very good at branching as it uses snapshots and estimates differences, the procedure of creating a branch is much easier and faster than other systems like SVN where such operation is a burden.

So let’s initiate a new branch on your project, you can simply work by utilizing the command ‘git branch’ which will move ahead and build a new branch with the name we give.

9. git checkout

We utilize the git checkout command to navigate an existing branch, add any new files, and commit the files.

We can then enter that particular new branch by using the following command:

git checkout "branch name"

You can edit branch ‘testing’ with a modification in the text file and commit all the unstaged files with alterations through the command ‘git commit -a'.

10. Pull request

For the creation of a pull request, first, we have to develop a new branch from the existing branch you desire to merge into. Next, it's time to commit some modifications and push this new branch to Github.

Then you have to click on the “New pull request” button that is available on the repository page and Github will display the following prompt:

Git UI for the pull request

Here, you can choose the targeted branches and can leave a comment. After the creation of a pull request, Github allows you the project director or owner, the option to merge or end the pull request.

If an automatic merge is not possible online, you need to get a pull and apply to merge them locally. Here, Github’s “command line instructions” button helps you out.

11. git stash

This ‘git stash' command can be utilized while we expect to save our job without staging or committing the code to our Git repository and desire to connect between branches.

The command ‘git stash -up' is utilized when we desire to stash the untracked files.

The command ‘git stash pop' is utilized while we are back on the branch that is created by us and wishes to obtain the required code.

12. git revert

The ‘git revert' command can be utilized as an ‘undo’ command. Nevertheless, it is not used as the traditional ‘undo’ function. It determines how to undo the modifications made by the commit and prepared a new commit with the inverse content.

13. git diff

Diffing is such a type of function that includes 2 input datasets and gives outputs the modifications between them. The command ‘git diff'' is used for multi-purpose which, when gets executed, runs a diff operation on Git data sources. These Git data sources can be branches, commits, files, and more. Often, the git diff command is utilized along with the commands like ‘git status' and ‘git log' to assess the present state of our repository.

14. Git log

This Git log command is utilized when we wish to review the log information for each commit in a detailed summary in our repository.

The Git log command will exhibit the log of our branch. We can review the last 3 logs through the command: ‘git log -3’, or we can check on the last 4 logs through the command ‘git log -4’ and many more in this pattern.

Also, the command ‘git log –graph' and ‘git log –graph –pretty=oneline’ are used to display the report in terms of a graph, and the ‘git log' is utilized to obtain the details of the commit IDs.

15. Git rebase

Rebase is the method of moving and integrating a progression of commits to another new base commit. Rebasing is meant for modifying the base of our branch from one commit to another commit, enabling it to function as if we’ve developed our branch from a separate commit. Git achieves this internally by making new commits and implementing them on a particular base. It’s very crucial to comprehend that even though the branch seems the same, it is formulated of absolutely new commits.

The command ‘git rebase' makes an automatic ‘git checkout <branch>’ before performing anything else. Contrarily, it remains on the same existing branch.

Consider a circumstance where we have moved from a master branch and have built a new feature branch, but the master branch is still possessing more commits. We need to obtain the latest version of the master branch in our new branch, retaining the history of our branch clean so that it occurs as if we are working on the recent version of the master branch.

Note: We need not rebase the public history and also, we should not rebase commits forever once they get pushed to a public repository. This happens because the rebase would displace the old commits with the new commit, and it appears as if a portion of project history got unexpectedly disappeared.

16. git reset

We utilize the command ‘git reset –hard [SOME-COMMIT]’ to return the working tree to the previously committed state.

This will exclude commits that are done in a private branch or remove away the uncommitted modifications!

If we apply the command ‘git reset –hard [SOME-COMMIT]’, then Git will:

return our existing branch (maybe master) to the required point of commit <SOME-COMMIT>.

Create the files in the working tree of us and the index (“staging location”) similar to the versions committed at the point <SOME-COMMIT>.

Summary

Overall, Git has become a crucial part of our daily development strategy. Github Version control is a necessity of most software development and a regular practice for developers. GIT Hub command list is a source of tool that you’ll  need for sure and the only ideal approach to get better with it is to utilize it.

Share

Anjan kant

Outstanding journey in Microsoft Technologies (ASP.Net, C#, SQL Programming, WPF, Silverlight, WCF etc.), client side technologies AngularJS, KnockoutJS, Javascript, Ajax Calls, Json and Hybrid apps etc. I love to devote free time in writing, blogging, social networking and adventurous life

Post A Comment:

0 comments: