#Table of Contents
This repository is meant to be a collection of useful commands, tools, and tips & tricks around Git and GitHub.
There are a lot of the not-so-basic things I have to look up in certain cases, or particular questions people are asking me. This repository should serve as a place to collect all those things.
If you have any suggestions or want to make additions, I would be very happy if you could send me an email, leave me a message on google+, or even send me a tweet on twitter (given you can fit it within the 140 character limit ;)).
Or even better: It would be great if you would simply fork this project and send me a pull request.
Shell command to pull changes from the remote for all subdirectories in the current directory.
find . -type d -depth 1 -exec git --git-dir={}/.git --work-tree=$PWD/{} pull origin master \;
For your convenience - since the syntax may not be easy to remember - you can put this command into a shell script and save it into your main GitHub directory to conveniently pull all changes for all your contained GitHub repositories via
sh pull_all.sh
Replace local changes with most recent contents from HEAD
.
git checkout -- <file>
Fetches the most recent files from the remote and resets the master branch to those by overwriting all local changes.
git fetch --all
git reset --hard origin/master
Deleting a file or directory from the Git history is - in my opinion - was an option that was not intended by its creator. However, sometimes large binary files can cause a repository to grow over multiple Gigabytes in size, and in this case it might be useful to delete those.
git filter-branch --prune-empty --index-filter 'git rm -rf --cached --ignore-unmatch MY-BIG-DIRECTORY-OR-FILE' --tag-name-filter cat -- --all
(for more info, see the original article)
After that, you might want to commit your changes and clone the "slimmer" repository:
git clone --no-hardlinks file:///Users/yourUser/your/full/repo/path repo-clone-name
###Connecting to GitHub and GitLab servers via SSH [back to top]
If you want to communicate to the GitHub/GitLab servers via a "more secure" connections or if your are using a terminal that doesn't support https
.
Create a new branch called "develop"
git branch develop
Switch to new branch
git checkout develop
Alternative shorthand for creating and switching:
git checkout -b develop
Delete the branch
git branch -d develop
Push branch to the remote server:
git push origin develop
Preview differences between branches
git diff <source_branch> <target_branch>
Merge changes from new branch back into the master branch
git checkout master
git merge develop
Add individual files
git add <file>
Recursively add everything in the current directory
git add .
Commit changes
git commit -m 'some commit message'
Shorthand for adding all files and committing them in 1 command
git commit -a -m 'some commit message'
Removes staged (add
ed files from the commit)
git rm --cached <file>
-
one line
$git log --pretty=oneline
7d8d551bc3c244659e5fef20bf5cf9cbe4db9f3c changes to a.txt ca6b353428d78dd3d8f03f03ee6128df703462d9 created a.txt and b.txt
-
short
$git log --pretty=short
commit 7d8d551bc3c244659e5fef20bf5cf9cbe4db9f3c Author: rasbt [email protected]
changes to a.txt
-
full
$git log --pretty=full
commit 7d8d551bc3c244659e5fef20bf5cf9cbe4db9f3c Author: rasbt [email protected] Commit: rasbt [email protected]
changes to a.txt
-
fuller
$git log --pretty=fuller
commit 7d8d551bc3c244659e5fef20bf5cf9cbe4db9f3c Author: rasbt [email protected] AuthorDate: Sat Jun 7 11:34:00 2014 -0400 Commit: rasbt [email protected] CommitDate: Sat Jun 7 11:34:00 2014 -0400
changes to a.txt
Warning: This should only be done for local commits that you haven't been uploaded to a remote, yet.
Here, we want to squash those 3 commits into 1 single one to keep the commit history "lean":
$git log --pretty=oneline
d3abc1933ed9782ddae0ef07505b2a0cd54dbe77 changes to a again
7d8d551bc3c244659e5fef20bf5cf9cbe4db9f3c changes to a.txt
ca6b353428d78dd3d8f03f03ee6128df703462d9 created a.txt and b.txt
We will use rebase
for the HEAD
(latest commit) up to 3 commits ago HEAD~3
$git rebase -i HEAD~3
And editor will open with the following example contents:
pick 3abc193 changes to a again
pick 7d8d551 changes to a.txt
pick ca6b353 created a.txt and b.txt
# Rebase d3abc1..4dbe77 onto 60709da
#
# Commands:
# p, pick = use commit
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
Now, we change the first 3 lines to summarize all 3 commits into the latest commit:
pick 3abc193 changes to a again
squash 7d8d551 changes to a.txt
squash ca6b353 created a.txt and b.txt
After we save and close the editor, a new editor window will open where we can modify the commit message and the rebase
should be successfully completed.
Git Cheat Sheet
This cheat sheet is probably something that you want to print out and pin to your wall right next to your monitor: All the most important Git commands on one page.
git - the simple guide
A very brief overview of the Git essentials as it headline says: "just a simple guide for getting started with git. no deep shit ;)".
In my opinion it is a little bit too "concise" to be a good introduction to Git for absolute beginners, but I think that this is a very good look-up reference for people who are just getting started.
Pro Git
Probably the most thorough resource about Git you can find. Its 200+ pages (in the free PDF eBook version) might be a little bit overwhelming for Git newbies, but I would still recommend the first 2 chapter as a good introduction to Git - and the rest of the book to the more advanced users.
help.github.com
GitHub help pages are especially useful for everything that is about setting up a connection to the GitHub servers and managing your repositories.