[Git 1] Git Basics: From Installation to Branches, Conflicts and Merge Requests

Git's three areas and the remote repository: working directory → add → staging area → commit → local repository → push → remote repository, and pull brings changes back

Written by

in

RTL code, testbenches, scripts, documentation: any work that lives in text needs version control. To answer "the simulation worked yesterday, so why not today?", you need to know what changed, when, and who changed it. The standard tool for that is Git.

This post walks through everything from installation to collaborating through a remote repository, in an order a first-time Git user can follow hands-on.


Why use Git?

Git is a distributed version control system.

  • You can roll back a single file, or the whole project, to an earlier state.
  • It keeps a record of who changed what, when, and why.
  • If you delete or break something by mistake, it is easy to recover.
  • Several people can edit the same code at the same time and safely merge it later.

The biggest reason is that you can always get back the version from any point in time. You can pin down exactly the version right before tape-out, or the version where a bug first appeared.

💡 Git can be used from the command line (CLI) or from GUI tools (VS Code, GitKraken and so on), but it is best to learn it on the CLI first. Typing the commands yourself is how you understand what Git actually does; a GUI only adds convenience on top.


The three areas

Git's three areas and the remote repository: working directory → add → staging area → commit → local repository → push → remote repository, and pull brings changes back
Figure 1. There is a place where you edit files (working directory), a place where you gather changes for the next commit (staging area), and a place where commits pile up (local repository). push and pull exchange them with the remote repository.
Area Description
Working directory Where you actually edit files
Staging area (index) Where you pick and hold the changes for the next commit
Repository (.git) Where commits (snapshots) are stored

The basic flow is edit → stage with add → save with commit.


Installation and first setup

# Ubuntu / Debian
sudo apt install git
# RHEL family
sudo dnf install git
# macOS
brew install git

git --version

💡 Old servers (CentOS 7 and the like) sometimes come with a very old Git such as 1.8. It lacks newer features (git switch, git restore and so on), so use 2.x or later if you can.

The first time, set your name and email. They are recorded in every commit.

git config --global user.name  "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main      # default branch name for new repositories
git config --list                                # check

Run it without --global inside a repository and it applies to that repository only. That is handy when you use different emails for work and personal projects.


Exercise 1: Create a repository and commit

mkdir git_practice && cd git_practice
git init                        # creates the .git directory → now a Git repository
echo "hi" > hello.txt

git status                      # hello.txt: Untracked (red)
git add hello.txt               # stage
git status                      # Changes to be committed (green)
git commit -m "Add greeting"    # commit

git log                         # commit ID, author, date, message
git log --oneline --graph       # one line each, with the branch graph
Command Meaning
git add file Stage a specific file
git add . All changes under the current directory
git add -A All changes in the whole repository (including deletions)
git commit -m "message" Commit (without -m, an editor opens)
git commit -am "message" add + commit changes to files already tracked (new files are not included)
git diff Changes not yet staged
git diff --staged Staged changes

How to write good commit messages is covered in 5 coding guidelines that work in any language.


Exercise 2: Branching and merging

So far all the work has been on the main branch. When you try a new feature or fix a bug, you branch off and work there without touching main.

git switch -c hotfix1           # create a new branch and move to it (= git checkout -b hotfix1)
git branch                      # list. The one with * is the current branch

echo "hello" >> hello.txt
git diff                        # check what changed
git commit -am "Add hello line"

git switch main                 # back to main
git merge hotfix1               # merge hotfix1's changes into main
git branch -d hotfix1           # delete the finished branch
Branching and merging: commits pile up on a hotfix branch split off from main, then merge back into main
Figure 2. Create a branch from main, work on it, and merge it back into main when done. If the same part was changed differently on both sides, the merge produces a conflict.

Exercise 3: Resolving a merge conflict

If two branches change the same line of the same file differently, Git cannot tell which to take and reports a conflict.

git switch -c hotfix2
sed -i '1s/.*/Hi there/' hello.txt && git commit -am "Change first line"

git switch main
sed -i '1s/.*/Good morning/' hello.txt && git commit -am "Change first line differently"

git merge hotfix2
# CONFLICT (content): Merge conflict in hello.txt

Open the conflicting file and you will see this.

<<<<<<< HEAD
Good morning
=======
Hi there
>>>>>>> hotfix2
  • From <<<<<<< HEAD to =======: the content of the current branch (main)
  • From ======= to >>>>>>> hotfix2: the content of the branch being merged

Edit it to what you want, delete the marker lines (<<<<<<<, =======, >>>>>>>), and commit.

vi hello.txt                    # tidy up the final content
git add hello.txt
git commit                      # completes the merge commit

To give up on the merge, git merge --abort takes you back to the state before the merge.


Exercise 4: Remote repositories and collaboration

Git is useful even on your own, but it really shines when you collaborate through a remote repository. The best-known services are GitHub and GitLab.

GitHub GitLab
Form Cloud service (Microsoft) Cloud service + self-hosted
Characteristics The largest open-source community Built-in DevOps features such as CI/CD; good for installing on company servers
Name for merge requests Pull Request (PR) Merge Request (MR)

Places where code must not leave the company, such as semiconductor companies, often install GitLab in-house. (See Running a self-hosted GitLab: installation, HTTPS, SSH keys and mirroring.)

Registering an SSH key

To connect to a remote repository, you usually register an SSH key.

ssh-keygen -t ed25519 -C "you@example.com"
cat ~/.ssh/id_ed25519.pub       # paste this into the SSH Keys settings on GitHub/GitLab
ssh -T git@github.com           # check the connection

Connecting a remote, push and pull

# to get an existing repository
git clone git@github.com:user/project.git

# to connect a local repository to a remote
git remote add origin git@github.com:user/project.git
git remote -v                   # check the connection

git push -u origin main         # push main to the remote origin (-u: afterwards plain git push works)
git pull                        # fetch remote changes and merge them (= fetch + merge)

If authenticating every time over an HTTPS address is a hassle, you can use a credential helper, but on shared servers avoid the store helper, which saves the password in plain text; SSH keys are safer.

The merge request (pull request) flow

Merge request flow: create branch → commit → push → open MR → review and fix → approve → merge into main
Figure 3. In team work you do not push straight to main; you push a branch, get it reviewed through a merge request, and then merge.
git switch -c fix-fifo-overflow
# ... work ...
git add -A && git commit -m "Fix FIFO overflow flag"
git push -u origin fix-fifo-overflow
# → open a merge request (pull request) on the web and assign reviewers
  1. Push the branch and open an MR. Describe what you changed, why, and how you verified it.
  2. When reviewers leave comments on the code, apply them or reply with the reason you did not.
  3. Once every comment is resolved and the MR is approved, merge it into main.
  4. After merging, get the latest state locally with git switch main && git pull.

Good places to practice

Commands and tips used often in real work continue in Practical Git commands and tips: stash, add -p, insteadOf.


Summary

  • Git works as working directory → (add) → staging → (commit) → repository.
  • New work happens on a branch and is merged when done. Resolve a conflict by tidying up between the markers and committing.
  • Collaborate with a remote repository + push/pull + merge requests. Connect with SSH keys.
  • Learn on the CLI first, and build your feel for branches with Learn Git Branching.

Git (5 posts)

  1. Git Basics: From Installation to Branches, Conflicts and Merge Requests Reading now
  2. Practical Git Commands and Tips: stash, add -p, insteadOf
  3. Tidy Commit Messages with Gitmoji: the 20 Most Used
  4. Managing Large Binaries with Git LFS
  5. Exchanging Changes with diff and patch, Including the -p Option

See the full Tech Notes index

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *