How to Use Git the Version Control System

Updated by Angel Guarisma

Contribute on GitHub

View Project | View File | Edit File

Git is a version control system that can be used to manage software projects. This guideā€™s six steps will show you how to initialize a Git repository, stage files for a commit, and commit these files to a local Git repository. For fuller instruction, refer to our more robust guide on Git Source Control Management.

  1. Create a folder in which to store your files, then initialize a Git repository in that folder:

    1
    2
    3
    mkdir testgit 
    cd testgit
    git init
    
  2. Create files for Git to track, then append some text to a file:

    1
    2
     touch file.txt file2.txt file3.txt
     echo "hello Linode" >> file.txt
    
  3. Use command, git status, to return information about the current Git repository:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    git status
    On branch master
    
    Initial commit
    
    Untracked files:
     (use "git add <file>..." to include in what will be committed)
      
    file.txt
    file2.txt
    file3.txt
    			
    nothing added to commit but untracked files present (use "git add" to track)
    
  4. Since file.txtcontains text, you want Git to track any future changes to that file. Use git add file.txt to add file.txt to the list of files Git monitors. Type git status after the addition to confirm that Git is tracking the new file.

    1
    2
     git add file.txt
     git status
    

    This will return:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     On branch master
    
     Initial commit
    
     Changes to be committed:
     (use "git rm --cached <file>..." to unstage)
      
     new file:   file.txt
      
     Untracked files:
         (use "git add <file>..." to include in what will be committed)
    	
         file2.txt
         file3.txt
    
  5. To commit the changes of file.txt to the version control system, use git commit. Git requires you to write a commit message, a message will help you remember the changes you have made to your files. In this example, use the -am options to commit all modified files, specifically the ones Git is tracking, and include a commit message:

    1
     git commit -am "Added Hello Linode to file.txt"
    

    Git will return the following message, confirming your new changes:

    1
    2
    3
     [master (root-commit) e8cc496] added new file
     1 file changed, 1 insertion(+)
     create mode 100644 file.txt
    
  6. Track the remaining files in the directory using git add -A, and commit them with a message:

    1
    2
     git add -A
     git status
    

    Returns:

    1
    2
    3
    4
    5
    6
    7
     On branch master
     Changes to be committed:
     (use "git reset HEAD <file>..." to unstage)
    
     modified:   file.txt
     new file:   file2.txt
     new file:   file3.txt
    

    Now, commit the changes:

    1
    2
    3
    4
    5
    6
    7
     git commit -am "The end!"
    
     [master 52a9240] The End
     4 files changed, 1 insertion(+)
     create mode 100644 file1.txt
     create mode 100644 file2.txt
     create mode 100644 file3.txt
    

git add -A, git add ., and git add -u can all be used to stage files for a commit. git add -A stages all of the files in the directory. git add . stages only the new and modified files, omitting any deleted files. git add -u stages only the modified and deleted files, omitting any new files.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This guide is published under a CC BY-ND 4.0 license.