Learning To Use Git


Setting Up Aliases

A great place to start concatenating git commands is in your shell's rc file. I currently use Oh-My-Zsh, in which case I'd vim into it and add aliases like so.

vim ~/.zshrc

You can add in extra aliases to fit your needs, but below is just a list of the main git commands one would use most of the time.

# Insert these main aliases
alias gaa = "git add ."
alias gstat = "git status"
alias gdiff = "git diff"
alias gcm = "git commit -m"
alias gpush = "git push"
alias gco = "git checkout -b"
alias gpull = "git pull"

Before we can use them we need to source (reload) our new config changes.

source ~/.zshrc

Now to use them in the command line, just type the alias.

gaa    # => This adds all modified files

Add/Remove Remote Upstream

If you need to remove one you'v added by mistake, check the list of remote branches and then remove the one you named previously.

git remote -v
git remote rm upstream

Add your new remote stream and then fetch the working branch. A good useful case for this is when you fork a repo and need to maintain an updated version of the remote master branch.

git remote add upstream git://github.com/USER/REPO
git fetch upstream
git pull upstream master

Reset to previous commit

git reset --hard commit_hash

Squashing Commits

Doing a soft git reset with a head of ~4, will take the latest 4 commits and delete the commit history forcing you to re-add everything that was changed into one new commit message.

git reset --soft HEAD~4
git commit -m "Squashed commit comment"

Rebasing

If you want to rebase and put your commits ahead of the current head in the project, you can do so based off any branch you want to rebase to.

For example, let's rebase off master because there have been a million updates and we are not updated on our current branch. During rebasing, you can also choose to squash, pick, edit, and delete commits. Follow the prompt that shows up when you are doing this to guide you.

git rebase -i origin/master

Or you can do it off a commit hash and then choose which commits above that one you want to squash. Use git log to find which hash you want to be the base.

git log
git rebase -i commit_hash

Visual Graph

If you want a terminal graphical interface that is built into Git, you can use the following command. You can also emit the --graph if you don't want to visualize it. Hit q to leave the view.

git log --graph

General Setup

Git global setup

git config --global user.name "Your Name"
git config --global user.email "youremail@gmail.com"

Create a new repository

git clone git@url.com:user/app.git
cd app
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

Existing folder

cd existing_folder
git init
git remote add origin git@url.com:user/app.git
git add .
git commit -m "Initial commit"
git push -u origin master

Existing Git repository

cd existing_repo
git remote rename origin old-origin
git remote add origin git@url.com:user/app.git
git push -u origin --all
git push -u origin --tags