Git CLI

This guide will help you navigate common Git commands. You can run these commands directly from the Ubuntu terminal or from VSCode's terminal. Ensure that you're inside the project directory before executing any Git commands.

1. Check the Status of Your Project

git status

This will show you which files have been modified, added, or deleted, but not yet committed.

2. Stage Files for Commit

Before committing changes, you need to stage them. You can stage either all files or specific files.

Staging allows you to selectively choose which changes will be included in your next commit.

3. Commit Changes to the Active Branch

After staging your changes, commit them to the active branch:

git commit -m "your commit message"

This command saves your changes locally on your machine. Each commit acts as a snapshot of your project at that point in time. It's recommended to commit regularly, even if your changes are incomplete. Frequent commits allow you to roll back to earlier, stable versions of your project.

NOTE: You do not need to push every commit immediately to the remote repository. It's fine to commit frequently and push at a later time.

4. Undo Staged Changes (Before Committing)

If you mistakenly staged a file or need to revert changes, you can undo the staging. This won't delete the file, but it will revert it to its state from the last commit.

This command helps restore files to their previous state before you committed any changes.

5. Push Commits to a Remote Repository

Once you've committed changes locally, you can push them to a remote repository, such as GitHub or GitLab:

git push -u origin branch-name

The -u flag sets the upstream branch, meaning future pushes from this branch will default to the remote branch specified.

Pushing to an Existing Remote Branch

If the branch you're working on already exists in the remote repository (e.g., GitHub), you don’t need to set the upstream branch with the -u flag every time. Instead, you can simply push using:

git push origin branch-name

However, if you’ve previously set the upstream branch (using the -u flag), you can omit the branch name and push with:

git push

This will push the current branch to the corresponding branch on the remote repository. If the remote branch and the local branch have diverged, you may need to pull the latest changes from the remote repository before pushing.

To ensure your local branch is up-to-date, use:

git pull origin branch-name

This will fetch any updates from the remote branch and merge them into your current branch.

6. Switch Between Branches

In Git, you may need to switch between different branches to work on different features or bug fixes. Here's how to switch branches or create a new branch:

If you encounter an error message like:

error: pathspec 'branch-name' did not match any file(s) known to git

This indicates that your local repository is out of sync with the remote repository. To resolve this, run:

git pull

This command will fetch and integrate the changes from the remote repository, updating your local copy.


Visual Studio Code

VSCode provides a great interface for Git.

  1. In the bottom left corner, you’ll find the branch you are currently on.

image (1)

  1. Select the Source Control section or use the keyboard shortcut Ctrl+Shift+G.

    This section offers many options. While I’m not extremely familiar with this interface, I understand the basic utilities it provides.

image (2)

  1. Here’s how to access the options.

image (3)

  1. To pull the latest commits in the branch from the repository (usually done before a push or branch switch): This action will not delete the changes you’ve committed locally, but it’s recommended to commit or stash all changes before running this command.

image (4)

If there’s a conflict, it will be displayed with a visual interface.

image (5)

  1. A commit (including staging all changes) can be done as follows.

image (6)

  1. To push on the branch you are on.

image (7)

You can view all changes made. If the changes are not displayed, you may need to refresh. To add these changes to the commit, you need to stage them by clicking the + icon.

image (8)


IMPORTANT: Do not push or merge on the main branch.

More Information

Checkout this page for information on conventional commits.