Git Tutorial For Beginners
Git is the most widely used Version Control System (VCS) in the software development profession. The best way to learn Git is to dive into the Command Line Interface (CLI). To get started, you need just open a terminal then type a command. The first step before we can do that is to make sure you have it installed.
Git Installation
Ubuntu
sudo apt install git
MacOS
For MacOS there are several ways to install git on your computer:
- Use Homebrew:
brew install git
- Use a binary installer https://sourceforge.net/projects/git-osx-installer/
- Out of the box with XCode.
For more details check the link https://git-scm.com/download/mac
Windows
For Windows it can be installed via executable installer https://git-scm.com/download/win
Basic git commands
To use git you should know at least these basics and for any additional commands you can check official documentation.
First of all is how to create a new git repository
git init
Clone if the repository already exists, usually it’s on GitHub or GitLab
git clone URL
Show changes in the local directory
git status
To add a file to the local repository
git add FILE_NAME
To commit (apply) your changes to the local repository
git commit -m "Commit message"
Or open a text editor to set the commit message
git commit
To see the list of commits for the current branch
git log
Create a new branch with name BRANCH_NAME and switch to it
git checkout -b BRANCH_NAME
Or just switch to the branch if it’s already exists
git checkout BRANCH_NAME
Branch names that are typically used: dev
, master
, staging
and feature branch names. GitHub and many other places are now adopting main
as the primary branch name over master
.
Usually if you switch branches and you have committed changes, you should “stash” your local changes. Then you can “un-stash” when you switch back to the branch you were on.
Save the file changes into the stash storage
git stash
Get the list of stash items
git stash list
Apply and remove the last state from the stash to the current working directory
git stash pop
Clear all stash items
git stash clear
Download the remote commits to the local repository
git pull origin BRANCH_NAME
Upload the local commits to the remote repository
git push origin BRANCH_NAME
Git Workflow
The first step will be individual for a new repository or exist.
If the repository exists on the remote repository provider like GitHub or GitLab
- You should clone a git repository:
git clone URL
- If the repository doesn’t exist, First of all you should initialize git repository by the command:
git init
- The next steps will be the same for both. Make sure you are on the right branch, and if needed switch to the right branch:
git checkout BRANCH_NAME
- Make some file changes are in the project directory.
- Push your changes to the remote repository. Usually
BRANCH_NAME
isdev
ormaster
git push origin BRANCH_NAME
Git CLI vs Git GUI
Here are three Git GUIs that we recommend for the times when you just can’t figure out the right command on the CLI.
GitKraken — cross platform tool with merge conflict resolver. Has free license for open source developers.
GitHub Desktop — cross platform and open source git tool, created by GitHub.
Sublime Merge — cross platform and Freemium git tool, created by the company behind Sublime.
Modern IDEs have git support directly in the application including; VSCode, PhpStorm, WebStorm, Atom and others.