close icon
daily.dev platform

Discover more from daily.dev

Personalized news feed, dev communities and search, much better than whatโ€™s out there. Maybe ;)

Start reading - Free forever
Start reading - Free forever
Continue reading >

Git Commit Repository Basics

Git Commit Repository Basics
Author
Nimrod Kramer
Related tags on daily.dev
toc
Table of contents
arrow-down

๐ŸŽฏ

Learn the basics of Git commit, staging changes, pushing commits, and undoing changes. Understand how Git commits work and how to use them effectively.

Understanding Git commit is essential for managing and tracking changes in your projects. Here's a quick guide to get you started:

  • Git Commit: It's like saving your project's progress at a specific point, including who made changes and when.
  • Staging Changes: Use git add to select changes you want to commit.
  • Committing: With git commit, you save your staged changes, along with a message describing what you did.
  • Pushing Commits: Use git push to share your commits with others on platforms like GitHub.
  • Undoing Changes: Use git revert for undoing specific commits safely, keeping your project history clean.

By regularly committing changes, you create a detailed history of your project, making collaboration and tracking easier. Each commit includes crucial information like the commit message, author, and timestamp, helping everyone understand the project's development. Remember, commits are local first; use git push to share your work with the team.

What is a Git Commit?

Imagine you're working on a project and you want to save your progress, kind of like hitting the save button in a video game. That's what a Git commit does. It's a way to save a copy of your work at a certain point.

Here's how it works: After you've changed some files in your project, you use the git add command to tell Git, "Hey, I want to save these changes." This step is like picking which photos you want to keep from your camera roll.

Next, you use the git commit command to actually save those changes. Doing this does a few things:

  • It takes a picture of all the changes you've decided to save
  • It notes down who made the changes, when they did it, and allows you to add a short note about what you did
  • It gives this save point a special code so you can find it again
  • It keeps this save point in your project's history

So, in simple terms, a Git commit:

  • Saves a copy of the changes you've picked
  • Is like a save point in your project that you can go back to
  • Includes info like who made the changes and when
  • Gets a special code for easy finding

By saving your work often, you create a history of your project. This is super helpful for keeping track of what you've done, explaining why you made changes, going back to older versions if you need to, and working together with others.

When you save your work with a commit, you also write a short message about what you did. This message helps anyone looking at the project history understand what each save point is about.

Some important parts of a Git commit include:

Commit Message: A quick note explaining the changes. It's like a label on a photo album.

Author: The person who made the commit. Git knows who this is based on the setup.

Date/Timestamp: The date and time when you made the commit.

Commit ID/Hash: A special code that makes each commit unique.

Snapshot of Staged Changes: The actual changes you decided to save.

In short, making a commit is like saving your progress in a project, with some extra details to help keep track of everything. It's a key part of working with Git and helps everyone understand what's happening in the project.

How Git Commits Work

Git commits take a full picture of your files, let you pile up changes before showing them off, and help manage the history of changes when you're working on different parts of your project.

File Snapshots vs Diffs

While some other tools remember only the differences from one version to another, Git remembers the whole file each time you make a change. This makes it easier to mix and match changes because Git has the complete picture, not just the bits that changed. It's like having the full recipe instead of just the adjustments.

Two Phase Commit Process

When you're ready to save your work in Git, there are two steps:

  1. First, you use git add to pick which changes you want to keep. Think of it as gathering the ingredients for a recipe.
  2. Then, you use git commit to actually save those changes, like writing down the recipe. This step also saves who made the changes and allows you to add a note about what you did.

So, the git commit command only saves the stuff you've already marked with git add.

Local First, Then Remote

Your changes are saved on your computer first. If you want others to see them, you have to use git push to send your changes to the shared project space. This lets you make a bunch of changes and get them just right before anyone else sees them.

Creating Commits

Staging Changes

To get changes ready for a commit, use the git add command. You can add specific files by typing:

git add file1.js file2.css

Or, to pick changes within files more carefully, you can use:

git add -p

This lets you look at each change and decide if you want to add it to the commit.

Committing Staged Changes

After staging your changes, you can commit them with:

git commit -m "Implement new footer design"

Using -m lets you type your commit message right there. Try to make your message clear but brief.

Amending Previous Commits

If you need to fix your last commit, maybe to change the message or add something you forgot, use:

git commit --amend

This will replace the last commit with a new one. It's best to do this only with commits that haven't been shared yet.

Crafting Commit Messages

Good commit messages are important. Hereโ€™s a simple guide:

  • Use a short, direct sentence
  • Keep the main message under 50 characters
  • Add a blank line before any extra details
  • Explain why you made the changes, if needed
  • Mention any related issues

Commit Early, Commit Often

It's a good idea to commit small changes more often. This way, you have lots of points you can go back to if needed. Waiting too long to commit can make it harder to fix mistakes.

sbb-itb-bfaad5b

Pushing Commits

Pushing commits means sharing the updates you've made on your computer with others on platforms like GitHub. This lets other team members see and use your changes.

Adding Remote Repositories

Before you can share your updates, you need to link your local project to an online repository. You do this with a command that looks like this:

git remote add origin https://github.com/user/repo.git

This command tells your computer where your online project lives. 'Origin' is just a shorthand name for this online spot. You only need to set this up once for each project.

Pushing Commits to Remote

After setting up, you can send your updates online with:

git push origin main

This command sends your updates from your local 'main' branch to the 'main' branch online ('origin'). It's like updating the shared project space with your latest work.

If you're working on a different part of the project, just replace 'main' with your branch's name.

Synchronizing Commits

To make sure your work matches up with the shared project:

  • Regularly send your updates online with git push
  • Before adding new changes, grab the latest updates from online with git pull
  • If there are any conflicts, fix them so your work and the shared project match
  • After fixing, send your updates online again with git push

This routine keeps everyone's work aligned and avoids mix-ups when working together. It's a good practice to update your work online often so everyone stays on the same page.

Undoing Commits

Sometimes you might need to undo your last action in Git. This part talks about how to do that without causing problems.

Reverting Specific Commits

If you need to go back on changes from a certain commit, you can use git revert:

git revert <commit-hash>

This command creates a new commit that does the opposite of what the original commit did. The cool thing is, the original commit stays in your history, so it's a safe way to undo things.

Here's what you should remember about git revert:

  • It basically 'un-does' the changes from a specific commit.
  • It keeps your project history clean by adding a new commit for the undo.
  • It's the best option when you've already shared your changes with others.

Reverting is better than using stronger commands that might mess up your project history.

Using Git Reset Safely

git reset is a bit like hitting the undo button, but it can actually remove commits, which might make you lose your work or mess up when working with others.

Before you use git reset, make sure to:

  • Save or stash any work you don't want to lose.
  • Understand the different types of reset (--soft, --mixed, --hard).
  • Check that you're not deleting anything you've already shared.
  • Remember you can use git reflog to find lost commits.

Resetting is usually for when you've made a mistake on your own and nobody else has seen it yet. For most other situations, sticking with git revert is a safer bet.

Summary

Git commits are like saving your project's progress at certain points. They help you keep track of what you've done step by step. Here are a few things to keep in mind about Git commits:

  • Commits are local first: Your commits are saved on your computer first. You need to use git push to share them with others on the internet.
  • Commit often: It's a good idea to save your work often with small commits. This makes it easier to see what changed and fix mistakes if needed.
  • Good commit messages: Writing clear messages about what you changed and why helps everyone understand the project's history better.
  • Amend commits: If you need to fix your last commit, you can change the message or add something you missed with --amend.
  • Undo commits safely: If you need to undo something, git revert is usually the safest way, especially if others have seen your changes. Use git reset carefully, mostly for things only you have seen.

The basic steps for using commits are:

  • Make changes to your files
  • Get your changes ready with git add
  • Save your changes with git commit
  • Share your changes with git push

By doing these steps regularly, you keep a good record of your project, make working with others smoother, and can easily fix mistakes.

Some important Git commands we talked about include:

  • git add - Gets your file changes ready to save
  • git commit - Saves your changes on your computer
  • git push - Shares your saved changes on the internet
  • git pull - Brings changes from the internet to your computer
  • git revert - Safely undoes specific changes

Getting the hang of commits and how to use them is a big part of making the most out of Git's tools for keeping track of your project's versions.

How to commit a git repository?

To save changes in a Git repository, just do these steps:

  • Get your changes ready for saving by using git add. This step picks the files or changes you want to include in your next save.
  • Save the changes with git commit -m "commit message". The -m lets you add a short note about what you did right away.
  • Make sure your commit message is clear and explains both what changes you made and why.
  • Share your saved changes with others by pushing the commit to a remote repository using git push origin branch-name.

Hereโ€™s how you might do it:

git add file1.js 
git commit -m "Fix bug in file1.js"
git push origin main

How to commit to existing repository git?

To add your work to an already started Git repository:

  • First, clone the repo that already has a README or some files.
  • Then, move your project files into this cloned folder.
  • Get these changes ready with git add .
  • Save them with git commit -m "commit message"
  • Finally, share your work by pushing the commits to GitHub with git push

This way, you add your files, save them locally, and then make them available in the remote repository on GitHub.

How do I commit to a remote repository in git?

To send a local save (commit) to a place where others can see it (remote repository):

  1. First, make sure you've saved your changes locally.
  2. Then, use git push -u remote-name branch-name to share them.

For instance:

git commit -m "Add new feature"
git push -u origin main 

This sends your local saves from the main branch to the remote place called origin.

How do I commit and push changes in git?

To save and share changes using Git:

  • Get your changes ready: git add .
  • Save them: git commit -m "commit message"
  • Share them: git push

You can also save and share in one go:

git commit -m "commit message" && git push

This saves your changes locally and then immediately sends them to the remote repository you've set up.

Related posts

Why not level up your reading with

Stay up-to-date with the latest developer news every time you open a new tab.

Read more