DEV Community

Cover image for Understanding Git
Ctrl Njenga
Ctrl Njenga

Posted on

Understanding Git

Git

Git was created by Linus Torvalds (who also created Linux) in 2005. Git is a command line tool which enables us to keep track of changes in our code, a version control system - software that helps you track changes to files over time. It's especially popular among programmers for managing code, but not limited to it can also be used for any files.

Version control is a system that tracks changes to files over time. This lets you go back to earlier versions whenever you need. It's like having a detailed "undo" button that remembers everything, and it also lets you create different versions of your work.

Hold that thought as we’ll come back to it later, here is what Git does in a nutshell:

Enables you to Track changes: Git saves different versions of your work, like taking photos of your project at different times. You can see what you changed and go back to an older version if you need to.

Enables collaboration: Multiple people can work on the same project at the same time. Git helps combine everyone's work and figures out what to do when two people change the same thing.

Works locally: Git saves everything on your own computer, so you don't need internet to use it and everything works quickly.

Branching: You can create separate copies to try out new ideas without messing up your main work, then add them back in when you're ready.

. Git runs on your computer, but websites like GitHub, GitLab, and Bitbucket let you store your work online so you can share it with others.

The basic steps are: make changes to your files, choose which changes to save (called "staging"), and then save them with a note about what you changed (called "committing").

Back to version control, one may ask why is it important? Well I would like to take a different approach and look at why it may not be a good idea to work without it and here is why:

Without version control, you'd face several frustrating problems:

1. The "Final Version" Nightmare
Your folder ends up looking like:

  • project.doc
  • project_final.doc
  • project_final_v2.doc
  • project_ACTUALLY_final.doc
  • project_final_USE_THIS_ONE.doc
  • project_final_december_12.doc

You waste time figuring out which is actually the latest version, and you can't easily see what changed between them.

2. Lost Work
You spend hours on a feature, save over your previous work, then realize your original approach was better. Without version control, that work is gone forever. There's no way to get it back.

3. Collaboration Chaos
You and a coworker are both editing the same website. You email files back and forth:

  • You edit the homepage and email it at 2pm
  • Your coworker edits the same homepage (using an old copy) and emails it at 3pm
  • Their version overwrites all your changes
  • You spend hours trying to manually merge both sets of changes, retyping everything you lost

4. The "What Broke?" Mystery
Your app worked fine yesterday but is broken today. Without version control, you have no record of what changed. You can't compare today's code to yesterday's working code, so you're just guessing at what went wrong.

5. Fear of Experimentation
You want to try a risky redesign but you're terrified because there's no safety net. If it doesn't work, you'll have to manually undo everything or lose all your experimental work.

6. No Accountability
In a team, someone introduces a bug but nobody knows who made the change or why, making it much harder to fix and learn from mistakes.

Here's how to push code to GitHub:

First Time Setup (for a new project)

1. Create a repository on GitHub

  • Go to github.com and sign in
  • Click the "+" icon in the top right, select "New repository"
  • Give it a name, choose public/private, then click "Create repository"

2. Initialize Git in your project folder

cd your-project-folder
git init
Enter fullscreen mode Exit fullscreen mode

3. Add your files

git add .
Enter fullscreen mode Exit fullscreen mode

(The . adds all files, or you can specify individual files)

4. Make your first commit

git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode

5. Connect to GitHub

git remote add origin https://github.com/YOUR-USERNAME/YOUR-REPO-NAME.git
Enter fullscreen mode Exit fullscreen mode

(Replace with your actual GitHub username and repository name)

6. Push to GitHub

git branch -M main
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Ongoing Work (after initial setup)

When you make changes and want to push them:

1. Check what changed

git status
Enter fullscreen mode Exit fullscreen mode

2. Stage your changes

git add .
Enter fullscreen mode Exit fullscreen mode

(or git add filename.txt for specific files)

3. Commit with a message

git commit -m "Describe what you changed"
Enter fullscreen mode Exit fullscreen mode

4. Push to GitHub

git push
Enter fullscreen mode Exit fullscreen mode

Common issues:

  • If you get authentication errors, you may need to set up a Personal Access Token (GitHub no longer accepts passwords for Git operations)
  • If someone else updated the repo, you might need to git pull first before pushing

That's basically it! Your code is now live on GitHub where others can see it, collaborate, or you can access it from anywhere.

**

Pulling Code from Github

**
We pull code to get the latest changes that others (or you from another computer) have pushed to GitHub.
Here are the common scenarios:

Your teammate added new features - pull to get them
You worked on your laptop yesterday and pushed - pull on your desktop today to continue
Before you push your own changes, you need to pull first to avoid conflicts

Here's how to pull code from GitHub:

First Time - Cloning a Repository

If you don't have the code on your computer yet:

1. Get the repository URL

  • Go to the GitHub repository
  • Click the green "Code" button
  • Copy the HTTPS URL (looks like https://github.com/username/repo-name.git)

2. Clone it to your computer

git clone https://github.com/username/repo-name.git
Enter fullscreen mode Exit fullscreen mode

3. Navigate into the folder

cd repo-name
Enter fullscreen mode Exit fullscreen mode

Now you have the entire project on your computer!

Ongoing - Getting Latest Changes

If you already have the code and want to get updates:

1. Make sure you're in the project folder

cd your-project-folder
Enter fullscreen mode Exit fullscreen mode

2. Pull the latest changes

git pull
Enter fullscreen mode Exit fullscreen mode

This downloads and merges any new changes from GitHub into your local copy.

Pulling from a Specific Branch

If you want changes from a different branch:

git pull origin branch-name
Enter fullscreen mode Exit fullscreen mode

What if You Have Local Changes?

If you've made changes locally and try to pull:

  • No conflicts: Git will try to merge automatically
  • Conflicts exist: Git will tell you which files conflict. You'll need to:
    1. Open the conflicting files
    2. Manually resolve conflicts (Git marks them with <<<<<<<, =======, >>>>>>>)
    3. Stage the resolved files: git add filename
    4. Commit the merge: git commit -m "Resolved merge conflicts"

Tip: If you want to save your local changes but pull cleanly:

git stash        # Temporarily save your changes
git pull         # Get updates
git stash pop    # Reapply your changes
Enter fullscreen mode Exit fullscreen mode

Tracking changes using Git

Why You Should Track Changes

  1. To Undo mistakes easily If you break something, you can quickly go back to a working version. 2.Keep a record of your work You'll be able to see what you changed yesterday or last week. 3.Know who made each change This helps you ask the right person questions when needed. 4.Experiment without risk Try new ideas knowing you can always return to what worked. 5.Debug problems faster You can see exactly what changed when something stopped working. 6.Collaborate smoothly Everyone's work is saved, so no one loses their changes. Bottom line: Think of it as unlimited undo with notes explaining what you did and why.

How to Track Changes Using Git

1. Check what changed

git status
Enter fullscreen mode Exit fullscreen mode

Shows which files you modified, added, or deleted.

2. See specific changes in files

git diff
Enter fullscreen mode Exit fullscreen mode

Shows exactly what lines were added or removed.

3. Stage files you want to track

git add filename.txt    # specific file
git add .               # all files
Enter fullscreen mode Exit fullscreen mode

4. Save the changes (commit)

git commit -m "Describe what you changed"
Enter fullscreen mode Exit fullscreen mode

5. View your history

git log
Enter fullscreen mode Exit fullscreen mode

Shows all past commits with dates, authors, and messages.

6. See changes in a specific commit

git show commit-id
Enter fullscreen mode Exit fullscreen mode

Git remembers everything, so you can always go back and see what changed, when, and why.

Top comments (0)