A Guide to Essential Git Commands and Workflows for Optimizing Version Control
If you’re working in a collaborative environment, then mastering Git is super important. This guide covers the basics of Git commands, the best ways to work with feature branches, and even some advanced stuff like reverting commits, managing remotes, and resetting commit history. Whether you’re just starting out or want to level up your workflow with git, this guide has you covered.
It’ll help you manage your version control like a pro, keeping your code neat and tidy. You’ll learn how to merge branches, track changes, manage remote repositories, and so much more.
Here are some basic Git commands to help you get started
1- git config — list
The git config — list command used to be super helpful for listing all the configuration settings for Git. It’ll show you both the global and local Git settings, like your username, email address, your preferred editor, and other configurations.
git config --list
Example:
user.name=Hamza Sehouli user.email=ham.sehe@myemail.com branch.master.remote=origin branch.master.merge=refs/heads/master
2- git config user.name “Your Name”.
This will set the user name for your commits, so that they’re linked to the right person.
git config user.name "Your Name"
3- git init
The git init command is like the building block of every Git-based project. It sets up a new Git repository in your project directory, so you can start tracking changes, managing versions, and working with others.
4- git remote add origin
When you’re working with Git, it’s super important to connect your local repository to a remote one. That way, you can work together with other people and keep track of all your changes. The git remote add origin command is what you’ll need to link your local repository to a remote one, and it’s usually hosted on platforms like GitHub, GitLab, or Bitbucket.
git remote add origin https://github.com/username/repository.git
This lets you push and pull changes to and from the remote server, so you can share your work with others.
5- git remote -v
This shows the URL used to retrieve (pull) and push to the remote repository.
git remote -v
Example:
origin https://github.com/username/repo.git (fetch) origin https://github.com/username/repo.git (push)
6- git branch -a
When you’re working with a repository that has multiple branches, you can use this command to list all of them. That way, you can keep track of both the local and remote branches.
git branch -a
Example:
* main remotes/origin/main remotes/origin/feature-1
The ‘*’ indicates the branch that’s in use. The ‘remotes/origin’ prefixes show remote branches that exist in the remote repository.
To display only local branches, just omit the -a flag git branch:
feature * master
7- git add -A
To stage all your changes for the next commit, including all the modified, new, and deleted files, use this shortcut. That way, you can rest assured that everything you’ve changed will be added to the staging area.
git add -A
This is equivalent to using:
git add .
If you want to stage a specific file use :
git add [filename]
For folder:
git add [folderpath]
8- git diff
If you want to see the changes in your tracked working directory compared to the ones modified, you can use Git Diff to check everything out. This handy tool will show you the differences between what you’ve done with Git Add and your working directory.
git diff
Or to compare them with staged changes.
git diff --staged
9- git reset
If you added files to the staging area with git add but then decided you’d rather not include them in your commit, no worries! Just use git reset to unstage them.
git reset
This command will unstage all the files that have been added to the staging area, but don’t worry, it won’t delete any of the changes you’ve made. It’ll just remove them from the “staged” state, so they won’t be included in the next commit.
To reset a specific file or folder use:
Git restore --staged <filename or folderpath>
Or
git reset <filename or folderpath>
10- git commit
This command will commit the changes you’ve made in your staging area to the repository. It takes all the changes you’ve made and creates a commit with a message describing what was changed.
git commit -m "Added EventController"
This creates a commit with the message Added EventController. A descriptive commit message helps other developers understand the purpose of the changes.
11- git pull origin your-main-branch
Get the latest changes from the remote repository to prepare for merging your current branch. Pulling is important to make sure your local repository is up to date with the remote before pushing changes.
git pull origin master
This’ll get and combine changes from your-main-branch of the remote repository.
12- git push
Once you’ve made your local changes and updated your main branch with the remote one, you just need to push them to the remote repository to make them available to other developers.
git push
13- git rebase
The git rebase command is an advanced Git operation that’s here to help you refactor your commit history. Instead of merging branches, git rebase applies changes from one branch to another, which gives you a nice, linear history. This is especially helpful for cleaning up feature branches before merging them into your main branch.
Example:
If you want to rebase feature onto your main branch, first head to you main branch and run:
git rebase feature
This command applies your feature commits on top of the latest commit of your main branch, effectively updating your branch with the latest changes from feature branch.
14- git cherry-pick
Git cherry-pick is a super helpful command that lets you apply a specific commit from one branch to another. It’s great when you want to add a fix or feature from one branch to another without having to merge all the changes. Whether you’re dealing with a quick fix or need to bring some commits over to a release branch, this command makes it a lot easier to pick and apply individual commits:
git cherry-pick <commit-id>
For example :
git cherry-pick 9bb819c59a36d5ac5d8c2da8a5cb8a82f8b55969
15- git merge
The git merge command is one of the most essential tools in Git. It’s like a bridge that connects different branches, allowing developers to bring your changes together into one.
Whether you’re working on a feature branch and need to merge it into your main branch or just want to keep everything in sync, git merge is here to help.
Example:
Merge a feature branch into the main branch:
git checkout main git merge feature
This will merge the changes from the feature branch into the main one. It’s recommended to always pull the latest version of your main branch before merging to avoid conflicts.
Merge with a no-commit option to just merge changes but don’t commit automatically:
git merge --no-commit feature-branch
This command is super helpful! It lets you merge changes without having to commit right away. So, you can take a look at the changes, check them out, and then commit manually whenever you’re ready.
Common Git Workflow
1- Feature Branch Workflow
It’s best practice to never work directly on the master (or main) branch. Instead, you create a feature branch for each task or feature you’re working on.
Create a new branch:
git branch new-feature
Switch to the new branch:
git checkout new-feature
Now, you’re free to work on this feature without affecting the master branch. Once you’re done, you’ll merge it back.
2- Merging Feature Branch to Master
To merge your feature branch back into master, follow these steps:
Switch to master branch:
git checkout master
Pull the latest changes from the master branch to ensure you have the latest version before merging:
git pull origin master
Merge the feature branch into master:
git merge new-feature
Push the changes back to the remote repository:
git push origin master
3- Clean Up the Feature Branch
Once the feature branch has been merged into master, you can delete it since it’s no longer needed:
Delete the local feature branch:
git branch -d new-feature
Delete the remote feature branch:
git push origin -d new-feature
4- Check Merged Branches
To see which branches have been merged into the current branch:
git branch --merged
Advanced Git Operations
1- git log
You can see all the commits from the past, and you can also find out who made them, when they were made, and the commit message.
git log
You can also use flags like — oneline to display a condensed version:
git log --oneline
For Example:
f2e2a4b Added docker and nginx files cd18ba6 Added node_modules to the .gitignore file b6d30b7 Added .dockerignore 9d9ee24 Added Dockerfile 5768cb2 Modified script.js bcff8b8 Added use strict into script.js file
2- git checkout <commit_id>
This feature lets you explore the repository at any point in its history. One thing to keep in mind, though, is that you’ll be in a detached HEAD state. That means you won’t be on any branch, but you’ll still be able to stage it and commit it if you have made any changes!
git checkout <commit_id>
3- git revert <commit_id>
If you need to undo a commit, just create a new commit that reverses the changes. This is a safe way to undo a commit without altering the history of the repository.
git revert <commit_id>
4- git reset — hard <commit_id>
Reset the repository to a specific commit and discard all changes since.
Warning: This is a destructive operation. It will permanently delete all commits after the specific commit and reset your working directory, so use it wisely.
git reset --hard <commit_id>
Remote Operations
1- git remote add origin <URL>
Set a new remote repository URL.
git remote add origin https://github.com/username/your-repo.git
2.- git remote set-url <URL>
If you need to change the remote URL (for example, due to access tokens or repository name changes), use git remote set-url <URL>.
git remote set-url origin https://<ACCESS_TOKEN>@github.com/username/repo.git
File Tracking and Manipulation
1- List tracked folder and files
To list all the files that are being tracked in the current branch,
git ls-tree --full-tree --name-only -r HEAD
2- git filter-branch — tree-filter ‘rm -rf <file>’ HEAD
If you added a large file to the wrong commits, you can remove it. For example, if you added a file (like storage.zip or node_modules) to the repository by accident, you can remove it from all the commits:
git filter-branch --tree-filter 'rm -rf storage.zip' HEAD







