Skip to main content

Command Palette

Search for a command to run...

How NOT to use Git in Practice. Ten Git usages, you should know to avoid.

Git is a popular version control system. While you must know how to use it, it is equally important to know how NOT to use it! Learn the DON'Ts.

Published
β€’6 min read
How NOT to use Git in Practice. Ten Git usages, you should know to avoid.
T

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human

Hello friends πŸ‘‹. I hope you all are doing great and had a productive week learning something new. Today, we will discuss Git and a few usages, practices that you should avoid making your development experience a better one.

What is Git, and How does it work?

Git is an open-source version control system(VCS) that helps all project contributors to keep their project artifacts(folder, files, source code, and many more) in sync. It provides a workflow that is easy to understand, learn, and use in practice. If you are a beginner to Git or have any difficulties with its usages, here is an in-depth, hands-on video tutorial for you. Please have a look.

Suppose you like the content, please feel free to subscribe to the channel. Thanks a lot πŸ™. Let us now see ten usages & practices that you should avoid.

10 Git Usages You Should Know to Avoid

Git is a highly flexible tool. When you know to handle Git Commands, you feel superior about managing things. At the same time, if you are unsure about specific usages or mismanaging them, you may end up losing changes, making yourself & the team frustrated. Here are ten usages we should stop or avoid.

The master is special

1.png

Yes, it would not be the best to commit to the master branch and other main branches(like the release, dev) directly. In Git, a master branch must contain the most stable version of your source code and other project artifacts. It is most likely that you will build your end product from the master or a release branch. Committing and pushing to these branches directly have a higher risk of corrupting changes, introducing bugs, hours of extra work.

In many cases, you will not have access to commit to master directly. Your source code repository administrator would have set the branch protection rules. If you have access to do some, please don't do it or be sure of what you are doing.

"With great power comes great responsibility"β€” the Peter Parker principle.

So better to create a feature/fix branch and then Change => Stage => Commit => Push to it than pushing directly to master/main/release.

πŸ’‘ It's better to practice it even when you are alone in the repoβ€”do a role play.

Keep application secrets a secret

2.png

When working on a project(including side hustles), you may have a few details that you want to keep private. It could be the API Keys, Secret Keys, Database secrets, Auth secrets, and many more. Making these publicly available may increase security risks to your application. At the same time, it may make a big hole in your pocket when someone misuses.

Do not commit your project secrets with any accidental pushes to the remote repository.

πŸ’‘ Create a .gitignore file at the root of the project folder and mention file patterns(like .env*) to ignore from staging.

Git is not your file storage

3.png

Do you want to store movies? An extensive demo video? Git is not the place for it. Other media services work better for these use-cases. It is better not to store large files and binaries in Git repositories. You know, they call it source code repository!

Technically, you can store anything in a Git repository as long as it doesn't bother and cost someone. The cost is not always the money. It is about time too. It would be a massive waste of time for someone to clone a repository because we have stored a few gigabytes of video in it.

πŸ’‘ You can configure the type and size with GitHub, BitBucket, etc., to limit and keep it in control.

Together we strong, but not always

4.png

It is usual to handle multiple features/fixes/issues in parallel. But do not handle multiple issues in a single branch(even when you are super confident about it!). It may cause a nightmare when you(or your lead) decide to drop one or more issues from the planned fixes. Now you have to pick the unwanted fixes, code changes and remove them. Who wants to do any extra work? So, let's avoid it.

πŸ’‘ A better pattern to follow is, 1 issue => 1 branch => 1 PR and repeat.

Do not force

5.png

Here is a situation,

  • You and your friend are working in the same branch.
  • She made a commit and pushed her changes to the remote.
  • You are also done with your changes now but you have made a force push to the remote branch.
  • In the blink of an eye, all the changes done by your friend are just gone!

Don't perform a force push unless you need it. Your co-workers may go insane because of this.

πŸ’‘ Always pull before the push.

History is important

6.png

When you commit a change to a branch, git writes the commit history and preserves it. Suppose you wonder why commit messages are important because those messages become a part of history. You can understand why someone has done specific changes to a file and who has done them.

Modifying or deleting history will take away the opportunity for us to debug and find the error points. So, please stay away from doing it.

Don't ignore to ignore

7.png

The .gitignore is a particular file where you can mention file name patterns to ignore specific files from the staging. Thus there is no chance of any accidental commit and push of the ignored files. It is a savior. If you are a web developer, you may not want to push the node_modules folder to your remote repository.

Please create a .gitignore file with file name patterns and keep it at the root of the project folder.

Resetting is probably the last solution

8.png

Do you reset often? You may lose your changes if you reset a branch without stashing or committing your changes. When you are in trouble with your files in Git, reset may not be the only solution.

πŸ’‘ Ask yourself, do you need to reset a branch?

Don't make it bulky

9.png

Do not park many commits for an end-of-the-day push. Many small logical commits are much better than a larger ones. As we know, for each commit we make, git writes it to the history with a commit id and user-provided message. Hence it is a good practice to break your commits into small logical chunks.

πŸ’‘ Commit often, push logically. It reduces your chance of facing deadly merge conflicts.

The past is past

10.png

It is not a good practice to amend a commit in the remote directly. Don't do it if you are not sure why and what you are doing.

πŸ’‘ If a StackOverflow solution is amending commits in the remote, look for another solution!!!

That's all for now, friends. I have shared this content originally as a Twitter thread recently. You may be interested in reading and following the discussions there as well.


I hope you enjoyed this article or found it helpful. Let's connect. Please find me on Twitter(@tapasadhikary), sharing thoughts, tips, and code practices. Please give a follow. You can hit the Subscribe button at the top of the page to get an email notification on my latest posts.

You may also like,

A
Adtc4y ago

A lot of this is subjective, depending on the organization and the project. These things are not set in stone and are not some holy rules that send you to Git Hell if broken. They're just guidelines for a sensible usage of Git in general but could be "not followed" in certain situations.

Also, as a fairly recent article it sounds outdated that you still call the main branch master when there's an active drive going on to rename it to main. :)

T

Great article. Thanks for sharing!!

2
T

Thanks, Tracy! Glad you found it helpful.

1
A

Great article, well explained. Tapas Adhikary,

3
T

Thanks, Ayushi.

M

Nice article, I would like to disagree with the "special master" point. I've been using trunk based development for several years and it is great:

https://trunkbaseddevelopment.com/

Keep the good work! :)

2
T

Thanks Marcos πŸ™‚.

Just curious, the trunk based approach you are using with git or with other VCS like SVN?

1
M

Hi Tapas Adhikary, yes it is git, with a small team of 5 devs. Being doing it for 7 years now.

We don't do formal reviews or PR's, all the code is produced on pairs or overviewed by a senior dev at coding time and commited to trunk.

We are full CI/CD using jenkins, and the simplification of the VCS model (git in our case), with low branching or other tasks, leads to more time coding.

Have yet to test the model at a scale, but it works nicely here.

1
T

Marcos Brigante Thanks for sharing, Marcos. With my team of 23, restricting master is the first thing we do πŸ™‚. Great to know about your environment and usecase.

N

Great list, Tapas Adhikary. Thanks.

Would like to add a couple -

  1. Always squash your commits w.r.t. Don't make it bulky section.
  2. Do git pull --rebase when appropriate.
4
T

Thank you, Niranjan.

These two additions sound great. Thanks a lot for sharing πŸ‘.

1
P
PFRouleau4y ago

I tell to my teammate to always use "git pull --rebase" and to define a "pr" alias to make it short.

It should not be required if they follow the rule to never commit directly to a main branch, but there is always someone that does it by mistake and this prevents the creation of an unwanted merge.

1

Git GitHub Open Source

Part 9 of 9

"Open Source" is a magic pot. After spending a couple of years contributing to and maintaining several OSS projects, I am in a position to spread awareness so that more developers participate.

Start from the beginning

10 Git stash commands every developer should know

Git Stash is an extremely useful tool for developers. Learn the usages of the git stash commands to become a productive developer.