How to create files in your git repository and commit them?



  • git man page: http://bit.ly/2nvGNWB

    Create a file in your repository.

    $ cd git/mygitrepo
    $ vim README.md
    

    Insert text such as:
    First Line: Today i woke up and went running
    Save the file.

    $ git status
    On branch master
    
    Initial commit
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
    	README.md
    
    nothing added to commit but untracked files present (use "git add" to track)
    

    As you can see the status is untracked git does not know about this file until you add it to the repository

    $ git add README.md
    

    Another git status shows:

    $ git status
    On branch master
    
    Initial commit
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
    
    	new file:   README.md
    

    Now git repository does know about the file, and is a new file in the staging area, but not yet save as a commit.

    $ git commit -m "Initial commit of repository"
    [master (root-commit) 2561e02] Initial commit of repository
     1 file changed, 1 insertion(+)
     create mode 100644 README.md
    

    Check the status now

    $ git status
    On branch master
    nothing to commit, working directory clean
    

    To show what you just did run this:

    $ git log
    commit 2561e02cc0914858fca41b990d454200c887614a
    Author: Jim Beam <[email protected]>
    Date:   Fri Nov 4 20:29:58 2016 +0000
    
        Initial commit of repository
    

    Do not be scared about the long number it's the commit number, each commit get a new number, it's used to reference commit when needed.

    Make a change to the README.md file add another line

    "Second Line: When I get back from running I'm tired"

    $ vim README.md
    

    Add the second line, save the file and run git status

    $ 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:   README.md
    
    no changes added to commit (use "git add" and/or "git commit -a")
    

    It says "Changes not staged for commit", mean that it already know about the file, but changes were made which are not saved "commited" to the git repository, so let's update the file in git and commit it (save it)

    $ git add README.md
    $ git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
    	modified:   README.md
    $ git commit -m "Added a second line to my file"
    [master dbaade7] Added a second line to my file
     1 file changed, 2 insertions(+)
    

    Check the what you just did, git log show in reverse order the history of what you did.

    $ git log
    commit dbaade7470cc5f4c613cc01eaea61fdf23b47ef7
    Author: Jim Bean <[email protected]>
    Date:   Fri Nov 4 20:44:14 2016 +0000
    
        Added a second line to my file
    
    commit 2561e02cc0914858fca41b990d454200c887614a
    Author: Jim Bean <[email protected]>
    Date:   Fri Nov 4 20:29:58 2016 +0000
    
        Initial commit of repository
    

    A short version of the above is:

    $ git log --oneline
    dbaade7 Added a second line to my file
    2561e02 Initial commit of repository
    

Log in to reply
 

© Lightnetics 2024