The Programmers Book

Git commands


Check the available files .

nagaraju@nagaraju:~/workspace$ ls
files scalaworkspace sparknowtest_2.12-0.1.jar sparkworkspace

Make a directory

nagaraju@nagaraju:~/workspace$ mkdir firstGitHub

After creating the directory , now check the list.

nagaraju@nagaraju:~/workspace$ ls
files firstGitHub scalaworkspace sparknowtest_2.12-0.1.jar sparkworkspace

Now , go to the created directory.

nagaraju@nagaraju:~/workspace$ cd firstGitHub/

Initialize the git in that folder.

nagaraju@nagaraju:~/workspace/firstGitHub$ git init
Initialized empty Git repository in /home/nagaraju/workspace/firstGitHub/.git/

Add github url as origin

nagaraju@nagaraju:~/workspace/firstGitHub$ git remote add origin “https://github.com/theprogrammersbook/GitCommands.git”
nagaraju@nagaraju:~/workspace/firstGitHub$ ls

Pull the github url (here we have given name as orgin) and the branch is master.

nagaraju@nagaraju:~/workspace/firstGitHub$ git pull origin master
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/theprogrammersbook/GitCommands

branch master -> FETCH_HEAD
[new branch] master -> origin/master

Now check the list .

nagaraju@nagaraju:~/workspace/firstGitHub$ ls
README.md

Create a file and add some data to that file.

nagaraju@nagaraju:~/workspace/firstGitHub$ touch FirstFile.txt
nagaraju@nagaraju:~/workspace/firstGitHub$ sudo gedit FirstFile.txt

Check the git status .

nagaraju@nagaraju:~/workspace/firstGitHub$ git status
On branch master
Untracked files:
(use “git add …” to include in what will be committed)

FirstFile.txt

nothing added to commit but untracked files present (use “git add” to track)

Add a file to git .

nagaraju@nagaraju:~/workspace/firstGitHub$ git add FirstFile.txt

After the above operation , check the status.

nagaraju@nagaraju:~/workspace/firstGitHub$ git status
On branch master
Changes to be committed:
(use “git reset HEAD …” to unstage)

new file:   FirstFile.txt

If we cant to clean the git .

nagaraju@nagaraju:~/workspace/firstGitHub$ git clean

Adding some more files.

nagaraju@nagaraju:~/workspace/firstGitHub$ touch SecondFile.txt
nagaraju@nagaraju:~/workspace/firstGitHub$ sudo gedit SecondFile.txt
nagaraju@nagaraju:~/workspace/firstGitHub$ touch ThrirdFile.txt
nagaraju@nagaraju:~/workspace/firstGitHub$ sudo gedit ThirdFile.txt

Check the git status

nagaraju@nagaraju:~/workspace/firstGitHub$ git status
On branch master
Changes not staged for commit:
(use “git add …” to update what will be committed)
(use “git checkout — …” to discard changes in working directory)

modified:   FirstFile.txt

Untracked files:
(use “git add …” to include in what will be committed)

SecondFile.txt
ThirdFile.txt

SecondFile.txt
ThirdFile.txt
no changes added to commit (use “git add” and/or “git commit -a”)

To add multiple files to git use the following command.

nagaraju@nagaraju:~/workspace/firstGitHub$ git add -A
nagaraju@nagaraju:~/workspace/firstGitHub$ git status
On branch master
Changes to be committed:
(use “git reset HEAD …” to unstage)

modified:   FirstFile.txt
new file:   SecondFile.txt


modified:   FirstFile.txt
new file:   SecondFile.txt

we are going to commit the file with the following options.

  • -a => Interactive mode.
  • -m => messeage.
nagaraju@nagaraju:~/workspace/firstGitHub$ git commit -a -m “adding 3 files together”
[master 7478a1f]

adding 3 files together
3 files changed, 3 insertions(+)
create mode 100644 SecondFile.txt
create mode 100644 ThirdFile.txt
nagaraju@nagaraju:~/workspace/firstGitHub$ git log
commit 7478a1f5ad7b1f82dee3ebacef69f29b461d7030 (HEAD -> master)
Author: The Programmers Book theprogrammersbook@gmail.com
Date: Fri Oct 18 19:45:41 2019 +0530

adding 3 files together
commit fe71288f986518f7b5ef7ffe918909d356fe1117
Author: The Programmers Book theprogrammersbook@gmail.com
Date: Fri Oct 18 19:40:39 2019 +0530

Adding First File
commit c89d806af63a3443bc4519ddcc784e6e07b81106 (origin/master)
Author: theprogrammersbook <53116635+theprogrammersbook@users.noreply.github.com>
Date: Fri Oct 18 19:30:34 2019 +0530

Initial commit

Now push the code to git.

nagaraju@nagaraju:~/workspace/firstGitHub$ git push origin master

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *