I wish I'd known these git commands earlier


[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. Read in-depth about version control systems here.
Commit
It’s important to understand what a commit is before you start reading about git commands.
In Git, a commit is a snapshot of changes to a codebase that has been made by a developer. When a developer wants to save changes made to the codebase, they create a commit in Git. This commit contains a record of the changes made to the code since the last commit, including modifications to files, additions of new files, and deletions of existing files. Commits are a fundamental concept in Git, and they allow developers to keep track of changes to a codebase over time.
Git Commands
Git Checkout
In Git, ‘checkout’ is a command that allows developers to switch between different branches or versions of a codebase. When a developer checks out a branch, they are changing the codebase to match the version of the code on that branch.
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 Push & Pull
Push and pull are two fundamental actions in Git that allow developers to share and synchronize changes between a local repository and a remote repository.
Git Push
The push action in Git is used to send the changes made in a local repository to a remote repository. When a developer pushes their changes, Git sends the new commits, branches, and tags to the remote repository. This makes the changes available for other developers to see and work with.
To push changes to a remote repository, a developer first needs to make sure that they have the latest changes from the remote repository by pulling the changes. Then, they can commit their changes locally and use the "git push" command to send the changes to the remote repository.
Git Pull
When you are finished making changes to someone else’s code, you can share them with the original owner via a ‘pull request.’ Before you ‘push‘ any code (or send your code into your remote repository), it’s good to ensure your branch is up-to-date with the latest code from the main branch. There are two options here: fetch
and pull
.
The pull action in Git is used to retrieve the changes made in a remote repository and merge them into the local repository. When a developer pulls changes from a remote repository, Git retrieves the new commits, branches, and tags and applies them to the local repository. To pull changes from a remote repository, a developer can use the git pull
command. Git will automatically fetch the changes from the remote repository and merge them into the local repository.
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
Git Log
Git log shows the commit history of the current branch.
Git Restore
If you have made changes to your files, but want to return to a prior commit, you can use the following command.
git restore
To restore and revert back to the original changes (i.e., your current edits will be removed) from the most recent commit you are working on use the following commands.
git restore file-name
To restore but keep your current edits in your working directory i.e., unstaging your changes use the following command.
git restore -staged file-name
Stashing Changes
Git stash is a way to save your changes temporarily. This is useful when you want to switch branches but don’t want to commit your changes. When running git stash
, your changes (staged and unstaged) will be saved and your working directory will be cleaned. Stash is like a temporary local storage.
To stash your changes with a message.
git stash save "my stash message"
To list your stashes run the following command, this will display a list of your stashes in the format stash@{n}
, where n
is the stash index. The most recent stash has index 0.
git stash list -> # Lists all stashed changes
To delete a specific stash.
git stash drop stash@{1}
To clear all stashes.
git stash clear
Other commands.
git stash -> # Temporarily saves your uncommitted 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
Cherry picking is done when you pick a commit from one branch and apply it to another branch. You can also pick multiple commits. This is useful in scenarios where you need to apply specific changes or fixes from one branch to another.
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
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.