Adding files into git repository

You can use “git add” to add files in your local branch of the git repository. And, use “git commit” to commit them into your local branch. For pushing the commits into the online git repository, you can check the steps here.

Initial Setup

The initial Setup is as shown. Only one file README.md is present.

~/repoone$ ls
README.md

git branch would show the list of branches and current branch. In this case, it is a single branch called main.

~/repoone$ git branch
* main

git status would show the current status of the current branch. It is up to date with originorigin represents a link to the online repository on github.com (https://github.com/codeversionmaster/repoone.git).

~/repoone$ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean

git log –oneline shows a summary of commits. Each commit will list with its number and title in one line.

~/repoone$ git log --oneline
2673111 (HEAD -> main, origin/main, origin/HEAD) Initial commit

git remote will display a list of remote branches and links from the current git repo.

~/repoone$ git remote -vv
origin  https://github.com/codeversionmaster/repoone.git (fetch)
origin  https://github.com/codeversionmaster/repoone.git (push)

Adding files in git – git add

Create sample files and directories.

~/repoone$ mkdir dirone
~/repoone$ touch dirone/fileone
~/repoone$ touch filetwo
~/repoone$ echo "this is file one" > dirone/fileone
~/repoone$ echo "this is file two" > filetwo

git status would show that files are not added into git.

~/repoone$ git status
On branch main
Your branch is up to date with 'origin/main'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        dirone/
        filetwo

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

Add the files using git add. You can use git add . to mean all the files/directories currently not added should be added.

~/repoone$ git add dirone filetwo

git status would show that the files have added into current repository.

~/repoone$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   dirone/fileone
        new file:   filetwo

git commit

Use git commit to create a commit out of the added files. If this is the first time, git would ask for email and name. –global flag sets them not just for this repo but all across the system for any git repos that you would create. For any local settings, drop –global argument and set the settings.

~/repoone$ git commit

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'xyz@abc.(none)')

Set the settings using –global flag. You can also omit the flag and set it only under this repo.

~/repoone$ git config --global user.email "codeversionmaster@gmail.com"
~/repoone$ git config --global user.email "Code Version Master"

On git commit, a screen opens. 

~/repoone$ git commit

On the screen, write the commit message in the first line. You can write the commit message in multiple lines. Keep the width of the line containing the commit message to not exceed the width of the lines below. Keep the second line empty. Press ^X. It would ask to confirm location, press Enter. Press Y when it asks for further confirmation.

In some OS and setups, it may open as a terminal or a vi editor, all you need to do is press “:wq!” like you save a file.

New screen would open as below.

This is commit 1

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch main
# Your branch is up to date with 'origin/main'.
#
# Changes to be committed:
#       new file:   dirone/fileone
#       new file:   filetwo
#

You can do the commit without opening the screen using git commit -m and giving the message as an argument.

git commit -m "This is commit 1"

git commit would show output as shown.

[main 52b0ecf] This is commit 1
 2 files changed, 2 insertions(+)
 create mode 100644 dirone/fileone
 create mode 100644 filetwo

Use git status. You can see that the branch is ahead by one commit.

~/repoone$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

Use git log –oneline to see the status of the new commit added at the top and HEAD pointing to the new commit in your local repository. Note, that this commit is only in your local git repository.

~/repoone$ git log --oneline
52b0ecf (HEAD -> main) This is commit 1
2673111 (origin/main, origin/HEAD) Initial commit

For pushing the commits into the online git repository, you can check the steps here.