git commit, explained

How to save progress using git version control?

Tom Parandyk

--

This is a mac tutorial

Have you ever wondered how do developers write code together and save the progress to the same files from different computers?

Did you ever hear word “conflicts” and had no clue what’s that about?

Do you want to have a quick reference guide to how to save your changes to the project’s repository?

If you don’t have git repository or don’t know what is it, check this post on how to get one:

To learn more about conflicts you might want to check this post:

Also, ☝️that post has git installation commands in case if you never installed git before on your computer.

Here’s git commit explained

Say you’ve got yourself a git project (☝️via git clone) and now want to make changes. You opened a file and changed some text or a label on a button. You saved the file. Now what? Is that all?

Well, that’s a big progress. You should feel proud. 🎉 Not many people get to where you are. If you know the basis of git or read the intro post you probably figured out that the change you’ve saved is only saved on your local computer and needs to be pushed and merged to master. 👏 I salut you!

Before pushing the changes you have one more thing to do, let git know what are the changes you’ve made.

There are two ways of pushing new changes to the git repository (repo, for short):

  • Pushing directly to the master branch — simpler process, but can result in rollbacks and many conflicts, if new changes broke something or you don’t communicate well with your team
  • Creating new branch and pushing with a Pull Request (PR) — more complex process, but changes made on the branch will not affect the master until merge, which makes it safer

Play time 🕹 (pushing to master)

  1. Go to the Terminal app > navigate to your git project folder

😎 INSTANT XP BOOST: To get quickly into a specific folder within the Terminal app navigate to the folder in the Finder app, then in the Terminal write cd leave one space after “cd” and drag and drop folder on the Terminal window. Press Return and you will go directly to the folder.

XP BOOST: go to the folder

2. To get the most recent state of the remote write git pull. It’s a good practice to do it every time you starting working on a project in case if someone else from your team recently pushed changes.

3. Open any file, make a change, and save it. You can list all the files in the folder with ls command.

😎 INSTANT XP BOOST: You can open any file from Terminal by writing open and name of the file. You can also autocomplete the file names using Tab button after typing just the beginning of the file name.

4. To see which files has been affected by your changes write git status . If no files were changed you should see:

Say you’ve made a change to one file. You should see:

If you’ve added a new file that is not tracked by git yet you would see:

5. If you only changed existing files and didn’t add new files you can skip this step. In case if you’ve added new files you need to tell git to start tracking them. Write git add . (space and full stop). Press Return. Terminal won’t print any confirmation. If you run git status again, you should see:

6. Time to commit the changes. You need to tell git and everyone else on the team what was the change you’ve made. Write git commit -am"describe the change you made". Press Return. You should see a confirmation:

7. Final step is to push the new changes to the remote. Write git push and press Return. You might need to confirm with the password.

In case if you forgot to run git pull before you saved your changes and remote has changes you don’t on your local, you will see this error:

Write git pull to resolve your local version being outdated. You will see this message now:

It’s quite confusing to get out of it without following these exact steps.

Type colon : (shift + semicolon) — a command line will appear

Now write wq which stands for write and quit . Press Return.

You should see:

This means that the changes were pulled from the remote and your local is now up-to-date with the latest state of remote.

It’s not over yet. One more thing is left.

Write git push again. You should see a confirmation:

And there you go, you’ve just made your first commit to a git project!

Bravo! 🎉

Believe it or not, that was the easy way of saving your changes as a developer. I bet ease of autosaving in Google Docs got a brand new meaning for you now, is that right? 😏

Play time 🕹 (pushing to a new branch)

  1. Go to the Terminal app > navigate to your git project folder
  2. Pull the latest changes from the remote git pull
  3. Check if you don’t have any uncommitted changes from before.
    Write git status and you should see:

4. Create new branch as a copy of the master branch so you can make your changes without breaking anything for everyone else.

Write git checkout -b name-of-your-new-branch . Press Return.

Usually the name would be somewhat connected to the change you will make.

You should see:

Notice that your terminal path will now show the name of the branch you switched to (switching in git terminology is called checkout).

Time to make some changes 💾

5. After making changes and saving all files you can run git status to double check that all your files are tracked by git (refer to point 4 and 5 from the first section for more details). You can also just run git add . to add all the files just in case.

You should see:

6. Time to commit the changes to let git know what happened. The message from between the quotes will be added to the Pull Request for everyone to see, so try to be as descriptive as possible.

Write git commit -am"describe the change you made" . Press Return.

You should see a confirmation:

7. FYI, you can always run git status command. For example, at this point in the process you should see:

Could be confusing, because it looks like everything is done, right?

Well, we still need to push the branch and changes to the repo.

Write git push . Press Return.

You should see a suggestion from git with the syntax of the correct command

I named my branch text-changes so git is giving me the full command together with autofilled branch name. Thank you 🙏

Just copy the full command and run it. You might be asked for the password.

After pushing the branch you should see:

8. It’s not over yet! Next step depends on the solution your project is hosted with. Mine is hosted with GitHub, so when I press-hold command button ⌘ and click the URL given to me by git I will be redirected to GitHub to create the PR (Pull Request) — this handy shortcut is available only in iTerm.

In Terminal you need to copy the path:

GitHub automatically prepared a PR form for me and filled it with my commit message. Nice 😃

You should now add reviewers to notify your team members that you’ve made a change and they should look if everything is ok?

Finally, click conveniently big green button to create the Pull Request.

You should see a confirmation:

Your open Pull Request is waiting for the review now. If everything goes well 🤞the reviewers will approve it and you will be able to merge it with the master branch.

At that point you will see the confirmation that your PR was merged successfully:

You’re done. Your changes are pushed and merged! You can go celebrate 🎉

Or… keep at it. Up to you. Good luck!

Here’s a brief explanation of git branches you might find useful:

A. Short list of all commands in the order of using when on master branch:

git pull
git status
git add .
git commit -am"describe the change you made"
git push

B. Short list of all commands in the order of using when creating new branch:

git pull
git status
git checkout -b name-of-your-new-branch
git add .
git commit -am"describe the change you made"
git push

git will suggest the command with the name branch, copy it:

git push --set-upstream origin name-of-your-new-branch

👏 if you find it useful

--

--

Tom Parandyk

Product designer, eager engineer, strategist, wild innovator, proud dad, creative leader, aspiring musician.