Everyday Git Commands

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Once you’ve set up your repo, it’s time to get to work. The standard workflow for Git is as follows:

  1. Make a branch to hold your work and give it a reasonable name.
  2. Make your changes.
  3. Stage files you want to add to the repository.
  4. Commit the changes with a useful commit message.
  5. Merge the branch.

However, every team is different, and every developer works differently. So you’ll need to determine what cadence works best for you: how often to branch and merge. But the flow will generally follow the steps above.

Branch

Before you start working, the first step is to make a branch and give it a useful name. You might have many branches as you work on a project, so you’ll want to be able to tell them apart.

git branch <new_branch_name>
git branch
git switch <new_branch_name>
git switch -c <another_new_branch>

Making Your Changes

Once you’re in the working directory of a clean branch, you can make any changes you like. You can add, delete, rename, or edit files, anything you want. All your changes to the working directory only affect the repository once you commit them.

Staging Changes

Don’t put all of your changes in a single commit. Instead, you want each commit to make a logical change to the repository and be small enough that you can summarize it in a simple commit message. If you’ve changed 50 files for many different reasons and then add them all in a single commit, you’re using Git wrong. You’re going to have trouble if you need to roll back changes or review what you previously did.

git status
git add <name_of_file>
git add <name_of_file> <name_of_another_file>
git add *.jpg
git add .
git restore --staged <name_of_file>

Making a Commit

When you’ve staged all the files that make up a coherent change to your project, it’s time to add them to the repository with a commit. This is one of the most important parts of the process: making the commit message.

git commit -m 'Your commit message goes here'
git commit -m 'Your commit title' -m 'A more detailed description'
git commit

Stashing

Sometimes, you’re in the middle of working and need to save your changes, but you’re not ready to commit them. In this case, you can clean the working directory using git stash. Stash lets you store your work in progress. Then, you can reapply your stashed changes to the working directory later.

git stash -m 'some message about the changes'
git stash apply 0
git stash drop 0
git stash pop
git stash show -p <index>

Tagging

The last command that’s similar to commit is git tag. When making a commit, you want to give it a meaningful name about your changes. Later, if you want to mark a particular commit as important, you tag it. An example of this is when you want to mark the build that was used for a particular release:

git tag 'v1.2-release' <some_abbreviated_hash>
git tag -d <tag_name>
See forum comments
Download course materials from Github
Previous: Configuration Demo Next: Git Commands Demo