How to Use Git the Version Control System

Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

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:

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

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

    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.

     git add file.txt
     git status
    

    This will return:

    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:

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

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

    [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:

     git add -A
     git status
    

    Returns:

    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:
    
        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
    Note
    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 page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.
The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please update your Cookie Preferences on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser.