DEV Community

Cover image for What help me to save my time, nerves and life - Git
Olha Gruzglina
Olha Gruzglina

Posted on • Updated on

What help me to save my time, nerves and life - Git

There is a magic pill that can help you and everyone else who writes code, it is Git! I recommend you learn it, and start using it as early as possible in your career! It should be your habit, your essentials.

Image description

To be honest, I cheated during my first two projects, I did not use Git the right way. I used it only because I needed to save and share all that, ahem-ahem, "pretty" code. But still, we tried to work with branches and commits, so at least we did not cram the entire project into one huuuge commit 😂. The first time we touched the same file to avoid having to deal with conflicts, my project partner sent me code via slack. However at the end of our project we tried the whole flow and it was not as painful as I imagined 😆.

Image description

I do not know how that happened, maybe because I was too scared to use this tool without any knowledge and experience, or because of the lack of time... However, I think you would have also preferred to spend 4 days on the project instead of learning Git and failing a phase...

During phase 3 project, since my partner was familiar with Git and worked according to its rules, I did not want to be a looser 😉. I was brave enough to solve conflicts on my side before pushing my code o_O. I had great experience, learned and tried a lot of commands, however I am not fully sure the commands I used were the best or correct ones, but it worked for me 🤷‍♀️.
Psst, we did 101 branches and 210 commits 😮😆.

The following commands I definitely found useful:

  • git status - check what branch I am on now, check if my tree is clear;

  • git checkout -b branch_name - create a new branch to work with every new feature on your project;
    P.S. it is a good habit to name your branch this way - your_name/branch_name (ogruzglina/get_all_reviews_back_end), because when you have a lot of programmers working on the same project it is easier to find your brunch/commit.

  • git checkout branch_name (or git switch branch_name - new syntax (as of Git 2.23) or 'git switch -' if you want to go back to the branch you were on) - change the current branch to the name_branch;

  • git log or git log --graph (if you want to see visual graph of branches);

  • git diff branch1..branch2 - see difference between two branches;

  • git rebase master or main - if you're working on your branch and someone made changes to the master/main and you want to have these changes on your branch, then: checkout or switch to master/main do git pull, checkout/switch to your branch and execute git rebase command. You may have to resolve conflicts. However, it is better to do this before your commit on this branch, otherwise you might have fun time solving problems with divergent branches. In that case just do push and resolve conflicts during merge if you will have any;

  • git reset --hard HEAD~n or git reset --hard - revert n last commits or a specific one;

  • git revert HEAD - rollback the last commit in Git;

  • git stash - to discard all local changes, but save them for later;

  • git branch -a - to see all local and remote branches, with -r flag you will see only remote branches, without any flag - you will see only local branches;

  • git branch -d branch_name - delete local branch;

  • git push origin --delete branch_name - to delete remote branch.

Do not afraid of Git, just learn it, just do it, it will help you!
Oh, I almost forgot, you should read the terminal or console (wherever you run git commands) because you can see lots of hints there.
Image description

Top comments (0)