View history [source]
- Simple log:
git log
- Last two commits:
git log -2
- Last two commits with changes:
git log -p -2
- Commit list with hash:
git log --pretty=oneline
- Commit ancestry graph by branches:
git show-branch
Committing changes
What has been changed?
- unstaged files:
diff file
- staged files:
diff --staged file
Add only parts of file to stage
- Interactive patching of file [source]
git add --patch file
- When using the above command, the hulks are somethimes too big and the edit hulk option too difficult to handle. I therefore prefer git gui [source]:
sudo apt-get install git-gui
just run this command before staging changes:git gui
Moving untracked changes to stash
When you want to apply a merge or update your repository and you get the following message: „error: Your local changes to the following files would be overwritten [..] Please, commit your changes or stash them before you can merge.“ This is how to use stash:
git stash
git stash list
git stash apply
Undo changes, remove changes, reset files..
Remove file from stage but keep changes [source]
git reset HEAD file
Reset all changes since last commit
git reset --hard HEAD
Reset all changes since second last commit
git reset --hard HEAD~1
Restore added changes removed with the above command [source]
git reset --hard HEAD@{1}
.gitignore
Ignore files only locally without using .gitignore [source]
To ignore files without adding them to a possibly syncronised .gitignore file, add the files to info/exclude.
Add tracked files to .gitignore [source]
git rm --cached path/file
echo 'path/file' >> .gitignore
Modify commits
Split previous commit [source]
git rebase -i HEAD~3
..where 3 is the number of commits you have to go back for the commit you want to split. The number can also be higher. You get into an edit mode where you see the last commits with the prefix „pick“. Change that to „edit“ for the commit you want to split.git reset HEAD~
- Commit multiple separate changes
git rebase --continue
Add changes to older commit [source]
git add my_fixed_files
git commit --fixup=OLDCOMMIT
git rebase --interactive --autosquash OLDCOMMIT^
Branches / forks
Renaming branch [source]
git branch -m old_branch new_branch
git push origin :old_branch
git push --set-upstream origin new_branch
Adding remote information to fork (before syncing) [source]
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
- Check with
git remote -v
Syncing a fork [source]
git fetch upstream
git checkout master
git merge upstream/master
git push
Working with remote branch of coworker [source]
- Pull branch:
git remote add coworker git://path/to/coworkers/repo.git
git fetch coworker
git checkout --track coworker/foo - Update branch:
git checkout foo
git pull
Creating branch with current staged changes [source]
- Unstage added changes:
git reset --soft HEAD~1
- Create new branch
git checkout -b newBranch
Deleting branches [source]
- Local branches:
git branch -d the_local_branch
- Remote branches:
git push origin :the_remote_branch