As projects become more complex and involve a greater number of people, the importance of using version control software during development is increasing exponentially. That is why I have compiled a list of 10 advanced git commands that will enable you to code and collaborate efficiently.
1. Git Auto Complete
Auto complete feature can be enabled for git commands and works in terminal. To do that, edit the bash_profile file:
sudo nano ~/.bash_profile
And add the following lines to it:
if [ -f ~/.git-completion.bash ]; then . ~/.git-completion.bashfi
2. Pull with Rebase Instead of Merge
When team members are working on the same branch (and that can often happen), you would have to pull the code and merge changes almost daily. To avoid merge messages from cluttering up the log, use the rebase:
git pull --rebase
Also, you can configure a certain branch to always rebase:
git config branch.mybranchname.rebase true
3. Track All File Changes
In order to keep things in check and to know who is responsible when something goes wrong, it is useful to see all the changes done to a particular file or files. Fortunately, you can do this easily by using the git blame command, which will show the author of every line in a file, the commits that were included the file and the timestamp of each commit:
git blame my_filename
4. Stash Uncommitted Changes
The stash command saves the day in situations where you have to unexpectedly show your application to the client or someone from the management team. Since you are in the middle of the work, your application would not work without reverting the changes. At that point, you would run the stash command, which will save all your changes for further use:
You will be able to showcase your work without losing any progress. Then, when you want to see the list of stashes, run:
git stash list
To recover the uncommitted changes, execute the following command:
git stash apply
It is also possible to retrieve a particular stash only:
git stash apply stash_unique_id
5. Staging Parts of a Changed File for a Commit
According to best practices when using version control, each commit should represent either a feature or a bug fix. However, you or other team members can sometimes add multiple features or fix multiple bugs without committing. Fortunately, in that situation you can stage files individually and commit them separately:
git commit -p myfilename
Let?s assume that we have made 5 changes to the file. Git will automatically try to ascertain whether the changes are part of the same feature or bug and will group them into smaller parts called hunks. After running the command, you will be prompted for what to do and you will be able to edit the hunks. Each hunk will be committed separately. Once you are happy with how the changes are grouped, you can run the commit.
6. Clone a Specific Remote Branch
In some cases, you would not want to clone the entire remote repository, but only one of its branches. Do that by running the following commands:
git remote add -t Mybranchname -f origin http://www.github.com/myrepository.git
7. Delete a Remote Branch
Deleting a remote branch is a bit more confusing than deleting a local one and is not that straightforward. In newer versions of git, you can use this command:
git push origin --delete Mybranchname
However, in versions before 1.7.0, you have to push “nothing” to the branch in order to delete it:
git push origin :Mybranchname
8. Merge Current Branch with a Commit from the Other Branch
When the team is working in parallel on multiple branches, it can happen that a change in one branch needs to be applied to other branches as well (e.g. a fix for a bug that was present across all branches). Using the cherry-pick command, you can do just that???merge a single commit from the other branch into the current branch, without changing other files or commits. So, first switch to the branch that you want to merge with the commit, and then run the cherry-pick command:
git checkout Mybranchnamegit cherry-pick commit_hash
9. Use .gitignore and .gitkeep
In git, it is possible exclude some files from version control. That is done by creating a file named .gitignore in the root of your project. After that, just add the path to each file in new line.
Also, the git skips empty folders by default. If you want to commit an empty folder, create a .gitkeep file and place it inside of that folder.
10. Change Default Message Editor
In UNIX operating systems, the default editor for commit messages is VI. If you want to change it, run this command:
git config --global core.editor "path/to/my/editor"
These are some of the top git tips and tricks that can help improve team productivity. I would like to hear which git commands are your favorite ones and which ones you use most often.