- Changing the last commit message
git commit --amend -m "New commit message"
- Ignoring at commit the modifications of an already tracked file:
git update-index --assume-unchanged file
- Reverting the effects of the git update-index --assume-unchanged command:
git update-index --no-assume-unchanged file
- Creating a branch, checking it out and setting to track a remote repository/branch:
git branch local-branch
git checkout local-branch
git pull remote-repository remote-branch
git branch --set-upstream local-branch remote-repository/remote-tracked-branch
- Check out a remote branch, track it and switch to it (in the same time):
git checkout -b local-branch remote-repository/remote-branch
git pull --all
- Check out a remote branch, track it and switch to it (in the same time) for unaware repository:
When updating paths is incompatible with switching branches error occurs you are trying to checkout a
remote branch that your local git repo is not aware of yet.
git remote show origin
If the remote branch you want to checkout is under "New remote branches" and not "Tracked remote branches" then you need to fetch them first:
git remote update
git fetch
git checkout -b local-branch remote-repository/remote-branch
Alternate syntax:
git fetch remote-repository remote-branch:local-branch
- Pushing a new local branch to remote:
git push -u remote-repository local-branch
git push -u remote-repository local-branch:remote-branch
- Reseting a local branch to remote HEAD:
git checkout local-branch
git fetch remote-repository
git reset remote-repository/remote-branch
git reset --hard remote-repository/remote-branch
- Setting tracked branch for pull
By default git push updates all the remote branches. But you can configure git to update only the current branch.
git config push.default current
It means git will update only the current (checked out) branch when you do git push.
Other valid options are:
nothing : Do not push anything
matching : Push all matching branches (default)
upstream: Push the current branch to its upstream branch. (the branch git pull would pull from)
tracking : Deprecated, use upstream instead.
current : Push the current branch to the remote branch of the same name.
Since git version 1.7.11, A new mode, "simple", which is a cross between "current" and "upstream", has been introduced. "git push" without any refspec will push the current branch to the remote branch with the same name, only when it is set to track the remote branch. This mode is the new default value when push.default is not configured.
- Deleting a remote branch:
git push origin :remote-branch
- Get the name of the current branch:
git rev-parse --abbrev-ref HEAD
- Get the remote tracking branch of the current branch:
git rev-parse --symbolic-full-name --abbrev-ref @{u}
- Creating and pushing tags to a remote repository
a. Simple tag
git tag tagname
b. Annotated tag
git tag -a tagname -m 'Tag message'
c. Pushing all tags to a remote repository
git push --tags remote-repository
d. Pushing a specific tag to a remote repository
git push remote-repository tag tagname
- Listing all the tags and the first line of the annotation
git tag -l -n1
- Delete local and remote tag
git tag -d tagname
git push origin :refs/tags/tagname
Note: Alternatively you can configure the remote you push to to always push all tags, e.g. put something like that in your .git/config:
[remote "publish"] # or whatever it is named
url = ...
push = +refs/heads/*:refs/heads/*
push = +refs/tags/*:refs/tags/*
This means force push all heads (all branches) and all tags (if you don't want force pushing of heads, remove '+' prefix from refspec.
[remote "publish"] # or whatever it is named
url = ...
push = :
push = +refs/tags/*:refs/tags/*
Meaning it pushes every branch that's already there, plus tags. It does not force push, and it does not push branch that you didn't push manually
And if you want to force fetch all the tags, you may set it in the config by:
git config remote.origin.tagopt --tags
From the docs:
Setting this value to --no-tags disables automatic tag following when fetching from remote . Setting
it to --tags will fetch every tag from remote , even if they are not reachable from remote branch
heads. Passing these flags directly to git-fetch(1) can override this setting. See options --tags
and --no-tags of git-fetch(1).
- Stash your current changes:
git stash save
- List current stashes:
git stash list
- Apply a stash:
git stash apply stash-id
git stash apply stash@{0}
stash@{0} refers to the last stash you made.
- Drop a stash:
git stash drop stash@{0}
- Pop off the last stash you saved:
git stash pop
- Clear all the existing stashes:
git stash clear
- Quickly stash your changes to restore them later:
git stash
...
git stash pop
- Cleaning up repository:
git gc
- Repository checking:
git fsck [--full]
- Recovering lost/dangling objects:
git fsck --lost-found
- Checking out the contents of the lost/dangling object:
git checkout dangling-object-id
- Cleaning up dangling objects:
git fsck --all
git reflog expire --expire=now --all
git gc --prune=now
- Pretty printing aliases for log (with commit graph):
[alias]
lg1 = log --graph --all --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(bold white)— %an%C(reset)%C(bold yellow)%d%C(reset)' --abbrev-commit --date=relative
lg2 = log --graph --all --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(bold white)— %an%C(reset)' --abbrev-commit
lg = !"git lg1"
- Turn on auto-rebase:
git config branch.autosetuprebase always
- Enable rebase for a branch:
git config branch.local-branch.rebase true
- Set rebase to list updated files when pulling:
git config rebase.stat true
- Adding aliases to git config:
git config alias.alias-name command
Note: Adding the --global parameter to the config command line changes to the settings will be done in the global namespace instead of the local (repository) namespace. The global namespace can be found in ~/.gitconfig file. The global file ignore list can set by creating and modifying the ~/.gitignore file.
Sources:
- http://stackoverflow.com/questions/2988088/do-git-tags-get-pushed-as-well
- http://stackoverflow.com/questions/5195859/how-can-i-push-a-tag-to-a-remote-repository-using-git
- http://gitready.com/beginner/2009/02/02/push-and-delete-branches.html
- http://stackoverflow.com/questions/945654/git-checkout-on-a-remote-branch-does-not-work
- http://stackoverflow.com/questions/1057564/pretty-git-branch-graphs
- http://stackoverflow.com/questions/6245570/get-current-branch-name
- http://stackoverflow.com/questions/2553786/how-do-i-alias-commands-in-git
- http://stackoverflow.com/questions/3354482/with-git-temporary-exclude-a-changed-tracked-file-from-commit-in-command-line
- http://www.tekkie.ro/news/howto-remove-all-dangling-commits-from-your-git-repository/
- http://osdir.com/ml/git/2009-04/msg01874.html
- https://git.wiki.kernel.org/index.php/Aliases