31

Become a Git Ninja: 7 Productivity Tips

 4 years ago
source link: https://www.tuicool.com/articles/ABZBNv7
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Improve your Git skills and save time with this list of tips and tricks

1*UOq6YofUBJDaO7bJIptNAA.png?q=20

Image by mohamed Hassan from Pixabay

Over the past decade, there has been an increase in the popularity of version control systems and Git, in particular. A large number of organizations over the world have reaped the benefits of adopting version control in their projects. While Git is mostly used as yet another productivity tool, developers are often unaware of its full potential. This post lists down useful Git practices that one may follow to increase their productivity.

This article assumes that you have a basic understanding of Git. In case you are new to Git, you may consider reading our article onGit for Beginners. This post also assumes that you are using the Git command-line interface and not a GUI client.

Let us get started on the top 7 Git tips to boost your productivity.

Tip: Optimize teamwork by using the right tools for code-sharing

Use Bit to share, install and collaborate on individual JS modules and UI components. Stop wasting time configuring packages, managing multiple repositories or maintaining cumbersome monorepos.

3Unimqm.jpg

Components with Bit : Easily share across projects as a team

Autocomplete Git Commands on the Terminal

Most terminal commands for Git start with the git keyword, followed by a space and a second keyword. As there is a space in between these commands, they are treated as different words by the terminal and hence autocompletion through the Tab key does not work by default.

Git provides an autocompletion script that you need to download in order to use this feature. Run the following command to download the script in your home directory.

cd ~
curl https://github.com/git/git/blob/master/contrib/completion/git-completion.bash

Next, add the following lines to the .bash_profile file, located in your home directory.

if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi

This snippet essentially searches for the autocompletion script in your home directory and if present runs it every time you log into bash.

Locally Remove Branch on a Remote

While it may be a tedious task to work with branches on a remote, there is a quick way of removing a branch on the remote from the local system using the push command. It is assumed that you have write access to the remote.

Run the following command to remove the branch_name branch from the remote_name remote. You must proceed with caution as this step is irreversible.

git push remote_name :branch_name

By running the command, you basically push an empty branch to the branch_name , thereby removing branch_name from remote_name altogether.

A Force Push without Risks

If you have rebased commits in your local branch or changed the history in some other way, your push to the remote would be rejected. The workaround here is to simply use a --force option that rewrites the history of the remote’s branch. However, this is a dangerous process because it would overwrite any commits that someone else may have pushed to the remote since your last pull.

A safer way of a force push is to use the --force-with-lease option. It works the same way if no one else has sent any updates since you last pulled from the remote. However, it would reject the push in all other cases. A rejected push would give you an indication that you need to update your local branch with the updated remote before trying to send these changes again.

Undo Changes in Git

While Git is useful in most cases, you may need to make changes to certain Git operations occasionally. In this section, we discuss undoing three types of changes — tracking a file, staging changes and creating a commit.

To undo the process of tracking a file with Git, run the following command:

git rm --cached added_file

To undo staged changes to a file since the last commit, run the following.

git reset HEAD staged_file

This command restores the status of the file staged_file to the HEAD pointer, which points to the latest commit. However, if you have committed the changes too, there is another way to getting back to the stage before the commit.

git reset --soft HEAD~N

This commands undos the status of the repository to N commits before the HEAD . All changes would still be staged, so this command does not result into any changes in your files. When you are resetting a commit, you will have to revert back the stage of the whole repository and can not do so for a single file.

Save Uncommitted Changes with Stashes

Imagine you are working in a new feature when your boss walks by and asks about the progress of the previous task. Your current feature is not complete, so creating a commit does not make logical sense. What should you do?

Git stashes come to the rescue. The following command saves all uncommitted changes and reverts back to the state of the repository at the last commit.

git stash

When you would like to resume working on the feature again, use the following command to check all stashes.

git stash list

It shows the list of all saved stashes with timestamps. You can apply the Nth stash from the stash list by using the following command.

git stash apply stash@{N}

Selectively Commit Changes from a File

What if you made multiple changes to the same file, but would like these changes to appear in different commits? While a traditional git add command would stage all changes to the file, you can add the -p option to start a wizard that would allow you to selectively stage changes in the file.

git add -p

Handling large files with Git LFS

While Git can manage text-based files well, it fails to efficiently handle binary files. Excel documents, Photoshop designs and other executable files are all examples of such binary files.

Git LFS comes to the rescue! It is an open source extension to Git that manages large binary files by placing text pointers within Git, while storing the file contents on a remote server. This keeps your repository management process efficient, while a remote server handles the large binary files when changes are made to it.

Once downloaded and installed in your local system, initialize Git LFS for each repository with the following command.

git lfs install

To track a single type of file extension (say PSD), run the following command:

git lfs track "*.psd"

Additionally, you can track other files by editing the .gitattributes file.

Bonus Tip: Debugging with Git

Since you stayed till the end of the article, here’s a bonus tip!

Did you know that Git is a very capable tool when it comes to debugging issues in your codebase? Git Blame helps you extensively analyze the history of a file, whereas the Bisect process initiates a binary search through your commits. Head over to this tutorial to know about these two popular Git techniques to help in your debugging process .

Final Thoughts

With this, we come to the end of this list. In this tutorial, we covered seven tips (and a bonus tip!) to increase efficiency in your workflow. Let’s hope that you are able to integrate these tips into your daily routine and benefit from their use.

On a side note, were we successful in creating a Git Ninja out of you?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK