Git records the history of your files so you can see what changed, try ideas safely and combine work later. By the end of this guide, you will be able to save snapshots with commits, try a change on a separate branch, merge it back and fix a simple merge conflict.

The steps follow the Git book and command reference published on git-scm.com. Every command below was run with Git 2.54 in an empty folder, and the output shown is real (long folder paths are shortened to ...). Your commit IDs will be different.

Before you start: tell Git who you are

The book’s first-time setup chapter explains that every commit stores a name and email address. Set them once:

git config --global user.name "Your Name"
git config --global user.email you@example.com

1. Create a repository

$ mkdir study-notes
$ cd study-notes
$ git init -b main
Initialized empty Git repository in .../study-notes/.git/

The git init reference says -b names the first branch. Without it, Git currently uses master, a default it says will change to main in Git 3.0.

2. Stage and commit a file

According to the book’s chapter on recording changes, files Git does not yet know about are untracked. You choose what goes into the next snapshot with git add, which stages it, and save the snapshot with git commit.

$ echo "Python notes" > notes.txt
$ git add notes.txt
$ git commit -m "Add notes file"
[main (root-commit) c847bb7] Add notes file
 1 file changed, 1 insertion(+)
 create mode 100644 notes.txt

Before the git add, git status listed notes.txt under “Untracked files”. Run git status often; it tells you which branch you are on and what is staged.

3. Work on a branch

A branch lets you build something without touching the main line of work. The git switch reference explains that -c creates a new branch and switches to it in one step. The book’s examples use the older git checkout -b for the same job.

$ git switch -c loops
Switched to a new branch 'loops'
$ echo "for loops repeat code" >> notes.txt
$ git commit -am "Add loops section"
[loops fd5d2d1] Add loops section
 1 file changed, 1 insertion(+)
$ git switch main
Switched to branch 'main'
$ cat notes.txt
Python notes

The -a option stages every tracked file that changed, so you can skip git add. Notice that after switching back, notes.txt lost its second line. When you switch branches, Git makes your folder look as it did at that branch’s last commit.

4. Merge the branch

To merge, switch to the branch you want to merge into, then name the branch to bring in:

$ git merge loops
Updating c847bb7..fd5d2d1
Fast-forward
 notes.txt | 1 +
 1 file changed, 1 insertion(+)
$ git branch -d loops
Deleted branch loops (was fd5d2d1).

The book’s chapter on basic branching and merging explains “fast-forward”: main had no new commits, so Git simply moved it forward. If both branches have new commits, Git does a three-way merge using the two branch tips and their common ancestor. It then creates a merge commit, which has more than one parent.

5. Fix a merge conflict

If two branches change the same part of the same file in different ways, Git cannot merge them by itself. Here, a branch called title and main both edited the first line:

$ git merge title
Auto-merging notes.txt
CONFLICT (content): Merge conflict in notes.txt
Automatic merge failed; fix conflicts and then commit the result.
$ cat notes.txt
<<<<<<< HEAD
My Python notes
=======
Python study notes
>>>>>>> title
for loops repeat code

The part above ======= is your current branch (HEAD); the part below comes from title. Edit the file so it holds the text you want and none of the marker lines. Then run git add notes.txt to mark it resolved and git commit to finish the merge.

Common mistakes

  • Editing after staging. Git stages a file exactly as it is when you run git add. Change it afterwards and you must add it again, or the commit gets the older version.
  • Switching with uncommitted work. The book warns that Git will not let you switch if uncommitted changes conflict with the other branch. Commit first, or keep a clean working state.
  • Merging in the wrong direction. git merge loops brings loops into the branch you are on. Check git status before merging.
  • Committing conflict markers. Search the file for <<<<<<< before running git add. During a conflict, git status also suggests git merge --abort if you would rather cancel the merge.

Most daily Git work is this loop: edit, git add, git commit. Branches let you try changes without risk, and git merge brings them back. Practicing in a throwaway folder like this one makes conflicts easier to handle when they appear in real projects.