GitHub

GitHub— basic commands

In this article I will share with you some basic commands of GitHub which developers needs to be aware of. You need to have installed git tool in your system to try it.

linuxuserind
4 min readMar 26, 2022
© git and GitHub

GitHub is a Software Development and Version Control Open Source tool for maintaining distributed version control of the source code of application written in any language. GitHub uses Git for providing features of Version Control and Source Control Management(SCM).

Using GitHub developers can share source code of their Open Source product across globe which allows other GitHub users to View or Suggest changes to the content or reuse the source code for developing other tool.

Basic operations on GitHub

We will be covering the git commands to perform below operations on your GitHub repository from your local or development system:

  1. Add GitHub configuration values in git tool (globally or directory level),
  2. Setup Repository in your local machine from GitHub quickly,
  3. Create new empty git repository locally,
  4. Clone a git repository locally,
  5. Add files for commit,
  6. Push/Checkin your changes,
  7. Check git status and log,
  8. Pull/GetLatest of repository.

In all the sample commands please replace {username}, {organisation} and other placeholders with your username, github organization names before executing.

Add GitHub configuration in git

Once your successfully installed git tool in your machine or when you want to work on GitHub repositories in local you have to set configuration values using certain git commads so that your GitHub account is virtually connected with local git.

git allows users to set these configuration values globally and local directory level in your file system. If you set GitHub config globally in your system, then the git tool will use it in all directories of your file system when you initialize git. Configuring GitHub at local directory level will make values only for that directory and when you initialise git in another directory you have to setup GitHub for that directory.

Setup Repository in your local machine from GitHub quickly

Once you created new empty repository in GitHub, next step is to add files to the empty repo. To add files from the local machine to GitHub follow the below

Create new empty git repository locally

git init {repository-name}

Create New Local git repo without .git directory

git init — bare {repository-name}

First command will create a directory with repository-name with .git folder inside it.

Clone a git repository locally

Add files for commit

Once you initialize and cloned the existing code from GitHub for making changes to single or multiple files you have to push the changes you done to GitHub. So the first step to be followed before performing push is add the files to git as it keeps track of file changes in the local repo

git add newfile.txt

Once modified or new files are added, next step is to commit the changes to git,

git commit

Once the above commit is executed, Default Text Editor will be opened by to add message for this commit. Developers usually provide precise to detailed information on the changes done with this commit and complete the commit.

To provide message with commit command use the below command,

git commit -m ”CommitMessage goes here”

Push/Checkin your changes

Post commit command, the changes will not be moved to GitHub automatically. We need to execute the below Push command to move code to GitHub,

git push

If we want to specify the remote branch explicitly, instead, we should use the following syntax:

git push <remote> <branch>

Check git status and log

git status

When the above command is executed inside git initiated directory, the changes done on the files inside the directory will be listed. Like Newly created, Updated and Deleted file list will be displayed which are yet to be added for commit.

git log

The above command will list all the commits happened in the repository with the messages included with unique commit id.

Pull/GetLatest of repository

To get the latest code changes done at GitHub repository in your local machine, below command can be used.

git pull
(or)
git pull origin master

Other Commands

Reset the author information on your last commit:

git commit — amend — reset-author

Do Diff for verifying changes in local

git diff

Stash everything that you’ve changed, pull all the new stuff, apply your stash.

git stash 
git pull
git stash pop

Conclusion

With the commands discussed above, you can do basic operation on GitHub from command line. Of course you can do all these steps from IDE itself once you configured Git as Version Control tool in IDEs like Visual Studio, VS Code.

Its always good to learn basic commands as we cant guarantee that IDEs will be available in all machines where you work in future due to Licensing or rights to installation. If git is not available we can always install it without much strain.

linuxuserind

Console.WriteLine(“Happy Coding”);
<a href="linuxuserind.medium.com">Follow me</a>

--

--

linuxuserind

Avid Learner of new technologies and interested on trending practices