Thursday, 27 December 2018

GIT: Performing a local Git workflow via the command line

In this exercise, you learn how to create and work with a local Git repository.
Open a command shell for the operations. Some commands are Linux specific, e.g., appending to a file or creating a directory. Substitute these commands with the commands of your operating system. The comments (marked with #) before the commands explain the specific actions.

10.1. Create a directory

The following commands create an empty directory which is used later in this exercise to contain the working tree and the Git repository.
# switch to the home directory
cd

# create a directory and switch into it
mkdir repo01
cd repo01

# create a new directory
mkdir datafiles

10.2. Create a new Git repository

You now create a new Git repository with a working tree.
Every Git repository is stored in the .git folder of the directory in which the Git repository has been created. This directory contains the complete history of the repository. The .git/config file contains the configuration for the repository.
Use the git init command to create a Git repository in the current directory. Git does not care whether you start with an empty directory or if it contains already files.
# you should still be in the repo01 directory
cd ~/repo01

# initialize the Git repository
# for the current directory
git init
All files inside the repository folder, excluding the .git folder, are the working tree for a Git repository.

10.3. Create new content

Use the following commands to create several new files.
# switch to your Git repository
cd ~/repo01

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

# create a few files with content
ls > test01
echo "bar" > test02
echo "foo" > test03

10.4. See the current status of your repository

The git status command shows the status of the working tree, i.e. which files have changed, which are staged and which are not part of the staging area. It also shows which files have conflicts and gives an indication what the user can do with these changes, e.g., add them to the staging area or remove them, etc.
Run it via the following command.
git status
The output looks similar to the following listing.
On branch master

Initial commit

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

    datafiles/
    test01
    test02
    test03

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

10.5. Add changes to the staging area

Before committing changes to a Git repository, you need to mark the changes that should be committed with the git add command. This command allows adding changes in the file system to the staging area. It creates a snapshot of the affected files. You can add all changes to the staging area with the . option or changes in individual files but specifying a file pattern as option.
# add all files to the index of the Git repository
git add .
Afterwards run the git status command again to see the current status. The following listing shows the output of this command.
On branch master

Initial commit

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

10.6. Change files that are staged

In case you change one of the staged files before committing, you need to add the changes again to the staging area, to commit the new changes. This is because Git creates a snapshot of the content of a staged file. All new changes must again be staged.
# append a string to the test03 file
echo "foo2" >> test03

# see the result
git status
Validate that the new changes are not yet staged.
On branch master

Initial commit

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 the new changes to the staging area.
# add all files to the index of the Git repository
git add .
Use the git status command again to see that all changes are staged.
On branch master

Initial commit

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

10.7. Commit staged changes to the repository

After adding the files to the Git staging area, you can commit them to the Git repository with the git commit command. This creates a new commit object with the staged changes in the Git repository and the HEAD reference points to the new commit. The -m parameter (or its long version: --message) allows you to specify the commit message. If you leave this parameter out, your default editor is started and you can enter the message in the editor.
# commit your file to the local repository
git commit -m "Initial commit"
Git also offers a mode that lets you choose interactively which changes you want to commit. After you quit the mode you will be asked to provide a commit message in your $EDITOR.
git commit --interactive

10.8. Viewing the Git commit history

The Git operations you performed have created a local Git repository in the .git folder and added all files to this repository via one commit. Run the git log command to see the history.
# show the Git log for the change
git log
You see an output similar to the following.
commit 30605803fcbd507df36a3108945e02908c823828
Author: Lars Vogel <Lars.Vogel@vogella.com>
Date:   Mon Dec 1 10:43:42 2014 +0100

    Initial commit

10.9. Viewing the changes of a commit

Use the git show command to see the changes of a commit. If you specify a commit reference as third parameter, this is used to determine the changes, otherwise the HEAD reference is used.

10.10. Review the resulting directory structure

Review the resulting directory structure. Your directory contains the Git repository as well as the Git working tree for your files. This directory structure is depicted in the following screenshot.
First Git directory

10.11. Remove files

If you delete a file, you use the git add . command to add the deletion of a file to the staging area.
# remove the "test03" file
rm test03
# add and commit the removal
git add .
# if you use Git version < 2.0 use: git add -A .
git commit -m "Removes the test03 file"
Alternatively you can use the git rm command to delete the file from your working tree and record the deletion of the file in the staging area.

10.12. Revert changes in files in the working tree

Use the git checkout command to reset a tracked file (a file that was once staged or committed) to its latest staged or commit state. The command removes the changes of the file in the working tree. This command cannot be applied to files which are not yet staged or committed.
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 checkout test02

# unwantedstaged.txt is not tracked by Git simply delete it
rm unwantedfile.txt
If you use git status command to see that there are no changes left in the working directory.
On branch master
nothing to commit, working directory clean
Use this command carefully. The git checkout command deletes the unstaged and uncommitted changes of tracked files in the working tree and it is not possible to restore this deletion via Git.

10.13. Correct the changes of the commit with git amend

The git commit --amend command makes it possible to rework the changes of the last commit. It creates a new commit with the adjusted changes.
The amended commit is still available until a clean-up job removes it. But it is not included in the git log output hence it does not distract the user. See git reflog for details.
Assume the last commit message was incorrect as it contained a typo. The following command corrects this via the --amend parameter.
# 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 use the git --amend command only for commits which have not been pushed to a public branch of another Git repository. The git --amend command creates a new commit ID and people may have based their work already on the existing commit. If that would be the case, they would need to migrate their work based on the new commit.

10.14. Ignore files and directories with the .gitignore file

Create the following .gitignore file in the root of your Git directory to ignore the specified directory and file.
cd ~/repo01
touch .gitignore
echo ".metadata/" >> .gitignore
echo "doNotTrackFile.txt" >> .gitignore
The above command creates the file via the command line. A more common approach is to use your favorite text editor to create the file. This editor must save the file as plain text. Editors which do this are for example gedit under Ubuntu or Notepad under Windows.
The resulting file looks like the following listing.
.metadata/
doNotTrackFile.txt

10.15. Commit the .gitignore file

It is good practice to commit the .gitignore file into the Git repository. Use the following commands for this.
# add the .gitignore file to the staging area
git add .gitignore
# commit the change
git commit -m "Adds .gitignore file"

0 comments:

Post a Comment