Reflect on your 2024 year in code

Common Git concepts and questions

Greg Foster
Greg Foster
Graphite software engineer
Try Graphite


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Git is an essential version control system for developers, but common challenges often arise when working with branches, commits, and merges. Whether you’re a beginner or a seasoned developer, understanding these key concepts helps streamline your development workflow and avoid common pitfalls.

This guide answers the most frequently asked Git questions with actionable solutions and step-by-step examples.

Branches allow you to work on isolated changes without impacting the main branch.

To create and switch to a new branch:

Terminal
git checkout -b <branch-name>
  • git checkout -b: Creates a new branch and switches to it immediately.
  • <branch-name>: Replace with the desired branch name.
Terminal
git checkout -b feature/add-new-button

You’ll now be on a branch named feature/add-new-button.

To view the list of branches:

Terminal
git branch

Switch back to the main branch using:

Terminal
git checkout main

Committing changes ensures your work is saved in the repository’s history.

Stage changes with git add and commit with git commit.

  1. Stage specific files:

    Terminal
    git add <file-name>
  2. Stage all changes:

    Terminal
    git add .
  3. Commit changes:

    Terminal
    git commit -m "Your descriptive commit message"
Terminal
git add index.html
git commit -m "Add navigation bar to homepage"

Merging brings changes from one branch into another, but conflicts can occur when the same lines of code are modified.

Follow these steps for a clean merge:

  1. Ensure your working directory is clean:

    Terminal
    git status
  2. Switch to the branch where you want to merge changes:

    Terminal
    git checkout main
  3. Merge the target branch:

    Terminal
    git merge <branch-name>

Merging feature/login into main:

Terminal
git checkout main
git merge feature/login

If there are no conflicts, Git will complete the merge automatically.

Merge conflicts happen when Git can’t automatically reconcile differences between branches.

  1. After running git merge, Git will notify you of conflicts:
Terminal
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
  1. Open the conflicted file, look for <<<<<<< and >>>>>>> markers:

    Terminal
    <<<<<<< HEAD Current code from main branch ======= Code from feature branch
    >>>>>>> feature/login
  2. Edit the file to keep the desired changes.

  3. Stage and commit the resolved file:

    Terminal
    git add index.html
    git commit -m "Resolve merge conflict in index.html"

You might need to undo a commit if you made a mistake.

To undo the last commit but keep changes:

Terminal
git reset --soft HEAD~1

To undo the last commit and discard changes:

Terminal
git reset --hard HEAD~1
Terminal
git reset --soft HEAD~1
# Now you can fix the changes and recommit

Rebasing re-applies your commits on top of another branch, resulting in a cleaner commit history.

  1. Switch to the branch you want to rebase:

    Terminal
    git checkout feature/login
  2. Rebase onto the main branch:

    Terminal
    git rebase main
  3. Resolve conflicts if prompted, then continue rebasing:

    Terminal
    git rebase --continue
  4. Push the changes to your remote repository:

    Terminal
    git push --force-with-lease

Stashing saves your changes without committing, allowing you to work on something else temporarily.

  1. Stash your changes:

    Terminal
    git stash
  2. View your stashed changes:

    Terminal
    git stash list
  3. Apply the stashed changes:

    Terminal
    git stash apply
  4. Remove the stash:

    Terminal
    git stash drop
Terminal
git stash
# Work on another branch
git checkout main
# Return to your changes
git checkout feature/login
git stash apply

You can delete branches when they are no longer needed.

Delete a local branch:

Terminal
git branch -d <branch-name>

Force delete a local branch:

Terminal
git branch -D <branch-name>

Delete a remote branch:

Terminal
git push origin --delete <branch-name>
Terminal
git branch -d feature/login
git push origin --delete feature/login

Viewing your commit history is crucial for tracking changes over time.

Use git log to view the commit history:

Terminal
git log

For a more concise view:

Terminal
git log --oneline
Terminal
git log --oneline

Output:

Terminal
f5d4a1b Add login feature
1a2b3c4 Fix typo in homepage
0d1e2f3 Merge feature/navbar

By mastering these Git solutions, you can address common branching, committing, and merging challenges. Streamline your workflow and collaborate more effectively with your team by implementing these best practices.

Built for the world's fastest engineering teams, now available for everyone