As the projects are getting more and more complex, the number of the developers needed to finish a project is also growing faster depending upon the complexity.In this era of complex projects we need a powerful version control software to manage and track the changes in the project. As you all know, Git is one of the most popular choice of version control tool among developers. I have also created a course on Git version control, subscribe below for 90% discount on this Git course.
In this blog post, I will write down 10 advance Git commands that will make your work easier and help you to collaborate effectively and efficiently.
Table of Contents
Toggle1)Rebase vs Merge:
When teammates are working on the same branch, and everyone is making changes. Its better to pull the code changes daily, so you can avoid merge conflicts later. Its better to use rebase instead of merge to avoid merge message in your commit history.
git pull –rebase
You can also configure a branch to always rebase:
git config branch.mybranchname.rebase true
2) Track All File Changes
If something went wrong in the project and you want to track down who made that change, git blame command comes to your rescue. With the help of this command, you can see the author of
each line in that file, the commits related to the file and as well as the timestamp of every commit. Blame command comes handy when you want to track down all the changes done to a particular file.
git blame <file_name>
3) Stashing Uncommitted changes
Let’s say you are in middle of working on the changes for the next release, and unexpectedly client or someone in the project management team wants to see your application. Since you are mid of making changes, and you know your application won’t run without reverting those changes, but you also want to save the new changes somewhere safe so you can get back to them.At this time stashing comes down to save you.
git Stash command will save your uncommitted changes at some safe location and when you are ready to go back you can run,
git stash apply: this will recover all the uncommitted changes.
git stash list: to see the list of stashes.
git stash apply <stash_unique_id> : to retrieve a particular stash
4) Staging parts of File for a commit:
According to Git best practices,each commit should represent either a bug fix or a new feature. It’s a bad practice to do single commit for multiple features or multiple bug-fixes.Let’s say someone in our team added multiple features or fixes without committing. Git understands we all are humans and we all do mistakes. so don’t worry Git has the ability to stage files individually and
commit them separately.
git commit -p filename
Let’s assume, we made bunch of changes to the file to fix multiple issue. With this command, Git will try to make sure whether the changes are part of the same bug and if they are part of the same fix will bundle them into smaller pieces called hunks.
After you execute this command, GIt will prompt for what to do next and you will be able to edit these hunks. Each hunk will be committed separately. Once you are happy with how these changes are grouped, you can commit them.
5) Clone only a remote branch:
Sometimes you do not want to clone the entire repository but you only want to clone a particular branch.
You can do this by the following command:
git remote add -t <branchname> -f origin <url for remote repository>
git remote add -t branchname -f origin http://www.github.com/myrepository.git
6) Delete a remote branch:
Deleting a local branch is simple but when it comes to deleting a remote branch, it might get confusing.
To delete a local branch :
git branch -d <local_branch_name>
But when you want to delete a remote branch run the below command <only for newer versions of git>
git push origin –delete <remote_branch_name>
But if you are using older version of Git <versions before 1.7.0> you need to run the following command:
git push origin :Mybranchname
7) Using .gitignore and .gitkeep
Git doesn’t automatically track every file in our project because we do not record generated files like C binaries or compiled Python modules or eclipse generated files. But seeing these files under untracked files list when we run git status command, might get confusing for some project. So git has a way to ignore these files.
Create a file called .gitignore in the root directory of your project and just add the path to each file that you want Git to ignore in .gitignore file. Important point to note here is: Git ignore empty folders by default.But if you want to commit an empty folder, you need to create .gitkeep file and place that file inside of the empty folder.
8) To change Default Message editor:
In UNIX operating systems, VI is the default editor for commit messages. If you want to switch to a different one. Use the following command:
git config –global core.editor “path/to/my/editor”
9) Merge the commit from another branch to the current branch:
When teammates are working in parallel or in multiple branches,it’s very likely to happen that commit made in another branch, you want to merge to your branch ( for eg: a bug that was present across all branches),with the help of cherry-pick command, you can just do that- you can merge a single commit made in differeent branch into your current branch without changing any other files or commits.
Steps to do it:
Step 1: Switch to the branch that you want to merge into.
git checkout <branch name>
Step 2: Run the following command:
git cherry-pick <commit_id>
10) Auto Complete
Git auto complete feature works in terminal and it can be enabled for git commands.
Edit the bash_profile file:
sudo nano ~/.bash_profile
Add the following lines:
if [ -f ~/.git-completion.bash ];
then . ~/.git-completion.bash
fi
Let me know which commands you use at your work place. I will be looking forward to your replies.