Top Most Used Git Commands

Thought process using Git

Starting a new branch and push
1) git checkout -b ‘my-new-branch’
     – new branch because new feature (think of this as putting things in silos)
2) git diff
     – Once I’m done, I look at the diff to look for obvious human / logic errors
3) git add -A
    – stage all changes
4) git commit
    – Write a useful commit message so others know what this commit is about
5) git pull rebase –origin master
     – Rebase to get your changes to the very top. (You’re change can be buried deep down if the company follows continuous deployments and your branch is old. Also this will catch merge conflicts
6) git log
    – Look at the log
7) git push “remote-v” my-new-branch
    – push to your branch. Then you’ll have to merge from Github

Testing a PR locally. 
1) git remote add john ‘ssh clone url’
2) git pull john pr-branch
3) git pull –rebase origin master
 4a) IF merge conflict. Tell commiter about conflict and ask them to fix
 4b) IF rebase goes smoothly. Continue to step 5
5) git checkout master
6) git merge –no-ff pr-branch
7)Add message saying “sign-off: John”

List of Git commands I use as a developer.

git add -A
Stage all un-staged changes

git commit 
Commit all staged files. This will open a editor for you to write a message in
git commit -m  ‘Add new commit message’
Commit all staged files. Add commit message directly on the same line

git remote -v
Display all remote connections
git remote add origin “SSH clone URL”
Add a remote endpoint
git remote remote origin “what”
Remove remote endpoint

git log
displays commit message and history
git log -G /regex/ (Tom C.)
display commit with specified regex
git log -S /search string/ (Tom C.)
display commit with specified string
git log –stat –max-count=10
see last 10 logs

git log –all — path_of_file (Thomas O.)
Displays all commits made to the specific file

E.G –  git log –all — app/views/blogs/index.html.erb
git log –diff-filter=D –summary | grep delete | grep somezing (Thomas O.)
Filter your logs by using grep

git pull –rebase origin master
My top 5 used git command. In a continuous development environment, you’re branch can become outdated easily. This pull’s in the master branch underneath your current branch. Your commit will be on top.

git diff 
Display changes made on the current branch

git status
List modified files
List committed files and uncommitted files

git stash
All changes will be stashed away to it’s pre-change state
git stash –keep-index
All changes that hasn’t been committed will be stashed away.
git stash drop
All stashed changes will be removed
git stash apply
All stashed changes are restored

git whatchanged -p –abbrev-commit –pretty=medium
Combination of git log and git diff. It will show the diff for each commit

git commit -m ‘wip’
wip = work in progress. I use it to mark commits that I’m still actively make changes to. I don’t actually push this to production. I would either rename the commit once it’s done or I’ll rebase.

git rebase –soft HEAD~1
Two things with rebasing. Either squash commits (squish multiple commits to a single commit) or Edit commits messages

git rebase –i ‘sha’
Allows you to squash, edit commit messages but you can specify the ‘sha’

git rebase –abort

When I mess up my rebase (merge conflicts or what have you). This comes in handy to put me back in the original step before rebasing.
git revert ‘sha’
Return branch to the state where sha (commit)
git reflog 
See all steps committed. I personally don’t use this often. Only in crucial times when I’m messing up in git will I have to use this. 

git merge –no-ff pr-576 (Chris A.)
adds the merging commit so others can see thats the following 3 commits are part of 1 commit. I like to `sign off: john moon` so others can see that I added/merged the list of commits.

git reset –hard origin/master (Chris A.)
Resets back to Master. This one’s nice. Creating spikes often leaves a mess of a code on your branch. This helps you go back to zero. 
ssh-add ~/.ssh/id_rsa_private
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I manage multiple accounts. 1 for work and 1 for personal use. I defaulted my .ssh to my work github. This led to problems with my personal account not having access to push or pull. I had to figure out how to give my personal account permission. I added an account id_rsa_private for my private use. Then every time I encounter that error message, I run this command and everything is groovy. 

ssh-add -K ~/.ssh/id_rsa_private (Chris A.)
Adds key permanently 

Happy Git 
John 

Standard