A repository is a place where Git stores all files that are under revision control, as well as additional data related to these files, such as the complete history of changes or information about who made those changes and when. Unlike in centralized revision control systems like Subversion or CVS, a Git repository and a working directory are usually the same. A typical Git repository also only stores a single project and when publicly accessible, it allows anyone to create its clone with a complete revision history.
Initializing an Empty Repository
To create a new, empty Git repository, change to the directory in which you want to keep the repository and type the following at a shell prompt:
scl enable devtoolset-2 'git init'
This creates a hidden directory named
.git in which all repository information is stored.
Importing Data to a Repository
To put an existing project under revision control, create a Git repository in the directory with the project and run the following command:
scl enable devtoolset-2 'git add .'
This marks all files and directories in the current working directory as ready to be added to the Git repository. To proceed and actually add this content to the repository, commit the changes by typing the following at a shell prompt:
scl enable devtoolset-2 'git commit [-m "commit message"]'
Replace
commit message with a short description of your revision. If you omit the -m option, this command allows you to write the commit message in an external text editor. For information on how to configure the default text editor, see Section 7.2.1, “Configuring the Default Text Editor”.
Cloning a Git Repository
To clone an existing Git repository, type the following at a shell prompt:
scl enable devtoolset-2 'git clone git_repository [directory]'
Replace
git_repository with a URL or a path to the Git repository you want to clone, and directory with a path to the directory in which you want to store the clone.
Cloning a Subversion Repository
When the devtoolset-2-git-svn package is installed in the system, Git allows you to create a Git clone of a Subversion repository. To do so, type the following at a shell prompt:
scl enable devtoolset-2 'git svn clone svn_repository [directory]'
Replace
svn_repository with a URL or a path to the Subversion repository you want to clone, and directory with a path to the directory in which you want to store the clone.
For information on how to install the devtoolset-2-git-svn package, see Section 1.5.3, “Installing Optional Packages”.
Cloning a CVS Repository
When the devtoolset-2-git-cvs package is installed in the system, Git allows you to create a Git clone of a CVS repository. To do so, change to the directory with your working copy of the CVS repository and type the following at a shell prompt:
scl enable devtoolset-2 'git cvsimport -C directory cvs_module'
Replace
cvs_module with the name of the subdirectory in which the project is stored, and directory with a path to the directory in which you want to store the clone.
For information on how to install the devtoolset-2-git-cvs package, see Section 1.5.3, “Installing Optional Packages”.
Adding Files and Directories
To add an existing file to a Git repository and put it under revision control, change to the directory with your local Git repository and type the following at a shell prompt:
scl enable devtoolset-2 'git add file...'
Replace
file with the file or files you want to add. This command marks the selected file or files as ready to be added to the Git repository. Similarly, to add all files that are stored in a certain directory to a Git repository, type:
scl enable devtoolset-2 'git add directory...'
Replace
directory with the directory or directories you want to add. This command marks all files in the selected directory or directories as ready to be added to the Git repository.
To proceed and actually add this content to the repository, commit the changes as described in Section 7.3.5, “Committing Changes”.
Renaming Files and Directories
To rename an existing file or directory in a Git repository, change to the directory with your local Git repository and type the following at a shell prompt:
scl enable devtoolset-2 'git mv old_name new_name'
Replace
old_name with the current name of the file or directory and new_name with the new name. This command renames the selected file or directory and marks it as ready to be renamed in the Git repository.
To proceed and actually rename the content in the repository, commit the changes as described in Section 7.3.5, “Committing Changes”.
Deleting Files and Directories
To delete an existing file from a Git repository, change to the directory with your local Git repository and type the following at a shell prompt:
scl enable devtoolset-2 'git rm file...'
Replace
file with the file or files you want to delete. This command deletes all selected files and marks them as ready to be deleted form the Git repository. Similarly, to delete all files that are stored in a certain directory from a Git repository, type:
scl enable devtoolset-2 'git rm -r directory...'
Replace
directory with the directory or directories you want to delete. This command deletes all selected directories and marks them as ready to be deleted from the Git repository.
To proceed and actually delete this content from the repository, commit the changes as described in Section 7.3.5, “Committing Changes”.
Viewing the Current Status
To determine the current status of your local Git repository, change to the directory with the repository and type the following command at a shell prompt:
scl enable devtoolset-2 'git status'
This command displays information about all uncommitted changes in the repository (
new file, renamed, deleted, or modified) and tells you which changes will be applied the next time you commit them. For information on how to commit your changes, see Section 7.3.5, “Committing Changes”.
Viewing Differences
To view all changes in a Git repository, change to the directory with the repository and type the following at a shell prompt:
scl enable devtoolset-2 'git diff'
This command displays changes between the files in the repository and their latest revision. If you are only interested in changes in a particular file, supply its name on the command line as follows:
scl enable devtoolset-2 'git diff file...'
Replace
file with the file or files you want to view.
To apply your changes to a Git repository and create a new revision, change to the directory with the repository and type the following command at a shell prompt:
scl enable devtoolset-2 'git commit [-m "commit message"]'
Replace
commit message with a short description of your revision. This command commits all changes in files that are explicitly marked as ready to be committed. To commit changes in all files that are under revision control, add the -a command line option as follows:
scl enable devtoolset-2 'git commit -a [-m "commit message"]'
Note that if you omit the
-m option, the command allows you to write the commit message in an external text editor. For information on how to configure the default text editor, see Section 7.2.1, “Configuring the Default Text Editor”.
Unlike in centralized version control systems such as CVS or Subversion, when working with Git, project contributors usually do not make their changes in a single, central repository. Instead, they either create a publicly accessible clone of their local repository, or submit their changes to others over email as patches.
Pushing Changes to a Public Git Repository
To push your changes to a publicly accessible Git repository, change to the directory with your local repository and type the following at a shell prompt:
scl enable devtoolset-2 'git push remote_repository'
Replace
remote_repository with the name of the remote repository you want to push your changes to. Note that the repository from which you originally cloned your local copy is automatically named origin.
Creating Patches from Individual Commits
To create patches from your commits, change to the directory with your local Git repository and type the following at a shell prompt:
scl enable devtoolset-2 'git format-patch remote_repository'
Replace
remote_repository with the name of the remote repository from which you made your local copy. This creates a patch for each commit that is not present in this remote repository.
To update your local copy of a Git repository and get the latest changes from a remote repository, change to the directory with your local Git repository and type the following at a shell prompt:
scl enable devtoolset-2 'git fetch remote_repository'
Replace
remote_repository with the name of the remote repository. This command fetches information about the current status of the remote repository, allowing you to review these changes before applying them to your local copy. To proceed and merge these changes with what you have in your local Git repository, type:
scl enable devtoolset-2 'git merge remote_repository'
Alternatively, you can perform both these steps at the same time by using the following command instead:
scl enable devtoolset-2 'git pull remote_repository'