Skip to content

Instantly share code, notes, and snippets.

@davidneedham
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidneedham/f7fbf4748372cc8809fd to your computer and use it in GitHub Desktop.
Save davidneedham/f7fbf4748372cc8809fd to your computer and use it in GitHub Desktop.
Git notes
http://www.gitref.org
-----------------------------------------
Facts:
-----------------------------------------
Git makes it really easy to use version control for everything, not just project files. (ie. Aarons personal computer configuration settings.)
Commit IDs are hash's, commonly referred to by the first 4 or first 8 chars
There is a .git folder at the root of the repository, not within every subdirectory. However, you can still do commands within any subdirectory from root.
3 stages a file or directory can be in at any given point:
- unstaged (new or changed files - not tracked)
- staged (not yet committed, but tracked)
- committed
There's an author and the commiter, so that it's logged who authored content vs who just committed it.
.gitconfig - where name and email settings are handled for committer information. Color handling too. Can set specific authoring information on a per repository basis.
Don't use force flags (-f --force) unless you absolutely have to! Doing so can damage the history and lose your and others changes. ("the nuclear option" - Nick)
.gitignore files - tell git what to ignore (*.temp, DS_store, ./cache, etc). That way you can still git add . without adding files/folders you never want git to track. You can use !./cache to force it to NOT ignore something.
git update-index --assume-unchanged sites/default/settings.php - This tells git to assume this file is never changed
git update-index --no-assume-unchanged sites/localhost/settings.php - Tell git to start tracking again
git commit --amend -m "My new message" - this tells git to amend the previous commit (which has not been pushed!) with this new message and files.
-----------------------------------------
Commands:
-----------------------------------------
git config --global user.name "John Dough"
- If no name or email is set up, git will generate something randomish for you, so you should always add your global user information if you have not already.
git init - in this directory, create a git repo.
git status - This is your friend, you will use it often. Information about the repo and hints for common commands to interact with those files, branch name, both staged and unstaged files + common commands to interact with those files.
git add - Add files to be tracked by the repo. This applies to both newly created files, and files which have changes in them!
git add . - add everything from this directory up (subdirectories, etc)
git commit -m "new message" - Add a commit, with a message (just like svn). If you get an error saying 'no changes added to commit' it's probably because you haven't staged them (git add .).
git commit -am "new message" - Add all (like git add .) and leave a message. -am does NOT add new files, only changed files.
git log - you can see recent commits, log message, commit ID, date
git log -n1 - only the latest log message. (-n10 shows latest 10 messages.)
git log graph - show logs, with a visual representation of branches
git diff - Will only look at unstaged changes
git diff HEAD - check changes locally vs committed version
git diff commitHashID - to compare vs a hash
gitg (linux?), gitk (depreciated) , gitx (mac) - show a GUI for git! Github has a better gui, if you're using that as a remote repo.
git remote add origin url-to-remote-repo - Add a remote repo (this is displayed on github & drupal.org). origin is the usual name for the first remote repo, but you can call it whatever you want. If you add another remote repo, you can call it something else, then push to and pull from those remotes respectively.
git push -u origin master - push our branch master onto the remote repo. In most cases the git push command works identically. If you try to push, and you get an error about losing history, that's because there was a remote change and you need to git pull first.
git pull - pull down the latest changes (same as svn update). If changes are not within a couple lines of each other, or if the changes are in different files, git has no problem merging them automagically. It's a best practice to git pull regularly, even if you're not doing git push very much. That way there is less of a chance of there being a conflict, you have the remote changes.
-----------------------------------------
Usual workflow:
-----------------------------------------
* Make changes, add files, etc.
* git status view the changes, check the branch, etc
* git add . (only if adding new files)
* git commit -m "Made changes"
* git push (if working with a remote server)
-----------------------------------------
What to do during a conflict:
-----------------------------------------
* git pull (oh snap, you got a conflict)
* resolve conflict by modifying the file(s) - conflicts from changes are marked
* git commit -am "My message"
* git push
-----------------------------------------
Branches:
-----------------------------------------
git branch -a - show all of the branches, marking the one we're in with a *
git branch branchname - create a new branch from the branch you are currently in
git checkout branchname - switch to the branch you want to work on. Any changes you make here will only show up when you're in this branch. If you go back to the master branch, your changes wont be there. This makes it easy to switch your focus from one feature to another, before committing and merging the changes into the master.
git merge branchname - merge the branchname branch into your current branch
-----------------------------------------
Bonus!:
-----------------------------------------
git stash - store all of your unstaged changes. This allows you to switch branches, etc before staging and committing the changes.
git branch -d branchname - Delete the branch you no longer want.
git checkout filename HEAD^ - Revert changes for a particular file 1 commit before HEAD.
git checkout -- filename to discard changes in a working directory
git reset HEAD filename - Revert all of your changes for a file since the last commit (unstage them) .
git reset --hard commitHashID - change both local working tree + index
git reset --hard ORIG_HEAD - undo the previous git pull. Useful if there's a merge / conflict that you don't want to deal with right away.
git log --name-status --color | grep "mobile-menu" -A 5 -B 10 - find a reference to the file "mobile-menu" in the git log in addition to 5 lines after and 10 lines before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment