# Git command cheat sheet Git is the perfect source control for my home projects. It is free, works on mac, linux and windows, makes it easy to share code through github and takes me out of my Microsoft technology comfort zone. Here are the basic commands I need to be productive. ## Basics Adding all changed files in the current directory to the commit: ``` git add . ``` Adding any deleted files in the current directory to the commit: ``` git add -u . ``` Seeing which files are waiting to be committed: ``` git status ``` Committing the currently staged files to a repository: ``` git commit -m "a very well explained commit message" ``` Pushing my changes to another repository: ``` git push origin master ``` Get changes from another repository and apply on top of the existing changes: ``` git pull origin master ``` Get changes from another repository, but rewind history, apply the changes and then apply the new changes on top. This makes the history more straight forward: ``` git pull --rebase origin master ``` ## Setting up a new repository Creating a new repository in the current directory: ``` git init . ``` ## Working with remote repositories Adding a new remote repository: ``` git remote add origin git@github.com:helephant/SeriesLink.git ``` Changing the URL of a remote repository ``` git remote set-url origin git@github.com:helephant/SeriesLink.git ``` Seeing a list of all remote repositories: ``` git remote -v ``` Cloning an existing repository: ``` git clone git@github.com:helephant/SeriesLink.git ``` ## Committing files Discard current changes - rollback the file in the working directory to the latest committed version: ``` git checkout myfile.cs ``` Unstage changes - keep the changes in the working directory, but roll the version of the file in the staging index back to the last committed version: ``` git reset HEAD myfile.cs ``` Get rid of any unstaged files that have been added. -d tells git to include directories. -f is force. ``` git clean -df ``` Commit all changed files - does the add and commit in a single step. Will only commit files that are already in the repository. New, untracked files will not be commited: ``` git commit --all -m "a very well explained commit message" ``` Ammending the last commit (because you've forgotten files or messed up the commit message): ``` git commit --ammend ``` Seeing differences between your working directory and the staging index: ``` git diff -w ``` Seeing differences between your working directory and the last commit: ``` git diff -w HEAD ``` ## History Viewing the history of a file: ``` git log src/series-link.php ``` View the complete history of a file that has been renamed: ``` git log --follow src/series-link.php ``` View the summary of the last 3 commits: ``` git log -n 3 --format=oneline HEAD ``` Search through the history of a file for a term: ``` git log -S command-line --format=oneline style.css ``` Search through versions of a file for changes that involve the search term: ``` git diff -S "latest-post" master~2 master~4 style.css ``` Viewing more details about the last commit: ``` git show ``` Viewing more details about a particular commit: ``` git show 9d35508c249f1e7060c54165fe0767c01f598359 ``` Seeing differences between two commits for a file: ``` git diff afb13de89c3 eb72e0b5182 -- style.css ``` ## Rolling back a file Rolling back a file to the last commited version (just replaces the file with the last commited version). ``` git checkout myfile.cs ``` Unstage changes - keep the changes in the working directory, but roll the version of the file in the staging index back to the last committed version: ``` git reset HEAD myfile.cs ``` Removing unstaged files: ``` git clean -f ``` ## Branching Create a new branch from the latest commit: ``` git branch new-feature-prototype ``` Create a new branch and switch to it straight away: ``` git checkout -b new-feature-prototype ``` List all branches: ``` git branch ``` Switch between branches: ``` git checkout new-feature-prototype ``` Switch between branches and merge any pending changes in your working directory: ``` git checkout -m new-feature-prototype ``` View a file in another branch: ``` git show master:style.css ``` Delete a branch that you're finished with: ``` git branch -d new-feature-prototype ``` ## Merging Merging changes from a branch into the current branch: ``` git merge -m new-feature-prototype ``` Generate a commit graph to graphically see branch history: ``` git log --graph --format=oneline --abbrev-commit ```