I wish I'd known these git commands earlier

Pranav BawgikarPranav Bawgikar
4 min read

[30]

Introduction

Git is a free and open source distributed version control system designed to handle everything from small to large projects with speed and efficiency. Git has nearly 300 commands all with their own specific functions. With commands like push, pull, fetch, commit and others, it gets tricky.

Understanding Version Control

Version Control refers to the process of saving different files or ‘versions’ throughout the various stages of a project. A local repository is storage of your project and files on your own computer and a remote repository is a virtual storage which is used to share code with others and maintain across a group.

Git Commands

Git Branching

According to the official documentation from Git:

“Branching means you diverge from the main line of development and continue to do work without messing with that main line.”

This means a new copy of the main repository is made and after it is split off from its original path and it can be modified as required which will not mess with the original branch. Branches can be an extremely powerful tool in collaborating as it allows multiple people to work on the same files at once without affecting other changes.

To make a branch, the command is:

git branch your-branch-name
git checkout your-branch-name

The above commands create a new branch and switch the path of your commits from the main branch to the newly made branch.

Use the following command to both create and then switch too your new branch:

git switch -c your-branch-name

Git Branch Pull

When you are finished making changes to someone else’s code, you can share them with the original owner via a ‘pull request.’

Git Branch Merge

After a change is ready to be published permanently to the main branch, we merge the branch onto the main, using the merge command. A merge should be executed by switching to the branch you want to merge into.

git checkout main
git merge your-branch-name

Stashing Changes

git stash -> # Temporarily saves your uncommitted changes
git stash list -> # Lists all stashed changes
git stash apply stash@{0} -> # Applies the specified stash without removing it
git stash pop -> # Applies the most recent stash and removes it from the stash list
git stash drop stash@{0} -> # Deletes a specific stash

Working with Tags

git tag -> # Lists all tags in the repository
git tag -a v1.0 -m “Version 1.0“ -> # Creates an annotated tag with a message
git push --tags -> # Pushes all tags to the remote repository
git tag -d v1.0 -> # Deletes a tag locally 
git push origin --delete v1.0 -> # Deletes a tag from the remote repository

Cherry Picking

git cherry-pick commit-hash -> # Applies a specific commit from another branch
git cherry-pick --continue -> # Continues the cherry-pick process after resolving conflicts 
git cherry-pick --abort -> # Aborts the cherry-pick process

Working with Remotes

git remote -v -> # Lists all remote repositories and their URLs
git remote add origin https://github/com/user/repo.git -> # Adds a new remote repository
git remote remove origin -> # Removes a remote repository
git fetch origin -> # Fetches all branches and updates from the remote
git pull origin branch-name --rebase -> # Rebases the local branch with the remote branch

Git Aliases

git config --global alias.st status -> # Creates an alias for ‘git status’
git config --global alias.co checkout -> # Creates an alias for ‘git checkout’
git config --global alias.br branch -> # Creates an alias for ‘git branch’

Bisecting to find Bugs

git bisect start -> # Starts the bisect process
git bisect bad -> # Marks the current commit as bad
git bisect good commit-hash -> # Marks a specific commit as good
git bisect reset -> # Ends the bisect session and returns to the original branch

Cleaning Untracked Files

git clean -n -> # Displays the untracked files and directories that would be removed
git clean -f -> # Removes untracked files
git clean -fd -> # Removes untracked files and directories

Interactive Rebase

git rebase -i HEAD~3 -> # Opens an interactive rebase for the last three commits
# Actions during interactive rebase:
# - pick: Keep the commit as is
# - squash: Combine this commit with the previous one
# - reword: Modify the commit message
# - edit: Modify the commit 
git rebase --abort -> # Aborts the rebase process and restores the original branch
git rebase --continue -> # Continues the rebase process after resolving conflicts
0
Subscribe to my newsletter

Read articles from Pranav Bawgikar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pranav Bawgikar
Pranav Bawgikar

Hiya 👋 I'm Pranav. I'm a recent computer science grad who loves punching keys, napping while coding and lifting weights. This space is a collection of my journey of active learning from blogs, books and papers.