Git basics - 10 steps to get started on Git

Introduction

Want to learn git but don’t know where to start? No worries, I will be sharing with you some git basics to get you started!

What is Git?

Git is a powerful version control system and one of the most widely used ones today. It is useful for keeping track of code changes and collaborating with other team members. Git can be used with other Git hosting services such as GitHub, GitLab, BitBucket. But in this article, we will be focusing on using Git with GitHub. Git and GitHub may sound similar, but they are not to be confused! Unlike git which is a command-line tool, GitHub is a graphical user interface and a Git repository hosting service.

1. Install git

If you do not have git installed, you can visit https://git-scm.com/downloads for instructions to install.

2. Setting up

git config --global user.name “your_username”
git config --global user.email “your_github_email”

On GitHub, create a new repository. To add a new remote repository as a shortname (in this case, “origin”), you can reference easily, copy the repository url and run:

git remote add origin github_url

3. Create a local git repository

Unlike a remote repository that is hosted on a server and accessible to all team members, a local repository is only accessible to the owner of the device that it resides on. Open up a terminal and go to an existing directory where you want to create the local repository. If you want to create the repository in a new directory, use the mkdir (make directory) command to create a new directory. Next, use the cd (change directory) command to go into the new directory.

mkdir project
cd project

Next, initialize a git repository:

git init

And, create a new branch called “main”:

git branch -M main

4. Create and add a new file into the repository

Run the following command to create a new text file called ‘hello.txt’ into the current directory:

touch hello.txt

5. Stage the file

Staged files are files that are ready to be committed to the repository. To stage a file, run:

git add fileName 

// OR to stage all files in the current directory
git add . 

Files that are staged are tracked by git - git knows about the files and they are added to the repository. Untracked files are in our working directory but not added to the repository. To check which files are staged / being tracked by Git, run:

git status

6. Commit the file

Commits help us to keep track of our progress. Commits are like “checkpoints” which we can go back to if we want to make any changes. To commit a file, run:

git commit -m “message”

Note that message is to tell others what changes you have made. To view the history of commits for a repository, run:

git log

7. Push the file

Push the file from the local repository to the remote repository:

git push -u origin main 

Now your code has been successfully updated on GitHub!

8. Branching

Branching is useful when we want to work on different parts of the project (e.g. different features or bug fixes) without interfering with the main branch. To create a new branch, run:

git checkout -b new-branch-name

At any point in time, you can switch between your new branch and your main branch:

// to check which branch we are currently working on 
git branch 
// the asterisk marks the branch we are currently on

// to switch to main branch
git checkout main

// to switch to new branch
git checkout new-branch-name

When we are done with the new branch, we can merge the changes with the main branch:

// first switch back to the main branch if we are not on the main branch
git checkout main

// merge the changes
git merge branchName

Now, we can delete the new branch as it is no longer needed:

git branch -d new-branch-name

9. Pull Request

Alternatively, if we want to share our new branch to enable collaboration, we can push the new branch to the remote repository:

git push origin new-branch-name

When your new branch is ready to be merged with the main branch, create a pull request (PR), by clicking on “Compare & Pull Request” button on GitHub. In a pull request, you will describe the changes, and request your team members to review your code before putting the changes on the main branch. When your new branch has been approved / is ready to be merged, click on the green “merge pull request” button. Afterwards, click the “Delete branch” button to delete your new branch, as it is no longer needed.

10. Collaborating

When collaborating, you and your team members will usually make changes to the same file. To ensure that you’re working on the most updated git repository, run:

git pull 

Note: git pull is equivalent to git fetch and git merge. Use git pull when you’re not currently working on any files and have no uncommitted files. Git pull will copy the changes from the remote repository to your local repository and your working directory. Else, use git fetch. Git fetch will fetch the latest changes from the remote repository to your local repository only:

git fetch

Afterwards, do git merge when you are ready to merge the remote changes to your main branch:

// switch to main branch first if you are not on it
git checkout main

// merge changes
git merge origin/main

Conclusion

There’s still a lot to learn about Git, but that’s it for now! I hope that this article deepened your understanding of Git in one way or another. If you found any mistakes along the way, feel free to let me know at the bottom of this page. Happy learning!