How to Use Git the Version Control System
Updated by Angel Guarisma
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.
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
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
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)
file.txtcontains text, you want Git to track any future changes to that file. Use
git add file.txtto add
file.txtto the list of files Git monitors. Type
git statusafter the addition to confirm that Git is tracking the new file.
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
To commit the changes of
file.txtto 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
-amoptions to commit
all modified files, specifically the ones Git is tracking, and include a commit
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
Track the remaining files in the directory using
git add -A, and commit them with a message:
git add -A git status
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 -ucan all be used to stage files for a commit.
git add -Astages
all of the files in the directory.
git add .stages only the new and modified files, omitting any deleted files.
git add -ustages only the modified and deleted files, omitting any new files.
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.