A brief Intro
Welcome to Brief Git - Notes For Version Control With Git.
This book is not written for beginners in Version Control. If you do not have any experience using Git, I would suggest reading the entire 517 pages of the Pro Git publication by Scott Chacon and Ben Straub which is available here. You can also find a helpful and handy 2 page Git cheat sheet from Github here.
The purpose of this book is to include all the commands you will need to run and manage a project as an administrator or to contribute as a collaborator. Feel free to save it as a pdf using the quick search to go directly to the commands that are useful to you.
This book is published under the MIT license, and provided on an as-is basis. I hope you enjoy it, I hope it helps you learn Git, and I hope you'll support this project by downloading a copy of the book at Amazon or iBooks.
Getting Started
Installing Git
To install Git is as easy as going to this website and downloading the relevant package. If you need to install the binary in Fedora or any closely related RPM-based distribution
$ sudo dnf install git-all
If you’re on a Debian-based distribution then
$ sudo apt install git-all
To install Git on a Mac I would suggest going with Homebrew. Assuming you have Homebrew installed, type
$ brew install git
Once it is installed, then type the following line, which will set our path to the local git distro instead of Apple’s
$ export PATH=/usr/local/bin:$PATH
To update it in the future all you will need to do is run
$ brew upgrade git
Getting Started
Git Config
Once you are all done you can proceed by adding your identity
$ git config --global user.name "_USER_NAME_"
$ git config --global user.email name@domain.com
To chose your Git text editor run the command
$ git config --global core.editor Vim
To check your settings
$ git config --list
Getting Started
Getting Help
To see the most common Git commands used in various situations
$ git help
For a full manual of a specific verb choose one of the following methods
$ git help _VERB_
or
$ man git-_VERB_
For a quick refresher
$ git _VERB_ -h
Getting Started
Getting A Git Repository
To start controlling a directory with Git there are two ways:
- You can take a local directory that is currently not under version control, and turn it into a Git repository
$ git init
- You can clone an existing Git repository from elsewhere.
$ git clone https://repository.com
To specify the new directory name as an additional argument
$ git clone https://repo.com _NEW_NAME_
Getting Started
Recording Changes To The Repository
To check the status of your files
$ git status
To start tracking one new file run
$ git add _FILE_NAME_
To start tracking all the files run
$ git add -A
Getting Started
Ignoring Files
To set your rules of file ignoring, you have to create a .gitignore file
To ignore all .a files
*.a
But do track lib.a, even though you're ignoring .a files
!lib.a
Only ignore the TODO file in the current directory, not subdir/TODO
/TODO
To ignore all files in the build/ directory
build/
To ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
To ignore all .pdf files in the doc/ directory and any of its subdirectories
doc/**/*.pdf
For ready .gitignore files
Committing
Committing Your Changes
To commit your changes and add a message through a text editor run
$ git commit
To commit your changes while adding a message to the command line
$ git commit -m "_MESSAGE_"
To do staging and committing at the same time, run
$ git commit -a -m "_MESSAGE_"
Committing
Removing & Renaming Files
To remove the version control completely you have to remove the .git folder
$ rm -r .git
To check if the .git folder has been removed, run the command
$ ls -a
To remove a specific file, run
$ git rm _FILE_NAME_
To remove a file from the staging area, run
$ git rm -f _FILE_NAME_
To keep the file on the hard drive but not have Git track it anymore
$ git rm --cached _FILE_NAME_
Renaming a file is the same as moving it to a different place
$ git mv _FILE_FROM_ _FILE_TO_
Committing
Saving Without Committing
Stashing is saving unfinished work when you need to checkout to a different branch but you don't want to commit the unfinished changes
$ git stash save
And when you need to go back to your stashing work
$ git stash list
$ git stash apply
or
$ git stash apply stash@{2}
Logging
Viewing The Commit History
To view the commit history, run
$ git log
To see the differences introduced in each commit and limit the entries to 2
$ git log -p -2
To see some stats for each commit
$ git log --stat
To see more readable options of the commits
$ git log --pretty=oneline
$ git log --pretty=short
$ git log --pretty=full
$ git log --pretty=fuller
You can specify the log format with the command
$ git log --pretty=format:"_OPTIONS_INSIDE_HERE_"
The available options can be found in the following table
Value | Option Description |
---|---|
%H | Commit hash |
%h | Abbreviated commit hash |
%T | Tree hash |
%t | Abbreviated tree hash |
%P | Parent hashes |
%p | Abbreviated parent hashes |
%an | Author name |
%ae | Author email |
%ad | Author date (format respects the --date=option) |
%ar | Author date, relative |
%cn | Committer name |
%ce | Committer email |
%cd | Committer date |
%cr | Committer date, relative |
%s | Subject |
To print a graph with the branch and merge history
$ git log --graph
Limiting log output with --since or --until for e.g. weeks/years/days/minutes or specific dates "2008-01-15" or "2 years 1 day 3 minutes ago". The --author filter is also available
$ git log --since=2.weeks
To find the last commit that added or removed a reference to a specific function
$ git log -S _FUNCTION_NAME_
To list all your branches
$ git branch -a
A nice example of a graph is the following
$ git log --pretty=format:"%h %s" --graph
Undoing Things
Minor Changes To A Commit
To redo a commit, make additional changes, stage them, and commit again, run
$ git commit --amend
This is an example where the second commit replaces the results of the first
$ git commit -m "initial commit"
$ git add forgotten_file
$ git commit --amend
Undoing Things
Unstaging A Staged File
To unstage one of the staged files
$ git reset HEAD _FILE_NAME_
Undoing Things
Unmodifying A Modified File
To revert it back to what it looked like when you last committed, run the following command
$ git checkout -- _FILE_NAME_
Notice that any changes you made to that file are gone — Git will copy another file over it.
Undoing Things
Setting The HEAD To A Previous Commit
To checkout to a given commit, run
$ git checkout _COMMIT_ID_
At this point you can create a branch and start to work from this point on
$ git checkout -b _BRANCH_NAME_
To move back to a commit and effectively lose the later commits
$ git reset --hard _COMMIT_ID_
This will destroy any local modifications. Don't do it if you have uncommitted work you want to keep.
To push the changes to the remote after a hard reset you will need to run
$ git push -f _REMOTE_ _BRANCH_
Undoing Things
Data Recovery
Git silently records what your HEAD is every time you change it. Each time you commit or change branches, the reflog is updated. You can see where you’ve been at any time by running the command
$ git reflog
To see the same information in a much more useful way, we can run the command
$ git log -g
If the commit you are looking for is not in the reflog then you should run the command that shows you all objects that are not pointed to by another object
$ git fsck --full
To recover it, create a new branch at that commit. The following command will create the recover-branch that is where your master branch used to be
$ git branch recover-branch _COMMIT_ID_
To delete the uncommitted state and return the working tree to the last committed state
$ git reset --hard HEAD
To delete the latest commit, and return to the previous one (one before HEAD)
$ git reset --hard HEAD~1
To return a single file to its last committed state
$ git checkout -- _FILENAME_
or
$ git checkout HEAD _FILENAME_
Stop a file being tracked (but do not delete it from the working directory, add to .gitignore etc after this)
$ git rm --cached _FILE/FOLDER_NAME_
Restore a file to a previous commit
$ git checkout _COMMIT_ID_ _FILE_TO_RESTORE_
Working With Remotes
Log - Push - Pull
To see a list of all the connected remote servers, run the command
$ git remote
To see the URLs that Git has stored
$ git remote -v
To add a new remote
$ git remote add _REMOTE_ _URL_
To fetch the data from a remote repository
$ git fetch _REMOTE_
To fetch the data and merge simultaneously
$ git pull _REMOTE_ _BRANCH_
To push to the connected remote server
$ git push _REMOTE_ _BRANCH_
To inspect the remote server
$ git remote show _REMOTE_
To rename a connected server
$ git remote rename _OLDNAME_ _NEWNAME_
To remove a connection with a server
$ git remote remove _REMOTE_
To checkout a remote branch
$ git checkout -b _LOCAL_BRANCH_NAME_ _REMOTE_/_REMOTE_BRANCH_NAME_
To delete a local branch
$ git branch -d _BRANCH_NAME_
To delete a remote branch
$ git push origin :_BRANCH_NAME_
or
$ git push origin --delete _BRANCH_NAME_
To pull and merge unrelated histories, type the command
$ git pull --allow-unrelated-histories _REMOTE_ _BRANCH_
Tags and Aliases
Working With Tags
To list your tags
$ git tag
To look for a specific tag, run the command
$ git tag -l "v1.8.5*"
To create annotated tags
$ git tag -a v1.4 -m "my version 1.4"
To see the tag data
$ git show v1.4
To create lightweight tags
$ git tag v1.4-lw
If you want to create a tag later, after the commit
$ git tag -a v1.2 _COMMIT_ID_
To push tags to a shared server
$ git push origin v1.5
To push a lot of tags to a shared server simultaneously
$ git push origin --tags
To checkout to a tag
$ git checkout 2.0.0
If you need to fix a bug on an older version you will want to create a branch and commit the changes there
$ git checkout -b version2 v2.0.0
Tags and Aliases
Git Aliases
To set up an alias for each command so you won't have to write the whole command
$ git config --global alias.ci commit
$ git config --global alias.unstage 'reset HEAD --'
To add a 'last' command, so you can see the last commit, you can do this
$ git config --global alias.last 'log -1 HEAD'
Branches
Creating And Switching
To create a new branch
$ git branch _BRANCH_NAME_
To switch to an existing branch
$ git checkout _BRANCH_NAME_
To print out the history of your commits, showing where your branch pointers are and how your history has diverged
$ git log --oneline --decorate --graph --all
To create a new branch and switch to it at the same time
$ git checkout -b _BRANCH_NAME_
or
$ git branch _BRANCH_NAME_
$ git checkout _BRANCH_NAME_
Branches
Merging
To merge a branch to master
$ git checkout master
$ git merge _BRANCH_NAME_
Then you can delete the merged branch with the command
$ git branch -d _BRANCH_NAME_
To use a graphical tool to resolve merging issues
$ git mergetool
To get a simple listing of your current branches
$ git branch
To see the last commit on each branch
$ git branch -v
To delete a remote branch
$ git push origin --delete _BRANCH_NAME_
Branches
Cleaning Out
To check for whitespace errors, before committing, run the command
$ git diff --check
License
Copyright (c) 2018 Fotios Tragopoulos
Brief Git is licensed under the MIT License
A short and simple permissive license with conditions only requiring preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.