Previous chapter we have created our new clean repository but we did not added any code. In this chapter we will add some code and create some revision or versions of the code.
Getting Status of Repository
As we stated before we have a clean repository we can check this.
$ git status On branch master Initial commit nothing to commit (create/copy files and use "git add" to track)
As we see status
command provides some brief information about repository. We are on the master branch. We will look branches in the future. We have nothing to commit. Sure we have not added anything for now.
Adding Files to the Revision
Git have some fundamental steps to commit. We will look them through writing our app. Below we can find the natural flow of a commit.
Working Copy
We will create our app with the following code.
$ echo 'print("Hello Poftut")' > main.py
We have created a file main.py
with single line code.
Review Changes
We have added some code to our project and want to see status of working copy. Working copy is currently used snapshot of the files.
$ git status On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) main.py nothing added to commit but untracked files present (use "git add" to track)
As we see mail.py is under untracked files. Untracked file is a files that is not added to the commit operation. So If we want to commit our file we should add main.py into tracked files.
$ git add main.py $ git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: main.py
We have added main.py file to the tracking files. After adding file if we look to the status of working directory we can see that main.py is tagged as new file and there is no other untracked file.
Review Status Shortly
We can review status more shortly than default one.
$ git status -s M main.p
status
show the current working status.-s
show status shortly please
Show Diff of Changes
We may want to see more detailed information about like the diff of the changes. Here is the solution.
$ git diff diff --git a/main.py b/main.py index 7865a88..9bce127 100644 --- a/main.py +++ b/main.py @@ -1 +1,2 @@ print("Hello Poftut") +print("Current version 3")
This is the code what is changed.
Skipping Staging
Staging can be skipped with simple -a
parameter like below.
$ git commit -a -m"Without staging" [master ca49f23] Without staging 1 file changed, 1 insertion(+)
-a
parameter make to skip staging which means no previousadd
needed
Commit Changes
We issue same commit command for git.
$ git commit
And we get following screen.
Start # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # On branch master # # Initial commit # # Changes to be committed: # new file: main.py #
Following screen show information about commit and wants some words about commit from us. We add Start to the beginning of the screen and then close it.
[master (root-commit) 1156333] Start 1 file changed, 1 insertion(+) create mode 100644 main.py
And finally we commit our main.py . Was it hard? For now may be but after practicing it this is very simple procedure.
Commit Again
We have worked on our project and added README.md
file, changed the main.py
to version 2. Now we have to commit again.
As we always do we issue a git status
command to see current situation.
$ git status On branch master 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: main.py Untracked files: (use "git add <file>..." to include in what will be committed) README.md no changes added to commit (use "git add" and/or "git commit -a")
As we see that REAME.md is not tracked if we issue commit
command. We need to it to the tracked files like below.
$ git add READMe.md $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: README.md 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: main.py
We have added the README file. So we can continue to commit.
$ git commit -m"Version 2, Add README" [master 22dc9ad] Version 2, Add README 1 file changed, 1 insertion(+) create mode 100644 README.md
We have commited more easily than before because we have provided the message
.
Recommit and Change Last Commit
Some times we hurry up and make mistakes while committing. Here is the solution we can recommit our last commit
$ git add README.md $ git commit --amend [master 1cbc0b2] Gitignore Readme Date: Mon Oct 10 14:47:53 2016 +0000 3 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .gitignore rename main.py => main2.py (100%) $ git log -1 commit 1cbc0b2deb791334886063d4d36a3d1657d387e6 Author: John Doe <jdoe@poftut.com> Date: Mon Oct 10 14:47:53 2016 +0000 Gitignore Readme
--amend
used to update the last commit and new changes are added to the last commit.
Unstaging a File
Unstaging is removing a file from stage and it will be untracked.
$ vim LICENSE $ git add LICENSE $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: LICENSE $ git reset HEAD LICENSE Unstaged changes after reset: M LICENSE $ git status On branch master 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: LICENSE no changes added to commit (use "git add" and/or "git commit -a")
reset
clears specified file from specified HEADHEAD
is current staged files statusLICENSE
the file for unstaging