r/learnjava • u/[deleted] • 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...
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 "[email protected]"
git config --global user.name "xxxx"
git add .
git commit -m "Initial commit"
git checkout -b developMy 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
2
u/desrtfx Dec 12 '20
Sometimes you can get in situations where you have neither GUI nor web browser. All you will have is a shell (bash for example) and a text editor.
Then, the commands will come in handy.
Overall, they are very easy to remember and use.
1
u/DigitalTorture Dec 12 '20
Yep. Clicking buttons works fine for some, but it sure comes in handy when you know how to do it manually.
2
u/knoam Dec 12 '20
The command line is the only standard interface for git. So as soon as you venture outside the abilities of github, and you have to search around, all the answers online will assume you're using the CLI.
There's a lot of advanced features to git that won't always be supported by GUIs. Bisect, archive, submodules, worktrees, subtrees, bundles, rerere, patches. Not always necessary but some of them are pretty awesome.
2
u/knoam Dec 12 '20
Bash is really valuable for things way beyond git. Learning the command line and something like shell scripting is pretty much a necessity for any programmer. A language like Python or Ruby could substitute for Bash, but Java not so much, despite the existence of jshell.
2
2
u/Farpafraf Dec 12 '20
You don't always have a GUI available: for instance you could be cloning your app repo on a remote machine and make it run there
Not all options are available with the GUI
You need to write a script using git for any reason
I suck at git too btw, hopefully I'll get better with time
4
u/smash_that_code Dec 12 '20
Don't be fooled by comments that git is easy.
It is distributed version control system unlike something centralized like SVN.
Some things are better with GUI like merge, some features may not have GUI like reflog interaction.
Some features just can be a bit easier with console. Like starting interactive rebasing to drop or edit some old commit.
It is totally fine to work with GUI for git if it helps to get things done.