Following on from the getting started with Git tutorial, this article will focus on adding, committing and pushing with Git.
Firstly, take a look at the image below.
Don’t worry if you do not understand everything in this image. For now, focus on the different ‘areas’.
If you have initialised a new Git repository, it will have nothing within it.
Adding a simple text file into the repository and then running the ‘git status’ command will output the following:
$ git status On branch master Initial commit Untracked files: (use "git add
..." to include in what will be committed) text.txt nothing added to commit but untracked files present (use "git add" to track)
Git is notifying you that in your working directory area, there is a untracked file.
Git will also tell you that in order to add the file into the staging area, we have to use the ‘git add’ command.
$ git add text.txt
It’s worth noting that should you need to add multiple files, you do not have to use the ‘git add’ command each time. By simply running:
$ git add .
Will stage all of the untracked files.
Running a git status afterwards shows that Git now recognises the staged file and states the changes are now ready to be committed.
$ git status On branch master Initial commit Changes to be committed: (use "git rm --cached
..." to unstage) new file: text.txt
Commits record changes to your local repository. They are usually accompanied by a commit message which is used to describe the change(s) made. This can be achieved with the following command:
$ git commit -m "Our first commit message"
Many companies use third party companies to host their source code. Remote repositories are set up which allow users to manage many codebases.
The ‘git push’ command will push the changes made to your local repositories to the remote repositories.
In order to do this, you must add a remote origin. For example, if you are using Github to host your source code, you might add an origin that looks similar to this:
$ git remote add origin firstname.lastname@example.org:johndoe/first_app.git
You will then be able to push your commit(s). The default branch within Git is called ‘master’, so to push to the master branch:
git push -u origin master