What help me to save my time, nerves and life - Git
Tags: git, beginners, programming, bootcamp
Published: 2022-03-21 00:46:17

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p16002ro32y1gqr6kari.jpeg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2a9hl7jd6rs0pp7jvmf.jpeg)
 
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 <commit-sha>**_ - 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8b3sbwpfmkv7e2omuax1.jpeg)