DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Dmytro
Dmytro

Posted on • Originally published at delphic.top

Git tale

Workflow tale

Once upon a time, a dwarf named Krevorook took a quest from a tree elf to build a static website and host it in CDN like Netlify. His journey was full of new discoveries… And befriending a ferocious beast, called GIT, was one of those. Following is a small account of the time they spend together.

After setting up the project, Krevorook created a new git repo.
So he had his MAIN branch, where deployable (production) code lived.

git init
git add .
git commit -m 'init'
Enter fullscreen mode Exit fullscreen mode

After creating a new Github repository, he added a remote.

git remote add origin https://github.com/user/repo.git
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

And git push worked well for him. He made a home page and deployed it.

Sometimes he bringed new files.

git add .
git commit -m 'added: .gitignore'
git push
Enter fullscreen mode Exit fullscreen mode

Sometimes he polished existing ones.

git commit -am 'my message'
Enter fullscreen mode Exit fullscreen mode

When Krevorook had forgotten to add some files or wrote an inaccurate message, he would correct himself.

git add .
git commit --amend -m 'new message'
Enter fullscreen mode Exit fullscreen mode

But more often he just made trivial amendments.

git commit -a --amend --no-edit
Enter fullscreen mode Exit fullscreen mode

He also has to make contact and about pages, which were obviously not production ready just yet.
So he created a new branch DEV where the β€œactively developed” code lived.

git checkout -b dev
git push -u origin dev
Enter fullscreen mode Exit fullscreen mode

He worked in sessions using pomodoro technique, 30 min work / 3 min break. One session usually took 2-3 hours followed by a minimum 1 hour break or change activity (nothing to do with coding or sitting).
At the end of each session Krevorook had the urge to commit, even without a meaningful message.

git commit -am 'up'
Enter fullscreen mode Exit fullscreen mode

It was ok to use dummy commits, because he could always clean up history.

git rebase -i HEAD~3
Enter fullscreen mode Exit fullscreen mode

More often than not he checked out the current state.

git status
git log --graph --decorate --pretty=oneline --abbrev-commit
Enter fullscreen mode Exit fullscreen mode

It was very repetitive. Therefore Krevorook turned to his precious pet, Bash, who was of great help for that sort of thing.

# ~/.bash_aliases
alias gs='git status'
alias gl='git log --graph --decorate --pretty=oneline --abbrev-commit'
alias gc='git commit -m'
alias gca='git commit -am'
Enter fullscreen mode Exit fullscreen mode

At last, pages were production ready and craved to be introduced to WWW.

And Krevorook joined DEV with MAIN.

git checkout main
git merge dev
git push
Enter fullscreen mode Exit fullscreen mode

He has the website live with 3 pages: home, contact and about.
But contact and about pages were fast made, and maybe he should try to make them better.

So he made two new branches, NEW-ABOUT-PAGE and NEW-CONTACT-PAGE, based on DEV, and worked tremendously on them.

git branch new-about-page dev
git branch new-contact-page dev
Enter fullscreen mode Exit fullscreen mode

At some point the new contact page looked good, there was nothing to improve. So he joined the NEW-CONTACT-PAGE branch with DEV as a single commit. Then he pushed it all to production and cleaned up the repo, deleting the NEW-CONTACT-PAGE branch.

# make sure everything is good, some adjustment is needed?
git checkout dev
git merge --squash new-contact-page
git commit -m "merge new-contact-page"

# deploy
git checkout main
git merge dev
git push

# delete
git branch -d new-contact-page
git push origin --delete new-contact-page
Enter fullscreen mode Exit fullscreen mode

And what about the about page?
Contarly to the contact page, the new about page looked not so good at all. So he decided to stick with the current one and moved on.

git branch -D new-about-page
git push origin --delete new-about-page
Enter fullscreen mode Exit fullscreen mode

The end.


Commands used

Create a new local repository

git init
Enter fullscreen mode Exit fullscreen mode

Displays the status of your working directory, (show modified files).

git status
Enter fullscreen mode Exit fullscreen mode

Displays all commits in the current branch’s history

git log
git log --graph --decorate --pretty=oneline --abbrev-commit
Enter fullscreen mode Exit fullscreen mode

Add a file to your next commit (stage)

git add [file]
Enter fullscreen mode Exit fullscreen mode

Add all files to your next commit

git add .
Enter fullscreen mode Exit fullscreen mode

Replaces that last commit with your new, improved commit.

git add .
git commit --amend -m 'new message'
Enter fullscreen mode Exit fullscreen mode

Trivial amendments

git commit -a --amend --no-edit
Enter fullscreen mode Exit fullscreen mode

Add, remove, or combine Git commits

git rebase -i HEAD~[num of commits]
Enter fullscreen mode Exit fullscreen mode

Create a new branch (dev) and switch to it

git checkout -b dev
Enter fullscreen mode Exit fullscreen mode

Switch to another branch (main) and check it out into your working directory

git checkout main
Enter fullscreen mode Exit fullscreen mode

Create a new branch (dev) in a remote repository

git push -u origin dev
Enter fullscreen mode Exit fullscreen mode

Create a new branch (feature) based on some existing one (dev)

git branch feature dev
Enter fullscreen mode Exit fullscreen mode

Merge (join) a specified branch (feature) into your current branch (dev)

git checkout dev
git merge feature
Enter fullscreen mode Exit fullscreen mode

Merge a specified branch (feature) into your current branch (dev)
as a single commit

git checkout dev
git merge --squash feature
git commit -m "merge feature"
Enter fullscreen mode Exit fullscreen mode

Delete a local branch (feature)

# safe delete
git branch -d feature

# delete without questions
git branch -D [failed-feature-branch]
Enter fullscreen mode Exit fullscreen mode

Delete a remote branch (feature)

git push origin --delete feature
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
adriano_moreira profile image
Adriano Moreira

nice,

I abandoned command git checkout ..., to new command git switch ... to change current branch and create new one

🌚 Browsing with dark mode makes you a better developer by a factor of exactly 40.

It's a scientific fact.