
Git Tutorial for Absolutely Beginners
if you want to start your journey in software development, learning Git is the most important skills you will need. Git is a version control system that can help you track changes you made in your code and let you collaborate with other developers effectively.
What is Git?
Git is a distributed version control system created by Linus Torvalds in 2005. This tool can help multiple developers work on same project without conflicting each other’s changes. By using Git, you can save your development time by focusing on your code. You can save snapshots of your work, go back to previous versions, create branch to experiment with newer feature implementation without breaking changes on your code.
Why You Need Git
Developer is need version control, if they don’t use it they can’t go back if they facing issues on newer features. Even if they use multiple copies of project like this project_v1.js, project_v2.js, project_v3.js, and so on—this will hard to manage.
Git solved this by tracking every changes you made with clear messages. Git also enables team collaboration, you can work with other developers worldwide. Popular platforms like GitHub, GitLab, and Bitbucket are built on Git, making this tool essential for modern development workflow.
Installing Git
installing Git is easy, you can visit Git - Install and choose operating system you use and then you will find download link to the latest version.
After installation, you can verify if Git is working by opening your terminal and type this git --version. When installation is successful, you will see that the command will return your Git version number.
# Verify if Git is working after installation
git --versionBasic Git Commands
Git has many command, to see list of all available command you can visit Git Cheat Sheet. But for now we can learn Git fundamental commands that you will use regularly in your projects.
- You must navigate to your project, for example: I have learn-html project, so i must begin to navigate to this folder by run
cd learn-html. - After navigate to project directory, you must initalize Git first to start tracking your project by use this command:
git init. - After initialize Git repository, you can see there is a hidden folder inside your project called .git folder.
- Now you can check your Git status by run this command:
git status, this command will show you which files have been modified or added. - if you have files in your project before initialize Git, you will see all your file is untracked.
- To track your files, you can run
git addcommand. For example, if i have index.html, style.css, and script.js i can simply rungit add .—this will stage all your files. - You can also stage your file one by one, like this
git add index.html—this will stage only index.html file on your project. - And then, when all changes is already staged, you can begin to commit them with
git commit -m "Add index.html, style.css, and script.js". - When writing commit message, I recommend you to write clear commit messages and explain what changes you made on your staged changes.
- You can follow Conventional Commits guide if you need to help write better commit messages.
- A good commit message can help you and your team to understand project history, so you can begin to continue where you left off.
- You can use
git logto see all previous commits. This will show who made changes, when they made them, and their commit messages. - You can also experiment in your Git by not committing directly to main branch by creating separate branches.
- You can do this with
git branchcommand, for example i want to add library that still on development so i create a branch withgit branch feature/nightlyand switch to it usinggit checkout feature/nightly.
# Navigate to project folder
cd learn-html
# Initialize Git to start tracking
git init
# Check file status (tracked/untracked)
git status
# Stage all files
git add .
# Stage a specific file
git add index.html
# Commit staged changes
git commit -m "Add index.html, style.css, and script.js"
# See commit history
git log
# Create a new branch (example given in text)
git branch feature/nightly
# Switch to that branch
git checkout feature/nightlyConfiguring Git
When you first install GIt, you will notice that Git need information about you. Git require your name and your email address, this information appears in every commit you made.
you can set your name with git config --global user.name "Your Name" and your email with git config --global user.email "your.email@example.com".
The global flag means these settings will apply to all of your Git projects. Use this if you don’t want to setup on each project manually, if you use it without --global option then you need to run this command again if you working on new project.
# Set your name (global)
git config --global user.name "Your Name"
# Set your email (global)
git config --global user.email "your.email@example.com"Git Workflow
Git follows simple three-stage workflow, your files is exist in the working directory where you make changes. When you are ready to save your changes, you can move the files to staging using git add. And then, you can commit staged changes with git commit command.
For best practices, if you believe that your code is working correctly—you must stage it. So if you made any breaking changes, you can simply discard them. The files on staging area will not be affected if you discard your current changes.
# Move files to staging area
git add .
# Save changes to history
git commit -m "Your message here"Git Branching
The default branch is usually called main or master branch. Please keep in mind, if you need to create a new branch avoid to use these.
When you create a new branch, you are creating a separate implementation. So your current branch will be not affected if you make any breaking changes.
Changes on branch will only affect if you merge them, if you delete it the main branch will not get affected.
For a branch name you can use Git conventional commit, For advanced use case you can create and switch to a new branch in single command by using git checkout -b feature-name. The git switch feature-name command also does same thing, the only difference is this command have clearer syntax.
When you are done working on a branch, if you are confidently that your changes are working correctly you can switch to main with git checkout main, and then you can run git merge feature-name command.
# Create and switch to a new branch in one command
git checkout -b feature-name
# Alternative syntax (clearer)
git switch feature-name
# Switch back to main
git checkout main
# Merge the feature branch into main
git merge feature-nameMerge Conflicts
While sometimes Git can’t merge automatically because of two people edited the same lines of code. When this happens, Git marks every conflicting sections on your code with marker like this <<<<<<< HEAD, =======, and >>>>>>> branch-name.
To resolve Git conflicts, open the file, decide what change you need to keep, remove the conflict markers, and then stage it and commit it to resolve this. This is a normal if you collaborate with others, so please don’t panic when this happens.
This merge conflict also happen if you create a separate branch, and then you switch to main branch and you commit some changes to your file. And if you return back to a branch that you created before, and some files are edited in both branches at the same lines, Git won't know which version to keep when you try to merge.
# Git marks conflicts in your file like this:
<<<<<<< HEAD
(Current Change)
=======
(Incoming Change)
>>>>>>> branch-name
# Action: Open file, delete markers, choose code, then:
git add .
git commit -m "Resolved merge conflict"Remote Repositories
While local Git repository is useful, you can also host it on popular Git hosting such as GitHub, GitLab, and Bitbucket.
By pushing your local repository to remote repository, you can enable collaboration on your project.
To connect your local repository with remote repository, you can use use git remote add origin https://github.com/username/repository.git. The word "origin" is the conventional name for your main remote repository.
And then, if you already make changes and commit on your local repository, you can push it to remote with git push origin main. Also, this command can work if you push main branch: git push.
If you need to push specific branch name, you can use git push -u origin branch-name to remote repository.
If you edit your changes on GitHub, your local repository need update—you an run git pull origin main to get latest changes on your remote repository.
# Connect local repo to remote
git remote add origin https://github.com/username/repository.git
# Push to remote (first time setup)
git push -u origin branch-name
# Standard push for main
git push origin main
# OR
git push
# Update local repo with remote changes
git pull origin mainGit Clone
If you already create a project on your remote repository, you can clone it with git clone https://github.com/username/repository.git command.
This will create a complete copy of your remote repository on your local computer.
# Copy a remote repository to your local computer
git clone https://github.com/username/repository.gitUndoing Changes
Mistake can happen everytime, Git gives you many ways to undo mistakes. if you modified a file but the file is not staged yet, you can restore it with git checkout -- filename.js or git restore filename.js.
To unstage file you added by a mistake, use git reset HEAD filename.js or git restore --staged filename.js.
If you accidentally commited something, you can use git revert commit-hash. This is safer and better approach than rewriting history when working with others.
If you don’t know what commit hash are, you can run git log and then you will find each of commit are like this a1b2c3d4e5f67890abcdef1234567890abcdef12.
# Restore modified file (not staged yet)
git checkout -- filename.js
# OR
git restore filename.js
# Unstage a file (added by mistake)
git reset HEAD filename.js
# OR
git restore --staged filename.js
# Undo a commit safely (creates a reverse commit)
git revert commit-hashGit Compare
You can review what you changed on GIt with git diff command. This will show line-by-line differences in unstaged changes. For staged changes you can use git diff --cached command.
To see changes between commits, you can use git diff commit1 commit2. You can also compare branches with git diff branch1 branch2.
# See line-by-line differences in unstaged changes
git diff
# See differences in staged changes
git diff --cached
# Compare two commits
git diff commit1 commit2
# Compare two branches
git diff branch1 branch2.gitignore
You can use .gitignore for sensitive files that shouldn’t be tracked on Git. Configuration files, dependencies, environment variables, and build outputs shouldn’t be in version control.
If you don’t have .gitignore, you can create it on your project root. And you can try to include common entries such as node_modules/ for JavaScript projects, *.log for log files, .env for environment variables, and dist/ for build outputs. Each line in .gitignore represents a pattern Git should ignore.
# Example content for .gitignore file
node_modules/
*.log
.env
dist/Git Tags
You can create tag on your Git, this will helps you make releases on your project. You can create tag with git tag v1.0.0 or an annotated tag with more information using git tag -a v1.0.0 -m "Initial release".
After that, you must push tags to remote with git push origin v1.0.0 or push all tags with git push origin --tags.
Please keep in mind that git push origin main will not push the tags by default.
# Create a simple tag
git tag v1.0.0
# Create an annotated tag (recommended)
git tag -a v1.0.0 -m "Initial release"
# Push a specific tag to remote
git push origin v1.0.0
# Push all tags
git push origin --tagsGit Stash
Sometimes you need to switch to another branches but the changes on your Git isn’t ready to commit yet, so you need to temporarily save your work.
You can use git stash to temporarily save your work, so your working directory becomes clean—and you can switch to another branches freely.
To retrieve your stashed work you can use git stash pop command. View all stashes with git stash list, and apply a specific stash with git stash apply.
# Temporarily save work (clean working directory)
git stash
# View list of stashes
git stash list
# Retrieve stashed work
git stash pop
# Apply a specific stash
git stash applyPull Requests
In team environments, you more likely don’t push directly your changes to main branch. Instead, you push it to feature branch and create a pull request on GitHub later for review before merged.
Pull requests facilitate code review, discussion, and automated testing. They are central key to modern collaborative development workflow.
Fork Another Repository
When you fork to another user repository on GitHub, your repository will not synced with original repository.
To do this, you can add the original repository as an upstream remote with git remote add upstream https://github.com/original-owner/repository.git.
Fetch changes periodically with git fetch upstream, then merge them into your local branch with git merge upstream/main.
# Add original repo as upstream
git remote add upstream https://github.com/original-owner/repository.git
# Fetch changes from upstream
git fetch upstream
# Merge upstream changes into local branch
git merge upstream/mainGit Best Practices
Make sure you write meaningful commit messages and explain why you made the changes, make sure your commit are follow Git Conventional Commits and have concise and clear messages.
Also commit often with small, logical changes rather than huge commits. This will makes it easier to understand history and you can revert later when you need it.
For branching, keep it focused on single features or fixes, this will make code review easier and reduce conflicts when merged.
If you working with team, pull from remote repository periodically to stay synchronized and to avoid conflicts when you made any changes.
Common Git Mistakes
if you accidentally commit to the wrong branch you can create a new branch from your current branch with git branch correct-branch, then reset your original branch with git reset --hard HEAD~1.
Never commit sensitive information to your repository, if you accidentally do it, you will need to remove it form your Git history. For best practices, keep sensitive information through .gitignore file.
# If you committed to the wrong branch:
# 1. Create correct branch from current state
git branch correct-branch
# 2. Reset the original branch back one step
git reset --hard HEAD~1Conclusion
The best way to learn Git is learn by doing, use it on your actual project and make commits regularly. Don’t be afraid to experiment and practice it with confidence.
Also, I recommend you to create GitHub account, while experimenting with local repository is good—but remote repository will help understand better and you can work with other people.
Categories
Programming
Tags
Git, GitHub
