Git

# To see which branches are merged in remote repository.
git branch --merged

# delete a branch in local repository, do it after the remote repository is merged.
git branch -d <branch_name>

# Create a new local branch.
git branch <branch_name>

# or, create a branch and checkout to that in a single cmd
git checkout -b <branch_name>

# Switch to another local branch
git checkout <branch_name>

# If we want to add some changes into latest commit, `--amend` flag should be used.
git add .
git commit --amend 

# To delete the last local commit, and turn back to previous commit.
git reset --hard HEAD~1

Q) What does origin stand for in Git terminology?

In Git, origin is the default name for a remote repository from which a local repository was cloned. It serves as a shorthand reference to that remote repository. Common uses of origin in Git commands include:

  • git push origin branch_name: Pushes changes from your local branch to the remote branch on the origin repository.
  • git pull origin branch_name: Fetches changes from the remote branch on the origin repository and merges them into your local branch.
  • git fetch origin: Fetches all updates from the origin repository without merging them.

# Adds all changes.
git add --all

# If .gitignore does not make a file untracked, use this command first to make the file untracked.
git rm --cached <file>

# List all branches
git branch -a

# To see which of your local branches are linked (tracking) to remote branches
git branch -vv

# Fetches all branches from the origin (if master is not the only branch)
git fetch --all

Q) What do we mean when we say checkout?

In Git, “checked out” means that a branch or commit has been selected to be active in your working directory. When you “check out” a branch, Git updates the files in your working directory to match the state of that branch and sets that branch as the one you’re currently working on.


# Create a local branch that tracks an origin `origin/feature`, and updates
# the working directory to match the `feature` branch

git branch feature origin/feature

# See commit logs
git log --pretty=oneline

# switch to main branch
git checkout main

# merge the feature branch into current branch 
git merge my-experimental-changes

# After merging the feature branch into main, delete the feature branch locally
git branch -d my-experimental-changes