Git is not GitHub. Git is the system, and GitHub is a repository hosting service (the most popular of many).
👉 After this lesson you will be able to:
- Understand what a version control system is
- Understand the advantages of using Git
- Create a new repository
- Clone a repository
- Check the status of a repository
- Add files to a commit
- Commit files
- Push files into a remote repository
- See and understand the git log
- Create an account in GitHub
- GitHub create account, create repo, clone repo, add files, commit files, push files, git log
Goals
Create a local project and launch it to a live server with Git using the command line.
What is Git?
Git is a difficult subject to tackle for self-taught web developers who didn't learn to code with a team. If you've always worked alone and want an explanation of how to get started with Git, this tutorial is for you.
Web development projects usually require the effort of more than one programmer. During web applications life cycle a lot of changes occurs: new features, bugs management, re-factorization of the code. The result?
New releases, code that has to be organized and accesible for everyone while we track changes and a easy and practical way of putting new code together without breaking what is already there.
Git is a free and open source distributed version control system. This means Git can do all of the tasks needed above.
A version control system is software that helps developers to track changes and distribute their code.
Git advantages
👉 Distributed architecture
Git is an example of a DVCS (Distributed Version Control System). This means that rather than have one single place for the full version history of the software, every developer gets to have his own working copy of the code and this copy is also a repository with the full history of all changes ever done to that version of the code. 👉 It is used to keep track of revisions and allow a developer or dev team to work together on a project through branches.
👉 Flexibility
Git allows us to have various development workflows, so it adapts to any project size. It also provides compatibility with a lot of existing systems and protocols.
👉 Everyone knows Git
Git has the functionality, performance, security and flexibility that most teams and individual developers need, that’s why everyone uses it. Also, a lot of developers already have Git experience, so it is a de facto standard.
👉 Git is open source
Git is an open source project with more than ten years of evolution. The project maintainers have shown balanced judgment and a mature approach to meeting the long term needs of its users with regular releases that improve usability and functionality.
This also means Git community is huge and a lot of good quality documentation is available on the Internet, through books, tutorials and dedicated websites.
Creating a Repository
Git manages different projects with different repositories. We generally call them repos for short 🤔
To create a new repo in your project, open your terminal and go to your project folder. Right in this folder, at the top of your project, type:
$ git init
Initialized empty Git repository in [your .git directory path]
❗️💡 You don’t need to type the
$
symbol. It is provided by your terminal to let you know where you should type your commands.
🛑 👉 This command creates a hidden directory .git
in that folder. This hidden directory is where Git operates and stores it’s data, so right now, it has an empty repository in it.
👉 Type ls -la
in your terminal to see the new folder.
Checking the Status Next up, let’s type the git status command to see what the current state of our project is:
$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
🛑 💡
It’s healthy to run git status often. Sometimes things change and you don’t notice it.
Adding and committing changes
Create a file called index.html
$ touch index.html
Run the git status command again to see how the repository status has changed:
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
index.html
nothing added to commit but untracked files present (use "git add" to track)
🛑 As you can see, Git is now detecting changes -in this case, a new file.
🛑 Also, it detects that this new file is not being tracked and Git suggests how to add this file in the next commit.
👉 A Git commit works a lot like taking a picture. It takes a snapshot of the files added to the commit.
This is a very important concept to understand. Imagine it’s your sibling’s birthday and you are the unofficial photographer. You get to choose which family members are going to be included in each picture. You select some of them, ask them to stay still somewhere in the house and then you take a picture. With Git, family members are your project files, when you ask them to stay in a certain place you’re adding and when you take the picture you’re committing the changes.
⚠️ 🛑
You are not making copies of the files you’re tracking. You are just creating a snapshot of the files you added.
Adding the file
Now, let’s add index.html
to the tracked group.
$ git add index.html
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: index.html
If you need to add every file in your filesystem, you can type:
$ git add .
👉 As you can see in your Git status output, you can now also remove the file from the tracking group. This is known as unstage the changes (to add them is to stage the changes to the commit).
🛑 Committing the files
To commit your changes simply type
\$ git commit -m "Add the index.html file"
[master (root-commit) d100e63] Add the index.html file
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 index.html
You could type git status
again to check your repository status now.
Git log
So we’ve made a commit. But if we have several commits, we would like to be able to see the history of changes we’ve done. Think of Git’s log as a journal that remembers all the changes we’ve committed so far, in the order we committed them. Try running it now:
$ git log
The output will be an opened txt file something like this
commit d100e63a4266ccaf1c0fe0e0c8e65b40440e4327
Author: irenedoe <irie6@gmail.com>
Date: Sat June 20 11:42:22 2019 +0200
Add the index.html file
The log file will open in your terminal with the less program and to exit simply type q
.
Remote repositories
To collaborate on any Git project, you need to know how to manage your remote repositories.
Remote repositories are versions of your project that are hosted on the Internet or network somewhere. Collaborating with others involves managing these remote repositories and pushing and pulling data to and from them when you need to share work.