Git

ALIAS GIT COMMAND

$ git config --global alias.b 'branch'

Updated Post.

git reflog

shows a history of all actions taken locally

git reflog show [branch_name]

shows a history of all actions taken within a branch

git reset –hard 09682a

restore branch to a previous commit

git stash -u

Stashes all changes, including untracked files

git stash pop

Restores files fom your stash

git stash drop

Removes the stash. Run this if you don’t care about the files you stashed away.

git log

Shows a summary of all commits

git log –oneline

Shows a summary of all commits, one line

git log –oneline –decorate

Shows a summary of all commits, one line with decoration

git sla

This is an aliased command of git log –oneline –decorate –graph –all

git sla -30

Limits results to 30 rows

git sl

This is an aliased command of git log –oneline –decorate created like git config –global alias.sl “log –oneline –decorate”

git pdl

Stands for pretty, detailed log. It’s an aliased command of git config –global alias.pdl “log –pretty=format:’%C(yellow)%h%C(reset) - %an [%C(green)%ar%C(reset)] %s’”. It shows relative dates and who committed what.

git glog ‘der(p|t)’

glog stands for grep the log. This is an aliased command of git config –global alias.glog 'log -E -i –grep’. Basically it searches the history for whatever we specify in quotes. We can also use regular expressions.

git log -S with_active_subscription

this searches within the codebase and will only show commits where the number of times with_active_subscription is changed.

git log –oneline – Gemfile

only shows commits that happen to the Gemfile. Amazing.

git blame –oneline – Gemfile

shows who made a change to a specified file and which commit it was associated with.

git show 93b13d8f

shows all the change information for a commit.

git car

car stands for commit, ammend, reuse. Lets say you commit something, but you forgot a file. Simply git add saidFile to prepare it for commit, then run git commit –amend –no-edit to update the previous commit to contain the file you’ve added.

this is an aliased command of git config –global alias.car 'commit –amend –no-edit’

git unstage [filename]

this unstages a file. This is an aliased command of git reset [filename]. Helpful if you acidentally git add a that you do not want to commit.

git uncommit

This uncommits your last commit that brings you just before you made the commit. Very safe, keeps all the files as they were just before the commit, can be utilized even if you’ve made changes since the commit or changes to the files themselves. This is an aliased commands of git config –global alias.uncommit 'reset –soft HEAD^’

:cquit

If you’re doing a rebase, and you realize, shit this is not what I want to do. Use :cquit to exit vim with a non 0 exit code. That way, your operation will be canceled.

git add [filename/path] –patch

Allows you to commit part of a file. Stage only parts of the file for committing. https://thoughtbot.com/upcase/videos/git-crafting-history

git diff

See unstaged changes.

git diff origin/master..master

get the difference from origin/master UP TO master. That’s how to read it. Gets the difference between two branches… shows you all the things that changed on your master branch compared back to your origin/master branch.

git diff –cached

See staged changes.

git cherry-pick origin/master..master

ALWAYS USE THIS COMMAND FROM THE CONTEXT OF A NEW BRANCH. This command is used if you accidentally committed to master BY MISTAKE, but it requires some initial steps.

  1. First you need to checkout AND BE ON a new branch
  2. Run the Command git cherry-pick origin/master..master (this creates new commit objects on the new branch, the original commits still exist, you can also modify the commit hashes to something other than master and origin master if needed.)
  3. Clean up the original commits git checkout master, git reset –hard origin/master

The last command makes it look like the commits never existed.

git rebase -i [most previous commit you wnat to preserve]

A VERY POWERFUL COMMAND. It is very common to squash 3 commits into one, but can also be used to remove commits. So if I’ve made 3 commits after master, I’d simply git rebase -i master. Then I’d be presented with a screen that looks like this…

pick be891be WIP add forum answer to FAQ
s c0a3941 Remove line in README
s 009ac91 Add other file

The lower commits get squashed into the top commit. Then after than, simply write and quit the file. It will ask you to update the commit message. Typically delete all the old commit messages and write your one commit message.

Original Post

Yes, git, you amazing SOB! I fucking hate you some days.

I remember my first time stumbling upon git in late 2013. I had such a difficult… actually nearly impossible time… getting my public key into github so that I could do git commands and see my code on github. I actually thought that git was written by github, LOL. But I’ve since learned some of the errors of my ways. At the time, I didn’t understand anything about git. And now I’ve got the basics down, git add, push, pull, clone, stash. But now I just want to make sure I start utilizing other important commands which I’ll list below.

I’m just gonna bitch about a few things. Fucking branches, merges, if I fucking create a branch, merge that shit into github master branch, then fucking when I go back to my master branch it fucking tells me I need to git pull. Well FUCK YOU git and github. Fuck man, fuck. It’s really annoying as fuck because I just want to code, but I’m getting pissed off cuz I’m trying to learn a new, “better” workflow. But damn, it takes more fucking time. I’d rather just commit to fucking master.

On a branch and changes have happened on master…

The command that most recently worked was git merge master

Creating a new Feature

Fork the repository

The following is covered in https://upcase.com/videos/git-workflow Create a branch git checkout -b [initials]-new-feature example: git checkout -b do-migration-fix

Make changes and commit them

Pushing the branch to the origin repository git push -u origin do-migration-fix

Creating the pull request Navigate to Github on the master repository, click pull-requests tab, new pull request, select your fork and branch and create

Oh shit, what if someone made a change to master while I was working? git fetch to grab the latest from origin master…

If everything was up to date, you’re good to go. But if there are commits on master, you’ll need to rebase, which will fast forward your changes as if they were created after the master branch’s commits…

https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase

Rebase git rebase -i origin/master When I rebase the first time, I’ll definitely want to watch https://upcase.com/videos/git-workflow at 12:00.

Try to rebase prior to creating your PR.

Afer Rebase, force push From within your feature-branch, git push -f to allow github to recognize all commits like normal

Merging your Feature with Master This is still a work in progress… git checkout master git merge do-new-feature

After submitting and merging my feature I got a message git status On branch master Your branch is ahead of 'origin/master’ by 1 commit. (use “git push” to publish your local commits) nothing to commit, working directory clean

I was afraid to use git push, however… it was fine. Because origin/master referred to my local forked repository. Not the master repository. It must be the difference between the -u flag.

GRABBING THE LATEST FROM UPSTREAM When you fork a repository. Sometimes changes happen and you need to grab the latest from the ORIGINALLY FORKED REPOSITORY. This is repo is generally called upstream. Like a salmon swimming upstream. Lots of work just to get laid. Gotta fight bears and shit. I heard salmon in Colorado have been getting lazier now that pot is legal there. Not as many are making the journey. Pizza sounds just as good and a lot less effort.

You definitely need to add the upstream repository to your local git. git remote add upstream [original repository] ex. git remote add upstream https://github.com/antwonlee/neck-beards.git git fetch upstream git rebase upstream/master

Then you’re gonna be cool to fly.

The Panic Button

git stash -u —> Stashes all files in your working directory away and saves them for use later

git stash drop —> REMOVES all files currently saved in your stash

git stash pop —> Puts everything from your stash, back into your working directory. A commonly used command after git stash -u

Working with Git History

git log —> The full blown comments, usually bloated

git log –oneline –decorate –graph –all —> beautiful display of git history with all branches and master history
But that’s a lot to remember, so we can create an alias. git config --global alias.sla 'log --oneline --decorate --graph --all'.

Now the command becomes
git sla

Searching the History

git glog This will search through all commit messages for a specific word that you specify with regular expressions. For instance $ git glog '(initial|bro)' => will find all commits with the matching either initial or bro


-S
git sla -S current_user —> This find all git commits where the number of instances of current_user has changed.


git log –oneline – Gemfile other options git log –oneline – source/index.html.erb —> allows us to see which commits changed the Gemfile


git blame Gemfile —> tells us who changed the gemfile


git show <commit hash> —> shows the complete log and diff of changes

Fixing mistakes!!

Scenario: Oops, I forgot to add a file into git and I committed.. Simply add the file that is missing and run one of the following commit commands.. git commit –amend –> opens up editor to amend the commit message

git commit –amend –no-edit –> adds the file to the commit without editing the commit message


git unstage <filename> —> unstages a file that has been under the affect of git add. The files will stay changed!


git checkout <filename> —> will discard changes to that file. It destroys them, so be careful with this command.

git uncommit —> will uncommit the last commit, but keep all the files changed.

cancel a commit —> Sometimes you’re editing a message in a commit… and that commit is not actually what you want to do… just exit vim with a non-0 exit code using –> :cquit

Commit partial changes to files

For instance, you change the Gemfile, one of the changes is for the git add –patch

Random stuff…

git help sla –> will show how sla is aliased and configured!

git diff –cached —> will see the difference between the last commit and the currently staged files

git diff —> Will show the changes in the unstaged files

Post Content