5 Git Tips You Should Try For Better Workflowโ€‚by@ygnys

5 Git Tips You Should Try For Better Workflow

Read on Terminal Reader
Open TLDR
react to story with heart
react to story with light
react to story with boat
react to story with money
Git is loaded with neat tricks that have the power to make your daily coding routine much smoother. In this article, you'll find 5 tips that will help you bring your Git skills to the next level. Use git log --graph --graph to help you visualize your Git history. You can view changes from other branches and even your teammates' uncommitted code, directly in GitLive and, if you need to, cherry-pick them to apply those changes to your local files. Find out who's to blame for a commit, credit a co-author of a commit.
image
Agnieszka Stec HackerNoon profile picture

Agnieszka Stec

@GitLive


No matter how experienced you are, Git will always find a way to surprise you. It is loaded with neat tricks that have the power to make your daily coding routine much smoother.

In this article, you'll find 5 tips that will help you bring your Git skills to the next level.

1. Visualize in the command line


You have cloned a repository, and to understand it better, you'd like to look at the commit history. Wouldn't it be nice to get a visual representation of all that has happened?


  $ git log --all --oneline --graph


If you need to visualize the Git history, git log --graph can be more helpful than a simple git log. By default, git log displays full commit hash, author, date, and message, each on a new line, which sometimes makes it difficult to get a clear understanding of what has happened. There are many ways to customize it, but if you just want to have a compact overview of your commit history, you should try the command above.


--all # will include commits from all branches 
--oneline # will show information on one line
--graph # will draw a text-based graphical representation of the commit history


image

2. Take a peek at a file of another branch


You are working on a branch feature-one and you want to take a quick look at a file that lives in feature-two branch. Wouldnโ€™t it be cool to do it without actually switching branches?


$ git show <branch_name>:<file_name>


With this command, you can view the content of a file from another branch. After running it you will be able to view the content of the chosen file in your terminal.


๐Ÿ’ก You can also view changes from other branches and even your teammates' uncommitted code, directly in GitLive and, if you need to, cherry-pick them to apply those changes to your local files.

3. Find out who's to blame


Some changes were made to the file you've been working on, and you are not entirely sure why they are there. Ideally, you would like to ask for help from the person who made the changes.


$ git blame <FILE_NAME>


You can use this command to see who changed what in a specific file, line by line.

image

Depending on what you want to show, you can pass different flags. In case you want to check the specific part of the file, you would use $ git blame -L <range> which restricts the output to the selected line range.

Other useful flags:


$ git blame -w  # ignores white space modifications
$ git blame -M  # ignores moving and copying text within the same file and shows the original author instead
$ git blame -C  # ignores moving and copying text into other files and shows the original author instead


4. Text search through time

Your code editor most likely allows you to easily search for text in your project, but often it comes in handy to search through previous revisions of the files as well.


$ git grep STRING $(git rev-list --all)


Git Grep is a command that allows you to quickly search through your tracked files for a given string or regex. By default, it will ignore untracked files, but you can add --untracked flag to the command to search through untracked files.

Letโ€™s take a look at some useful flags:


$ git grep STRING # searches through the files in your working directory (current state)
$ git grep STRING $(git rev-list --all) # searches through all the commits (entire history of the project)
$ git grep STRING HEAD~1 # search only in the commit prior to the current head
$ git grep STRING <commit1> <commit2> <commit3> # search only in the specific commits (list as many commits as you need)
$ git grep -ni STRING  # Case insensitive search. Will list the matches along with line numbers
$ git grep -c STRING  # Shows the number of lines that match, instead of listing all the results
$ git grep STRING *.js # search only the files with the specific extension (ignore matches from other files)

5. Teamwork is key: credit a co-author of a commit


The feature youโ€™ve been working on was really tough, and you know you could not have done it without Annaโ€™s help. Her contribution was invaluable, and itโ€™s only fair to give her credit for that.


Co-authored-by NAME <email>

$ git commit -m "Prepare release.
>
>
Co-authored-by: name1 <[email protected]>
Co-authored-by: name2 <[email protected]>"


This easy way of supporting multiple authors was introduced by GitHub in order to improve collaboration within the team and promote social coding. This syntax is supported by GitHub and GitLab, and itโ€™s a great way to credit both developers who worked on a commit.


To add one or more co-authors to the commit, you need to know their name and email (if they decide to keep their email private, you should ask them for their GitHub provided no-reply email address instead, which can be found on the email settings page in GitHub). You add them to the message (each on a new line) before the closing quotation mark.

If you follow this syntax, you will see the co-authored commit on GitHub. It can also be included in the profile contributions graph and the repository's statistics.


Thanks for reading! I hope you enjoyed our selection of 5 Git Tips to Level Up Your Workflow. Feel free to share your feedback at [email protected] Happy Git-ing!


Also published on https://blog.git.live/5-git-tips-to-level-up-your-workflow.

react to story with heart
react to story with light
react to story with boat
react to story with money
L O A D I N G
. . . comments & more!