Getting Started with Git

Before getting started with Git commands lets go through some git terminology

Installing Git

  • For CentOS or RHEL
$ sudo yum install git
  • For Debian based , such as Ubuntu
$ sudo apt install git
  • Check the version of Git which is installed
$ git --version 


Creating Github Account

  • Create an Github account here

Setting up Git

  • After installing Git , first thing you are supposed to do is provide your identity by setting username and email address
$ git config --global user.name "<< your_name >>"
$ git config --global user.email "<< your_email >>"
  • If you pass the --global option, then Git will always use that information for anything you do on that system and If you don’t want this to happen override this with a different name or email address for specific projects, you can run the command without the --global option when you’re in that project.

Working on git :

1. Creating Local git Repository

Open the terminal, change your directory where you want your project to be placed.

$ mkdir test_dir
$ cd test_dir

2 . Initializing git repository

Command git init transforms the current directory into a Git repository. This adds a .git subdirectory to the current directory and makes it possible to start recording revisions of the project.

$ git init 


3 . Adding file to repository

Lets create a file  test1

$ touch test1

Now check whether the file you created is included in git or not by using command git status.

The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git.

$ git status


It shows a file created “test1” but it has not been added to commit, it says to use “git add”.

To add a file to a commit, you first need to add it to the staging area. The staging area is a simple file, generally contained in your Git directory, that stores information about what will go into your next commit. It’s sometimes referred to as the index, but it’s becoming standard to refer to it as the staging area.

$ git add test1

Now again check git status, it shows a new file added test1 but it is not added to commit. Hence it says “Changes to be committed”

$ git status


4 . Creating a commit

To create a commit use the command :

$ git commit -m "this a test commit" 
<< or >>
$ git commit

git commit command opens a default text editor, where you can write about your commit and then save.



5. Git log

git log lists the commits made in that repository in reverse chronological order; that is, the most recent commits show up first.



6. Git stash

Git stash is a command that allows you to store local modifications you have made in your working directory and go back to a clean directory.

To understand about stash, let’s create another file test2 , add to git but before commit we will use git stash command.

$ touch test2
$ git add test2
$ git stash


We will repeat the same thing with file test3 and test4





let’s check if we have any existing stashes.

$ git stash list

The list option shows all of the existing stashes.



To restores the file back to your directory you can use following commands:

$ git stash pop

It will remove the most recent stash from the list and apply it to the current working directory



or

Check the stash list and you can specify the pop stash using the command :

$ git stash pop stash@{0}


or

$ git stash apply

Just like stash pop, stash apply will choose the recent stash and then it will apply to current working directory, but it doesn’t drop it from the list of stashes.



Even after restoring test2 file it shows in the stash list.



or

$ git stash apply stash@{0}

Again you can specify your stash using the command above.

we have restored all the three files test2, test3 and test4 so now you can proceed to make commits.

To learn more about stash commands :

$ git stash --help