Git: execute local git workflow from the command line

In this exercise, use the Git command line to create a local Git repository and commit to it.

Open the command shell of the operation.

Some commands are Linux specific, such as attaching to files or creating directories. Replace these commands with those of the operating system. The notes before the command (marked with #) explain the specific operation.

Create directory

The following command creates an empty directory that will be used later in this exercise to contain the work tree and Git repository.

# switch to a directory of your choice and afterwards
# create a directory named "repo01" and switch into it
mkdir repo01
cd repo01

# create a new directory
mkdir datafiles

Create some files

Use your favorite text editor to create the following file and directory structure in the current folder.

Data file / data txt

Test 01

Test 02

Test 03

You can also create these files through the command line. For example, the following command creates these files on Linux through the command line.

# ensure that you are in your Git Git repository

# create an empty file in a new directory
touch datafiles/data.txt
touch test01
touch test02
touch test03

Create a new Git warehouse

Use this command to create a new local git repository in the created directory. Git doesn't care whether you start with an empty directory or have included files. git init

# initialize the Git repository for the current directory
git init

All files in the repository folder (excluding this folder) are working trees git

View the current status of the warehouse

View the status of the repository with the following command.

git status

The output is similar to the following listing.

On branch master

Initial commit

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


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

Add changes to staging area

Notify Git that all new files should be added to the Git repository using this command. git stage

# add all files to the index of the Git repository
git stage .
# if stage is not available use git add instead, stage was added around 2020 to the git command line

Then run the command again to see the current status. The following listing shows the output of this command. git status

On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
    new file:   datafiles/data.txt
    new file:   test01
    new file:   test02
    new file:   test03

Change the temporary file

Adjust existing files.

# append a string to the test03 file
echo "foo2" >> test03

Verify that the new changes have not been staged.

On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   datafiles/data.txt
    new file:   test01
    new file:   test02
    new file:   test03

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   test03

Add new changes to the staging area.

# add all files to the index of the Git repository
git stage .

Use this command again to see if all changes have been staged. git status

On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   datafiles/data.txt
    new file:   test01
    new file:   test02
    new file:   test03

Commit staging changes to the repository

Commit staging changes to the Git repository.

# commit your files to the local repository
git commit -m "Initial commit"

View submission history

The submit operation creates a new version of the file in the local repository within the folder. Run this command to view the history gitgit log

# show the Git log for the change
git log

You will see output similar to the following.

commit dbbd83bffddb8b9129f37912338011bb82927d0e (HEAD -> master)
Author: Lars Vogel <>
Date:   Mon Feb 8 21:53:12 2021 +0100

    Initial commit

View submitted changes

Use this command to view the submitted changes. If the commit reference is specified as the third parameter, it is used to determine the change, otherwise the HEAD reference is used. git show

Delete file

Delete the file. Use this command to temporarily delete for the next commit. git stage .

# remove the "test03" file
rm test03
# add and commit the removal
git stage .
git commit -m "Removes the test03 file"

Alternatively, you can use this command to delete a file from the work tree and record the deletion of the file in the staging area. git rm

8.11. Restore changes in files in the work tree
Use this command (or in older Git command-line tools) to reset tracked files (once staged or committed files) to their latest staged or committed state. git resetgit checkout

Restore the deleted file by checking out the last version (HEAD~1) before the current submission.

git checkout HEAD~1 -- test03

Check out the status and submit the file again.

git status
git commit -m "Adding test03 back"

You can also replace the contents of a file with its final stage version or a version in submission.

In the following example, you will reset some changes in the work tree.

echo "useless data" >> test02
echo "another unwanted file" >> unwantedfile.txt

# see the status
git status

# remove unwanted changes from the working tree
# CAREFUL this deletes the local changes in the tracked file
git restore test02

# unwantedstaged.txt is not tracked by Git simply delete it
rm unwantedfile.txt

If you use the command, you will see that there are no changes left in the working directory. git status

On branch master
nothing to commit, working directory clean

Please use this command with caution. This command will delete changes to the tracking file (a file known to Git) in the working tree, and this deletion cannot be restored through Git. git reset

Using git corrections to correct commit changes

This command can rework the last committed changes. It will create a new commit with the adjusted changes. git commit --amend

The modified submission is still available until it is deleted by the cleanup job. But it is not included in the output, so it will not distract the user. git log

Suppose the last submission message is incorrect because it contains spelling errors. The following command corrects this problem with parameters. – amend

# assuming you have something to commit
git commit -m "message with a tpyo here"
# amend the last commit
git commit --amend -m "More changes - now correct"

You should only use this command for submissions of public branches that have not been pushed to another Git repository. This command will create a new submission ID, and the user may have based their work on the existing submission. If this is the case, they need to migrate their work according to the new submission. git --amendgit --amend

8.13. Ignore with Files and directories of gitignore files
Create the following files in the root directory of the Git directory to ignore the specified directories and files gitignore

cd ~/repo01
touch .gitignore
echo ".metadata/" >> .gitignore
echo "doNotTrackFile.txt" >> .gitignore

The above command creates a file from the command line. A more common method is to create a file using your favorite text editor. This editor must save the file as plain text. For example, the editor to do this is gedit under Ubuntu or Notepad under Windows.

The generated file is similar to the following listing.


Submit gitignore file

It is best to submit the file to the Git repository. To do this, use the following command gitignore

# add the .gitignore file to the staging area
git stage .gitignore
# commit the change
git commit -m "Adds .gitignore file"

Tags: git github

Posted by achilles on Thu, 14 Jul 2022 12:50:48 +0930