Git commands to get you started with programming.

Git commands to get you started with programming.

Some of the most useful git commands to get you started with programming.

Hey Guys welcome to my blogs, this is my second article from the series Basics of Git if you have not read my first article please do read it. I have started this series targeting the student and the guys new to modern programming.

In this article, I will be explaining the basic git commands and their uses, these commands are widely used by all the developers across the globe to do code collaboration and versioning. Before going further I do recommend you to read my previous article about the What is Git? How does it work? to get the basics idea about Git.

There are plenty of Third-Party Tools that provide a Rich User Interface for git operation, which makes developers' life easy, but unless and until you are not sure about what a specific operation does, you will not be able to take advantage of that UI-friendly operation tool.

I personally prefer the command-line tool over GUI, because it is short and clean and does not require any additional tool installation, and eats up less system memory. Understanding about working and use of these commands can save you a lot of time.

config

This allows you to query/add/update the configuration option for your workspace or repository, the syntax is quite simple you need to provide a config name followed by the value(add/update)

git config config.name config.value

The most basic example config command is setting user name and email, so that can be visible in the commit you can easily do it by typing the following command,

git config user.name "John Doe"
git config user.email "john.doe@getnada.com"

This will set the user name and email configuration for your current repository, If you would like we can set it globally using --global option. You can fetch a specific config using the following command,

git config --global user.name
// John Doe

clone

Well, one of the most basic commands is clone, this allows you to download a remote repository onto your system,

git clone "PATH-TO-REMOTE-REPO"

After successful execution, this command will download the repository into a newly created directory under the remote repository name, It will also create remote-tracking branches for each branch in the cloned repository.

fetch

After cloning a repository you would like to execute the fetch command it fetches branches, tags collectively refs from one or more repositories.

git fetch

Fetch command fetches data from either a single repository or group of repositories, you will need to specify that group in the git config file using config command.

branch

In my first article from this series I explained to you about the branches in the git version control system, the branch command is used to add delete and list those branches.

You can easily list all the branches available in the repository, by default it will show you a list of all local branches,

git branch --list
// all the local branches

If you wish to see a list of all remote branches you can use -r option with the git branch --list command and to all of them together(local + remote branches) you can use -a option with it.

git branch --list -r
// all the remote branches
git branch --list -a
// all the remote + local branches

Fun Fact: In the list, the currently used branch will be green in color with an asterisk.

checkout

The checkout command allows you to switch the branches or update the current working tree with the version index or specified tree. Let's see the syntax, then I will explain to you the command

git checkout -b <branch> --track <remote>/<branch>

The first part git checkout -b <branch> will create and switch to a branch with the given name, if the --track <remote>/<branch> will set newly created branch to track remote repo or branch. The -b tells to create and switch to that branch if that is not provided, will switch to the existing branch if present.

stash

The stash command will basically allow you to record or take a dirty backup of your current changes in the repository and update it with the HEAD. If you can clear all your local modifications store them and revert back to the clean working directory you can use this command. The syntax is as follows,

git stash

The modification stored away can be seen with git stash list can be inspected with git stash show and can be restored with git stashapply`,

git stash
git stash list
git stash show <stash>
git stash apply <stash>

When you create a stash you can provide a message to differentiate between all the available stash. They are named like stash{0}, stash{1} ... stash{n} with stash{0} as the latest you need to provide that name in place of <stash>.

git stash apply and pop works similarly with one difference, the pop will restore the stash and then deletes it, whereas apply restores it as well as keeps it for future uses

status

The status command allows you to see the status of your current working directory, this will give you the addition/deletion to the current directory with respect to its HEAD Also gives the newly added or untracked files that are not present under the current index. You can simply execute it,

git status

the output will be,

➜  dummy project git:(feature-one) ✗ git status                         
On branch feature-one
Your branch is up to date with 'origin/feature-one'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   src/resources.js

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:   src/ban.js

The first ones (Changes to be committed) are the changes that are staged and can be committed using git commit and the second ones (Changes not staged for commit) are not staged and can be committed using git add followed by git commit.

clean

This will allow you to clean your current working directory and restore it to the HEAD, It cleans the working tree by recursively removing files that are not under version control, starting from the current directory.

git clean

Generally, unknown files are removed, but if you provide -x option then ignore files can be removed.

These are some generally used commands with some of the most used options but git provides you bunch of other useful commands and options that can be used. You should also check out the official git documentation for further details.

That is it for this article, Thank you for reading. To get notified about my next article do follow me, till then You can connect me on Twitter or DM on Discord.

Did you find this article valuable?

Support Adarsh Thakur by becoming a sponsor. Any amount is appreciated!