Initial introduction to Git and github
Joel Hanerth
Posted on September 19, 2024
If you're a developer or just starting to program, you've probably heard of Git and GitHub. These tools are essential in the world of software development, especially for those working in teams or on open-source projects. But what exactly are they, and why are they so important?
What is Git?
Git is a distributed version control system created by Linus Torvalds in 2005. It allows developers to track changes in code, collaborate on different features, and revert changes when necessary. Git makes team collaboration easier since each developer can have their local copy of the repository and integrate their contributions into the main project in a controlled manner.
Version control is also extremely useful for maintaining the project's history, enabling you to "go back in time" and see how the code evolved, or even revert to a previous state if something isn't working as expected.
What is GitHub?
GitHub is an online platform that hosts Git repositories. It adds a user-friendly interface and several extra features to collaborate with other developers. Besides storing repositories, GitHub offers tools such as pull requests, issues, and projects, making collaboration management and tracking bugs and new features easier.
Another important point is that GitHub has become one of the main platforms for open-source development, allowing developers worldwide to collaborate on a wide variety of projects.
With Git and GitHub, you not only have a powerful way to manage code locally but also a space to share your work and collaborate globally.
How to create a repository on GitHub:
1. Access GitHub: Log in to your GitHub account. If you don’t have an account, create one at github.com.
2. Create a new Repository:
In the upper-right corner of the screen, click the "+" icon and then click New repository.
-
Fill in the repository information:
- Repository name: Give your repository a name (for example, "my-project").
- Description (optional): Add a brief description of the project.
- Choose whether you want the repository to be Public or Private.
You can check the option to add a
README
file if you want the repository to have an initial description.
3. Create the Repository:
- After filling in the details, click Create repository.
- GitHub will create the repository and show the repository URL, which you will use to clone it into your Visual Studio.
Cloning the repository in Visual Studio
Now that you've created the repository on GitHub, you can clone it to your local machine and start working on it.
Steps to clone a repository in the Visual Studio terminal:
- Open Visual Studio.
- Open the Integrated Terminal.
- Navigate to the desired folder.
- Use the
git clone
command:- In GitHub, copy the repository URL (HTTPS or SSH).
- In Visual Studio's terminal, type the following command:
git clone <repository_url>
5.Result:
- Git will clone the repository into the current folder, and you'll see something like this in the terminal:
Cloning into 'my-project'...
remote: Counting objects: 100, done.
remote: Compressing objects: 100% (XXX/XXX), done.
Receiving objects: 100% (XXX/XXX), done.
6.Navigate to the Cloned Repository:
- Now, go to the folder of the cloned repository:
cd my-project
YOU’RE READY TO START!
Note: If you haven’t previously configured Git on your machine, you won’t be able to clone the repository successfully. Before trying the git clone
command, ensure Git is configured using the following commands in the terminal:
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
These commands configure the global username and email in your Git environment, which will be used when making commits. If Git isn’t configured, you may encounter errors when interacting with repositories.
Let's get to work!
Now that you've cloned your repository, let's create a file, add it to version control, and explore Git's essential commands.
Creating a File and Using Basic Commands
1. Create a file:
- In Visual Studio, create a new file at the root of your project, for example, a file named
index.html
. In the terminal, you can do this with the command:
touch index.html
- Or simply create the file in the Visual Studio editor.
2. Check the Repository Status:
- To see what has changed in your repository, use the
git status
command:
Untracked files:
(use "git add <file>..." to include in what will be committed)
index.html
- This means Git has detected the
index.html
file but is not tracking it yet.
git add
Command
3. Add Files to Version Control:
- To start tracking changes to a file, use the
git add
command:
git add index.html
- If you want to add all modified or created files, you can use:
git add .
- Now the files are ready to be committed.
git commit
Command
4. Committing Changes:
- A commit records changes to the repository. To create a commit, use the command:
git commit -m "Adds index.html file"
Note: The -m
flag allows you to add a descriptive message for the commit.
git log
Command
5. Check Commit History:
- To see the history of commits made in the repository, use the
git log
command:
git log
- This is the default log option, but Git offers some useful options to simplify and customize the
git log
output.
Using git log --oneline
- The oneline mode compresses the display of commits into a single line per commit, showing only the short hash and commit message. Command:
git log --oneline
Using git log --graph
- The
git log --graph
command displays the commits in a graph format, allowing you to visually see the structure of branches and merges. Command:
git log --graph
Combining git log --graph --oneline --all
- You can combine several options for a more comprehensive and simple view of the commit history. Combining
--graph
,--oneline
, and--all
(which shows commits from all branches - you can know more about branches here) is particularly useful for a clear view of the project's history. Command:
git log --graph --oneline --all
Output (example):
* a1b2c3d (HEAD -> master) Adds new feature
| * 9e8f7g6 (feature) Fixes bug in feature branch
|/
* 5h4i3j2 Refactors code for improvements
git checkout
Command
This command has multiple uses, but I'll cover just two:
6. Revert Changes:
If you make changes to a file and want to return to the last committed state, use the git checkout
command:
git checkout index.html
- This will undo changes made to
index.html
that haven't been committed yet.
7. View Project at a Specific Commit:
You can use the git checkout
command to "look" at how the project was at a particular commit:
git checkout [commit_hash]
Pushing and Updating the Remote Repository
Now that you've made local changes, you might want to push these changes to the remote repository (GitHub) or pull the latest changes someone else made in the repository.
git pull
Command
8. Update Local Repository:
- Before pushing your changes, it's a good practice to ensure your local copy is up to date with the remote repository. To do this, use:
git pull
- The
git pull
command brings changes from the remote repository to your local machine.
git push
Command
9. Push Your Changes to the Remote Repository:
- Now, you can push your local commits to GitHub with the
git push
command:
git push
- This will push your changes to the
master
ormain
branch on the remote repository.
Did you like the post 🤔? Are you interested in learning more about Git features? Visit: Introduction to branch 😉
Posted on September 19, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.