Git for beginners: basic terms and commands
Git is a distributed version control system (DVCS). It helps developers track code changes while they build software.
The Linux developer community, especially Linus Torvalds, created Git in 2005 to manage development of the Linux kernel. Git was made because BitKeeper, the tool the kernel project had used since 2002, stopped being free of charge. Git is still widely used today.
Why use Git
- Git records changes over time. You can return to an older version if you need to.
- Several developers can work on the same project at the same time. Git merges changes that do not overlap. If two changes touch the same line, Git marks them as a conflict.
- Branches keep work separate. You can build a feature on its own branch, then merge it when it is ready.
- Every developer has a full copy of the repository, including its history. You can still commit without a connection to the server.
- Git is designed to be fast, including for large projects.
Basic Git terms
- Repository or repo: a project folder that Git tracks, along with its full history of changes.
- Commit: a single point in Git’s history. Each commit stores the state of the files at that moment and has a unique ID.
- Branch: a line of development.
- Merge: combining changes from one branch into another.
- Remote: a repository hosted somewhere else, for example on GitHub or GitLab.
- Clone: making a complete copy of a remote repository on your computer.
- Pull: fetching changes from a remote and bringing them into the active branch.
- Push: sending commits from a local branch to a remote.
Installing Git
Check first whether Git is already on your computer:
git --versionIf that command does not show a version number, install Git for your operating system:
- Windows: download the installer from the Git for Windows download page (opens in a new tab), then run it.
- macOS: run
xcode-select --installto install the Xcode Command Line Tools. Or runbrew install gitif you use Homebrew. - Linux: on Debian and Ubuntu, run
sudo apt install git. On Fedora, runsudo dnf install git. Other distributions have their own commands. See the Git download page for Linux (opens in a new tab).
Setting up Git
Before you use Git, set your name and email. Git stores both in every commit.
git config --global user.name "Your Name"
git config --global user.email "name@example.com"Also set the initial branch name to main:
git config --global init.defaultBranch mainWithout this setting, Git uses master as the initial branch name. Git 3.0 will use main by default, but no release date has been set.
Basic Git commands
Create a new repository
Go to your project folder, then run:
git initGit creates a hidden folder named .git. That folder stores the full history of the project.
Clone a repository from a remote
git clone https://github.com/account-name/repo-name.gitThis command creates a new folder named repo-name that contains a copy of the repository.
Check the status
git statusThis command shows the active branch and the files that changed since the last commit.
Stage and save changes
Run git add to put a file in the staging area, the list of changes that goes into the next commit. Then run git commit to save everything in the staging area as one commit:
git add README.md
git commit -m "Add README"Replace README.md with the name of the file you changed. Write a commit message that explains the change.
View the commit history
git log
git log --onelinegit log shows the full history. git log --oneline shows one line for each commit.
Create and switch branches
Create a new branch, then switch to it:
git branch login-feature
git switch login-featureOr create and switch in one step:
git switch -c login-featuregit switch is only for switching branches. git checkout still works. For example, git checkout -b login-feature creates and switches in one step.
Merge branches
Switch to main, then merge the feature branch:
git switch main
git merge login-featureIf two branches change the same line in one file, Git stops and marks a conflict. Open the file and choose the correct content. Delete the conflict markers (<<<<<<<, ======= and >>>>>>>). Then run git add and git commit.
Connect to a remote
Replace the URL with your repository’s address:
git remote add origin https://github.com/account-name/repo-name.gitPush the main branch to the remote for the first time. The -u option links the local branch with the remote branch:
git push -u origin mainIf Git asks for a password when you push over HTTPS, use a GitHub personal access token (opens in a new tab) instead of the password.
After that, git push is enough. If the remote has commits that are not on your computer yet, the push is rejected. Run git pull first, then try again. If git pull stops because the histories have diverged, see the next section.
Get changes from a remote
git pull origin mainPull fetches new commits from the remote and applies them to the active branch. If only the remote has new commits, Git moves your branch forward (a fast-forward).
If the local and remote histories have diverged, Git stops and asks you to choose how to combine them. For a single pull, use merge:
git pull --no-rebase origin mainTo use merge for every pull, set it once:
git config --global pull.rebase falseTo learn more, read the Pro Git book (opens in a new tab) on the official Git website.
