git notes
git init
If you’re starting to track an existing project in Git, you need to go to the project’s directory and type the command above. This creates a new subdirectory named .git that contains all of your necessary repository files – a Git repository skeleton
git add *.c
git add LICENSE
begin tracking files
git clone https://github.com/libgit2/libgit2
cloning an existing repository
git clone https://github.com/libgit2/libgit2 mylibgit
That command does the same thing as the previous one, but the target directory is called mylibgit
git status
Checking the Status of Your Files
git status -s
short status
git diff
see what is still unstaged
git diff --cached
see what you’ve staged so far (--staged and --cached are synonyms)
git commit
committing your changes
git commit -m "Story 182: Fix benchmarks for speed"
you can type your commit message inline with the commit command by specifying it after a -m flag
git commit -a -m 'added new benchmarks'
Adding the -a option to the git commit command makes Git automatically stage every file that is already tracked before doing the commit, letting you skip the git add part
git rm PROJECTS.md
remove a file from Git
git rm --cached README
keep the file in your working tree but remove it from your staging area. In other words, you may want to keep the file on your hard drive but not have Git track it anymore
git rm log/\*.log
This command removes all files that have the .log extension in the log/ directory
git rm \*~
This command removes all files whose names end with a ~
git mv file_from file_to
moving files
git log
lists the commits made in that repository in reverse chronological order
git log -p -2
shows the difference introduced in each commit. You can also use -2, which limits the output to only the last two entries
git log --stat
see some abbreviated stats for each commit
git log --pretty=oneline
The oneline option prints each commit on a single line. In addition, the short, full, and fuller options show the output in roughly the same format but with less or more information, respectively
git log --pretty=format:"%h - %an, %ar : %s"
The most interesting option is format, which allows you to specify your own log output format. This is especially useful when you’re generating output for machine parsing
git log --pretty=format:"%h %s" --graph
another log option called --graph. This option adds a nice little ASCII graph showing your branch and merge history
git log --since=2.weeks
time-limiting options such as --since and --until are very useful. For example, this command gets the list of commits made in the last two weeks
git log -Sfunction_name
takes a string and only shows the commits that introduced a change to the code that added or removed that string
git log --pretty="%h - %s" --author=gitster --since="2008-10-01" \ --before="2008-11-01" --no-merges -- t/
see which commits modifying test files in the Git source code history are merged and were committed by Junio Hamano in the month of October 2008
git log --oneline --decorate
command that shows you where the branch pointers are pointing
git log --oneline --decorate --graph --all
print out the history of your commits, showing where your branch pointers are and how your history has diverged
git commit -m 'initial commit' git add forgotten_file git commit --amend
if you commit and then realize you forgot to stage the changes in a file you wanted to add to this commit, you can do something like this
git reset HEAD CONTRIBUTING.md
Unstaging a Staged File
git checkout -- CONTRIBUTING.md
Unmodifying a Modified File
git remote
see which remote servers you have configured
git remote -v
shows you the URLs that Git has stored for the shortname to be used when reading and writing to that remote
git remote add pb https://github.com/paulboone/ticgit
add a new remote Git repository as a shortname you can reference easily
git fetch pb
fetch all the information that Paul has but that you don’t yet have in your repository
git push origin master
When you have your project at a point that you want to share, you have to push it upstream
git remote show origin
see more information about a particular remote
git remote rename pb paul
change a remote’s shortname
git remote rm paul
remove a remote
git tag
Listing Your Tags
git tag -l "v1.8.5*"
search for tags with a particular pattern
git tag -a v1.4 -m "my version 1.4"
create an annotated tag
git show v1.4
see the tag data along with the commit that was tagged
git tag v1.4-lw
create a lightweight tag
git tag -a v1.2 9fceb02
tag commits after you’ve moved past them
git push origin v1.5
share tags
git push origin --tags
transfer all of your tags to the remote server that are not already there
git checkout -b version2 v2.0.0
create a new branch at a specific tag
git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.st status git config --global alias.unstage 'reset HEAD --' git config --global alias.last 'log -1 HEAD'
set up some aliases
git config --global alias.visual '!gitk'
set up alias for external command
git branch testing
create a new branch called testing. It didn’t switch to that branch
git checkout testing
switch to the new testing branch. This moves HEAD to point to the testing branch
git checkout -b iss53
create a branch and switch to it at the same time
git checkout master git merge hotfix
merge hotfix branch to master. This may result a fast forward, or create a commit snapshot that have two parents
git branch -d hotfix
delete a branch
git branch
get a simple listing of your current branches
git branch -v
see last commit in each branch
git branch --merged git branch --no-merged
filter this list to branches that you have or have not yet merged into the branch you’re currently on
git branch -D testing
Trying to delete unmerged banch would fail. Force it using this command
git push origin serverfix
If you have a branch named serverfix that you want to work on with others, you can push it up the same way you pushed your first branch
git config --global credential.helper cache
avoid entering password everytime pushing
git checkout -b serverfix origin/serverfix
If you want your own serverfix branch that you can work on, you can base it off your remote-tracking branch
git checkout -b serverfix origin/serverfix git checkout --track origin/serverfix git checkout serverfix
Those 3 commands above are synonyms. Branch serverfix set up to track remote branch serverfix from origin. Switched to a new branch 'serverfix'
git checkout -b sf origin/serverfix
Branch sf set up to track remote branch serverfix from origin. Switched to a new branch 'sf'
git branch -u origin/serverfix
Branch serverfix set up to track remote branch serverfix from origin. If you already have a local branch and want to set it to a remote branch you just pulled down, or want to change the upstream branch you’re tracking
git fetch --all
fetch from all remotes
git branch -vv
If you want to see what tracking branches you have set up
git pull
a git fetch followed immediately with git merge, in most cases
git push origin --delete serverfix
deleting a remote branch
git checkout experiment git rebase master
simplify history by moving branch experiment to the top of branch master
git rebase --onto master server client
check out the client branch, figure out the patches from the common ancestor of the client and server branches, and then replay them onto master
git rebase master server
rebase the server branch onto the master branch without having to check it out first
git pull --rebase
git merge followed immediately with rebase
git rebase -i --root
Commit squashing
git push —force origin BRANCH_NAME
Force push
git rebase --i HEAD~2
Merge the last two commits into one commit
get revert COMMIT_HASH
Remove changes made by a commit
git cherry-pick COMMIT_HASH
Add changes made by a commit to the current branch