diff --git a/help/git.md b/help/git.md new file mode 100644 index 0000000..1239dd1 --- /dev/null +++ b/help/git.md @@ -0,0 +1,40 @@ +# Git + +## Common commands + +- Start a new git repository: `git init` **OR** `gi` +- Add files to commit: `git add ` **OR** `ga ` +- Commit those files to the repository: `git commit -m ` **OR** `gc ` +- Undo the last commit: `gr` +- Undo the last commit and delete changes: `grr` +- Update your local repository with a remote repository: `git pull -u ` + - The most common use is `git pull -u origin master`, although this also works for upstream remotes + - +- Show the status of the local repository: `git status` **OR** `gs` **OR** `git s` +- Push changes from your local repository to a remote repository: `git push ` **OR** `git push` **OR** `gp` + - The most common use is `git push origin master`, which is usually the default for `git push` +- Show the difference between the staging area and the working tree: `git diff` **OR** `gd` +- Show a log of all the commits: `git log` (full log) **OR** `git lg` (commits only, easier to read) + +Note that the `-u` flag means `set-(u)pstream-to`. It records the location so you don't have to set which remote to push or pull from every time. + +Also note that `git clone` makes a remote name of `origin` by default. This is why `git push -u origin master` is usually used. + +### Working with Branches + +- Show all the branches: `git branch -a` +- Checkout a different branch: `git checkout ` + - Useful for 1) checking out the master or development branch, 2) checking out an upstream branch, and 3) checking out a feature branch +- Create a new branch: `git checkout -b ` +- Delete a branch when you're done with it: `git branch -d ` + +## Less common commands + +- Removes files to commit: `git rm ` +- Delete all local changes in the repository: `git clean -f` +- Show all the diffs and changes from the last N commits: `git log -p -` +- Show the differences between your repository and a remote one: `git log -p master../master` +- View all remotes for the current repository: `git remote -v` (e.g. GitHub mirror, friend's fork, upstream, etc.) +- Show the entire contents of a commit, including diffs: `git show ` +- Search for a string in the files of a git repository: `git grep ` +