Most of the time, you will likely use just a handful of git commands:
- git add to tell git this file is ready to be committed to the repository.
- git commit which tells git to commit the files that have been git added.
And if you are working with a remote repository, you will also use:
- git clone
- git pull
- git push
And that’s about it.
“Here is the git URL”
Some day, somebody will tell you: “Our source repository is in git. Here is the git URL” and you will want to know what to do. See Git and Remote Repositories and then come back here for a summary of the typical git commands.
Read on for a quick start on how to git going quickly with the commands you’ll need to use most of the time.
Do try this at home: Create a new git repository
To start playing with git, go ahead and create a new git repository on your system. (Typically, when you start out with git, you probably are given a URL for a git repository to start working on. But in this case, you can create a git repository just for playing.)
Create the repository in any empty directory (we’ll create the empty directory in this example):
$ mkdir repo $ cd repo $ git init Initialised empty Git repository in .git/
Use git status to check the status of local files.
$ git status # On branch master # # Initial commit # nothing to commit (create/copy files and use "git add" to track)
The above output from git status tell us a branch named master was created (the default branch created is always named master).
Create a file for adding to the repository
$ echo This is the README file. > README $ git status # On branch master # # Initial commit # # Untracked files: # (use "git add ..." to include in what will be committed) # # README nothing added to commit but untracked files present (use "git add" to track)
Above, git status told us git saw the new file, README that is not part of the repository (“untracked”), but is suggesting you might want it added.
Add the file to the list of files to be committed to the repository
$ git add README $ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached ..." to unstage) # # new file: README #
Above, git status told us the next git commit command will save the new file, README to the repository. README is not yet committed to the repository: It is just on the list to be committed if the git commit command is used.
Commit the file to the repository
$ git commit -m"Add README to the repo" [master (root-commit) 94e24a4] Add README to the repo 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 README
The “-m” option sets the message that will be saved with the commit. If you don’t use the -m option, then an editor will be started and you can enter the commit message in the editor.
README is the first file committed to the repository.
$ git status # On branch master nothing to commit (working directory clean)
Above, git status says nothing new is going on. It is correct.
Modify the README file
$ echo One more line. >> README $ cat README This is the README file. One more line. $ git status # On branch master # Changed but not updated: # (use "git add ..." to update what will be committed) # (use "git checkout -- ..." to discard changes in working directory) # # modified: README # no changes added to commit (use "git add" and/or "git commit -a")
Note above that the README file that is listed as being modified, but needs a git add README before it can be committed to the repository.
Add and commit the modified README file
$ git add README $ git commit -m "Added a second line." [master bb937de] Added a second line. Committer: Tim Flagg <email@example.com> 1 files changed, 1 insertions(+), 0 deletions(-)
You’re ready to git going!
You’ve now seen how to:
- Create your own repository: git init
- Add files to the repository: git add filename followed by git commit
- Add changed versions of files to the repository: git add filename followed by git commit
If you are going to use git with a remote repository, then you use those same commands, but also use the git clone, git pull, and push commands as described in Git and Remote Repositories.
You’re now ready to git going!