Skip to content

Instantly share code, notes, and snippets.

@hayespotter
Created November 14, 2016 15:06
Show Gist options
  • Select an option

  • Save hayespotter/71c42702cb3497c266f4bbb6a28cc7c4 to your computer and use it in GitHub Desktop.

Select an option

Save hayespotter/71c42702cb3497c266f4bbb6a28cc7c4 to your computer and use it in GitHub Desktop.
git guide
#git basics and standards
commands you should be comfortable with:
--help
add
reset
status
branch
checkout
commit
diff
fetch
pull
push
## Check out a repository
git clone username@host:/path/to/repo
## Workflow
your local repository consists of three "trees" maintained by git. the first one is your **Working** Directory which holds the actual files. the second one is the **Index** which acts as a staging area and finally the **HEAD** which points to the last commit you've made.
Unstaged commits are on your working direccting, staged files are on index, commits go to HEAD
## add & commit
git add <filename>
git add * //for everything
git commit -m 'message'
//this will add everything and commit
git commit -am 'message'
Now everything is committed to HEAD but not yet in the remote repository.
## pushing changes
To send changes to your remote repo:
git push origin <branch-name>
When pushing to a branch for the first time you may want to add the 'u' flag to set its upstream.
git push -u origin <branch-name>
## branching
Branches are used to develop features isolated from each other. The master branch is the "default" branch when you create a repository, and we use this to serve the production code. Never commit directly to master, or any staging or development branch. All commits should be merged in from feature-branches.
**create a new branch named "feature_x" and switch to it using**
git checkout -b feature_x
**switch back to master/development**
git checkout master
**delete a branch that you no longer need**
git branch -d feature_x
**a branch is not available to others unless you push the branch to your remote repository**
git push origin <branch-name>
## update and merge
**to update your local repo to the newest commit**
git pull
In your working directory this will fetch and merge remote changes.
**to merge another branch into your current branch**
git merge <branch-name>
In both cases git tries to auto-merge changes. Unfortunately, this is not always possible and results in conflicts. You are responsible to merge those conflicts manually by editing the files shown by git. After changing, you need to mark them as merged with 'git add'
**before merging, you can preview changes**
git diff <source-branch> <target-branch>
## replace local changes
In case you mess something up, you can checkout previous commits
git checkout <commit-ID>
**If you want to drop all of your local changes**
git stash && git stash drop
There are a number of methods to do this, but this is my preferred method.
-
-
# tips and common issues
### realize that you have been working on the wrong branch
If you have not committed your changes, it is enough to:
git checkout -m <correct-branch>
-
### logs
**Quick and easy way to look at logs and get commit ID's**
git log --oneline --graph
You can navigate with the arrow keys, and hit 'q' to quit.
**log changes in a file**
git log -p <filename>
**log changes for some specfic lines in a file**
git log -L 1,1:<filename>
-
###committed something and immediately realized I need to change one minor thing
# make your change
git add . (or add individual files)
git commit --amend
# follow prompts to change or keep the commit message
# now your last commit contains that change
**change message on last commit**
git commit --amend
-
### accidently committed something to master that should have been on a new branch
# create new branch from the current state of master
git branch new-branch
# remove commit from master
git reset HEAD~ --hard
git checkout new-branch
# now your commit is in the new branch
This does not work if you've already pushed to origin.
-
### tried doing a diff and nothing happened
git diff --staged
Git won't do a diff of files that have been staged without this flag. Not a bug, this is intended.
## merge conflicts
When in doubt, ask for help.
**list files that have conflicts**
grep -lr '<<<<<<<' .
-
###resolve simple conflicts
**accept our local version**
git checkout --ours PATH/FILE
**accept remote/other-branch version**
git checkout --theirs PATH/FILE
**If you have multiple files and you want to accept local/our version**
grep -lr '<<<<<<<' . | xargs git checkout --ours
**If you have multiple files and you want to accept remote/other-branch version**
grep -lr '<<<<<<<' . | xargs git checkout --theirs
### fix complex conflicts
Just open up the conflicted files in your code editor and fix it manually. Super easy. Stage and commit when finished.
-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment