All I git

Here’s everything I know about git without having to google. It isn’t much.

I type the following to edit the git config:

$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status

Jeez, I had to google that. I couldn’t remember the --global flag. Anyway I was close so I’ll count it for the purposes of this post…

  • git init initializes the git repo locally. Then I have to check github to figure out how to get it up to a remote. I never remember how to type this off the top of my head.
  • git branch gives me a list of the branches that I have locally with a * next to the one I’m on (but my I already know that from the terminal prompt). There are a gajillion options for git branch but I don’t know any of them. I use it so infrequently that I don’t even use the git br abbreviation. Type git branch -help for all the other things you can do.
  • git ci is my commit. I always use the args avm so that I’m prompted to enter a message for the commit.
  • git push pushes all the latest commits to my remote branch, alost always a github repo. Sometimes I have to copy/paste the git push --remove-commmand-args-I-Don't-Remeber that come back in an error message.
  • git co is my checkout. I switch to different branches or if I’m making a new branch I type git co -b new_branch_name
  • git st is my status. Let’s me know what files have been changed, added or delete and not yet commited.
  • git log shows me the results of the last several commits & merges.
  • git merge let’s me merge a branch into the current branch. If I wanted to merge the branch new_feature into the master branch I’d type: git co master to make sure I was on the master branch and then git merge new_branch_name. Things get messy here for me if there are conflicts. I almost never do this but I haven’t forgotten it. I usually use github Pull Requests instead of merger from the CLI.
  • git show sha_arg shows me the result of a single commit that matches the SHA argument. You don’t actully type sha_arg, you type the as many characters of the SHA as you have to type to point to a unique commit, and it’ll show you the details of the commit.
  • git stash This is my life saver. Have you ever been working on some code, get ready to commit and realize you’re on the wrong branch? Usually it’s the master branch. I don’t want to commit it there, I want to commit it to some other branch. Well git stash is your friend. If you haven’t commited the code yet, type
  • git stash and your uncommitted changes will be….stashed… somewhere in the git ether. Then create or move to the feature branch you should have been working on. Once there type git stash pop and the changes will pop out of the git ether and land in your current branch. You still need to commit them.
  • If there are files that you don’t want git to track (files that contain secrets, private keys or config files that contain data you don’t want to kept to the repository), you can create(or edit if it already exists) the file .git_ignore. If you had a .git_ignore file with that contained the lines…
*.json
*key*
howdy*

…then git would not track any file that ended with .json, contained the characters key or any file that started with howdy. You will need to save and commit the changes to the .git_ignore file. It’s a hidden file so you won’t normally see it, but it’s there.

Lastly, and I don’t have this memorized, but I have this gist handy to add the following lines top my .bash_profile.

export PS1='$(git branch &>/dev/null; if [ $? -eq 0 ]; then \
echo "\[\e[0;32m\][GIT: \[\e[0;31m\]$(basename `pwd`); \[\e[0;33m\]$(git branch | grep ^*|sed s/\*\ //) \
$(echo `git status` | grep "nothing to commit" > /dev/null 2>&1; if [ "$?" -eq "0" ]; then \
echo "\[\e[0;32m\]clean"; else \
echo "\[\e[0;31m\]dirty"; fi)\[\e[0;32m\]] \$ "; else \
echo "\[\e[0;31m\][\w]\[\e[m\] \$ "; fi) \[\e[0m\]'

I can’t following everything going on in that bit of bash but I don’t need to and neither do you. Anyway, that makes my command line give me more info if I’m in a directory under git’s purview. Looks something like this:

git CLI

That’s it. That’s all I know in my brain. I don’t know how to re-base branches or cherry pick commits. Those are commands that I hear talked about quite often, but I have never used them without someone else walking me through…every…single…step.