DEV_TOOLBOX

Git

Git is a distributed version control system. It is used to track changes in source code and manage different versions of the same project.

Installation

scoop install git

Usage & Commands

git clone

Clone a repository to your local machine.

git clone https://github.com/<your-username>/<your-repo>.git

git add

Add all changes to the staging area.

git add .
# or
git add -A

Add a specific file/directory to the staging area.

git add *filename/path*

TIP

(*) stands for a wildcard and will match any file or directory. Running git add * or git add . will add all modified/new files and directories to the staging area.

git restore

Restore a file/directory from the staging area (git add-ed file/s).

git restore --staged *filename/path*

May also be used to restore a file/directory from the working directory. ("Discard changes")

git restore *filename/path*

TIP

Same as git add, you can use wildcards to restore multiple files/directories.

git commit

Commit all changes to the local repository.

git commit -m "commit message"

git push

Push all commits to the remote repository.

git push

Push a specific branch to the remote repository.

git push origin <branch-name>

git pull

Pull all commits from the remote repository.

git pull

Pull a specific branch from the remote repository.

git pull origin <branch-name>

Flags

Flags that can be used with git pull in specific scenarios.

  • --rebase: Rebase the current branch on top of the upstream branch.
  • --autostash: Automatically stash/unstash changes before/after a pull.

git log

Show the commit history.

git log

Flags

Flags that can be used with git log in specific scenarios.

  • --oneline: Show the commit history in a single line.

git reflog

I rarely use this command, but in cases like when you accidentally delete a branch without pushing it to the remote repository, you can use this command to restore the deleted branch.

git reflog

git rebase

Rebase the currently active branch.

git rebase <branch name>
# Assuming we're we want to rebase our current
# branch to the latest changes from the main
git rebase main

If we're on a different branch, we can specify the branch name and we want rebase a specific branch, we can use the following command:

git rebase <branch name> <branch name>

git branch

List all branches.

git branch

Create a new branch.

git branch <branch name>

Delete a branch.

git branch -d <branch name>
# or
git branch -D <branch name>

Rename a branch.

git branch -m <old branch name> <new branch name>
# or if we're on the branch
git branch -m <new branch name>

git checkout

Switch to an existing branch.

git checkout <branch name>

We can also checkout to a specific commit by copying the commit hash from the commit history using git log or git reflog.

git switch

Is a somewhat more advanced version of git checkout.

Switch to an existing branch.

git switch <branch name>
# it is essentially the same as
# git checkout <branch name>

Unlike git checkout, switch can bring local changes to the branch you're switching to.

Flags

Flags that can be used with git switch in specific scenarios.

  • -c: Create a new branch if it doesn't exist and switch to it. (It is essentially the same as creating a new branch branch using git branch and then switching to it using git checkout.)

git stash

Stash changes.

git stash

Flags

Flags that can be used with git stash in specific scenarios.

  • -u: Include untracked (new) files in the stash.

git stash pop

Readd the changes from the stash.

git stash pop

Development workflow

  1. Clone a repo using git clone
  2. After making changes, use git add to add the changes to the staging area
  3. Switch to a new branch using git switch -c <branch name>
  4. Make sure the main/master branch by running git fetch origin main:main (This command is not on the list above, as we will only use it this one time).
    • or if you want to update a different branch, run git fetch origin <branch name>:<branch name>
  5. Use git rebase main to rebase the current branch on top of the main branch.
  6. Use git push to push the changes to the remote repository