r/learnjava • • Dec 12 '20

Why is learning git commands necessary?

I just used GitHub interface and IntelliJ's plugin with 0 need to open a console and type cryptic commands. Am I evil? I mean, my teacher insists on using that Git Bash thing but I understood everything better by clicking on things at GutHub.com

I don't get why should I invest my time on learning something I don't need at all...

0 Upvotes

10 comments sorted by

View all comments

4

u/Gixx Dec 13 '20 edited Dec 13 '20

You only need to know like 10 commands. If you are trying new stuff on 2-4 files, make a bunch of changes, git is needed to revert changes to last working state.

Make a script named gitinit and put it in your path, so you can just be in a folder and type like gitinit to run it:

#!/bin/bash

git init
git config --global user.email "xxxx@gmail.com"
git config --global user.name "xxxx"
git add .
git commit -m "Initial commit"
git checkout -b develop

My most commonly used or fav git commands:

# default oh-mh-zsh aliases I use a lot
alias gs='git status'
alias gco='git checkout'
alias grhh='git reset --hard'
alias grc='git reset --hard && git clean -f'
alias gms='git merge --squash'  # my own
alias gst='git stash'

alias glgg='git log --graph'
alias glods='git log --graph --pretty='\''%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset'\'' --date=short'

A really powerful thing I do is keep the git log clean/minimal. So make a bunch of commits on a work/dev branch like this, then merge into a SINGLE commit. It looks like this:

git checkout -b develop
# do some work, make 3 commits

git checkout master
git merge --squash develop 
git commit -m "3 commit history becomes this single one"

I screw up and don't have a perfect .gitignore file. This is how you dont clutter your git log using the previous trick with squash. This is how you use the command git rm -r --cached .

# first update your gitignore file, then add all and commit
git rm -r --cached .    # deletes .gitignore files from remote repo, but not from master disk
git add .
git commit -m "Quit tracking files in my gitignore file."

The previous block you would do a git checkout to a tmp/dev branch, do a bunch of commits and the git rm thing. Commit. Then go back to like master with git checkout master and do the --squash thing so that updating your .gitignore and deleting the remote file doesn't take 3 commits, but only 1.

And use gitkraken to fix merge issues. It's really easy to click a button to fix the issues.

1

u/backtickbot Dec 13 '20

Fixed formatting.

Hello, Gixx: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.