My Git Notes
- Typical git commands
- Work with branches
- Work with Git Remote URL (URI)
- Force git to overwrite local files on pull
- Tag version
- Set commit messages in modal text editor, like Vim
- Fork & Merge changes from remote github repository to your local repository
- Rollback a git repository to a specific commit
- How to undo a local/public commit
- Clean up a fork and restart it from the upstream
- Submodule git commands
- Helpful .gitconfig Configurations
- Practice-Git Links
Typical git commands
git clone git@github.com:<user_name>/the-repo-you-are-cloning.git
- Clones your remote origin repo locally
git fetch upstream
- Pulls in the remote changes not present in your local repo. Downloads objects and references from another repository.
git merge upstream/master
- Merges any changes fetched into your working files
git add <file>
- Start tracking new files and also stage changes to already tracked files
git status
and git diff
- Tells us what files and assets have been modified and staged
git status -s
- This will display what files have been removed, changed or modified.
- (M) - modified
- (A) - added
- (AM) - file has not been altered since it was last added
git commit -m 'the message goes here for the commit'
- Records a snapshot of the project into your history at the time of your commit.
git add '*.<file_extension>'
- This command adds all file types with the same extension, especially from different directories. Without quotes the command will only execute within the same directory it's been called from.
git rm --cached <file>
- Unstages a file from the working tree (i.e. stops tracking the file).
git log
- Remembers all the changes we've committed so far, in the order we committed them.
git log --summary
- See where new files were added for the first time or where files were deleted.
git reset <file>
- Removes the desired file from staging area.
git rebase
- Rebase allows you to easily change a series of commits, reordering, editing, or squashing commits together into a single commit.
- Be warned: it's considered bad practice to rebase commits which you have already pushed to a remote repo. Doing so may invoke the wrath of the git gods. https://help.github.com/articles/interactive-rebase
Work with branches
git branch -r
- List all the remote branches currently tracked
git branch -m <old-name> <new-name>
- Rename local branch
git checkout -b <new-branch-name>
- Create local branch
git push <remote-name> <new-branch-name>
- Push new branch to remote server
git remote <name> origin
- Deletes branch locally if it has been removed remotely. Helps to remove stale references.
git push origin --delete <branch-name>
- Delete a old Git branch and push new name, both locally and remotely
git push origin :<old-name> <new-name>
- Rename a Git branch locally and rempotly
git push origin :<old-name> <new-name>
- Reset the upstream branch for the new-name local branch
git checkout <target>
- Changes the desired target back to the state of the last commit. A target can be a file or a directory (for example).
Work with Git Remote URL (URI)
git remote add origin git@github.com:<username>/<reponame>.git
- Creates a brand new remote repository.
git remote set-url origin git@github.com:<username>/<reponame>.git
- Change the URI (URL) for a remote Git repository.
git remote -v
- Show a list of the current remote repositories
Force git to overwrite local files on pull
git fetch --all
<br>git reset --hard origin/master
- Force git to overwrite local files on pull
1. downloads the latest from remote without trying to merge or rebase anything.
2. resets the master branch to what you just fetched
Tag version
Create tag
git tag
List existing tagsgit tag -a v1.0 -m 'my version 1.0'
Annotated Tags with descriptiongit push --tags
Push to repository
Delete tag, also remote
git tag -d v1.0
Delete Tag locallygit push origin :refs/tags/v1.0
Delete Tag on remote repo
Set commit messages in modal text editor, like Vim
- Press
i
to enter insert mode. - Now you can type your message, as if you were in a normal (non-modal) text editor.
- Press
esc
to go back to command mode. - Then type
:w
followed byenter
to save. - Finally
:q
followed byenter
to quit.
Fork & Merge changes from remote Github repository to your local repository
- Fork the Project repo, click the "Fork" button
- Clone your fork
git clone <Your Public Clone URL>
- Configure remotes
cd <Local-Repository>
git remote add <remote-name> <Public Clone URL>
git fetch <remote-name>
- Merge changes from remote Github repository to your local repository
Fork
git remote add <name> <Public Clone URL>
git pull <name> master
git push
Example
- Fork via button
$ git clone git@github.com:bueltge/wp-butler.git
cd wp-butler
git remote add upstream git://github.com/Japh/wp-butler.git
git fetch upstream
git pull upstream master
git push
Rollback a git repository to a specific commit
git reset --hard <old-commit-id>>
git push -f <remote-name> <branch-name>
How to undo a local/public commit
git revert HEAD
- Undo the last public commit
git reset --soft HEAD^
- use
--soft
if you want to keep your changes - use
--hard
if you don't care about keeping the changes you made
More, check out (StackOverflow 927358)[http://stackoverflow.com/questions/927358/undo-the-last-git-commit]
Clean up a fork and restart it from the upstream
Using 'upstream' as the remote name referencing the original repo forked
git remote add upstream /url/to/original/repo
git fetch upstream
git checkout master
git reset --hard upstream/master
git push origin master --force
More, check out (StackOverflow 9646167)[http://stackoverflow.com/questions/9646167/clean-up-a-fork-and-restart-it-from-the-upstream]
Submodule git commands
git submodule init
- Get the submodule initially
git submodule update --recursive
- Easy way pull latest of all submodules in a repo; do this from the base of the repo
Helpful .gitconfig Configurations
- My short GitAlias
[alias]
a = !"git alias"
c = commit
s = status
p = pull
w = whatchanged
rb = rebase
# Rebase interactive
rbi = !"git rebase -i"
# Rebase interactive on our unpushed commits
rbiu = !"git rebase -i @{u}"
- Setup alias for convenience.
git alias
to list all alias from .gitconfi
g<br>alias = !git config --list | grep 'alias\\.' | sed 's/alias\\.\\([^=]\\)=\\(.\\)/\\1\\\t => \\2/' | sort
git changelog
to create changelog from git commit
s<br>git config --global alias.changelog = "!() { t=$(git describe --abbrev=0 --tags); git log ${t}..HEAD --no-merges --pretty=format:'* %s'; }; "
git log
to view commit log like network grap
h<br>git config --global alias.lg "log --all --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ci) %C(bold blue)<%an>%Creset' --abbrev-commit"
Practice-Git Links
- Git Magic — www-cs-students.stanford.edu/~blynn/gitmagic/intl/de/
- Learn Git in minutes — https://github.com/adambard/learnxinyminutes-docs/blob/master/git.html.markdown
- Git Commands — https://github.com/pascalmaddin/Git-Commands
- Git Reference — http://gitref.org/basic
- Pro Git Online Book — http://git-scm.com/book
- Quick Command Practice — http://try.github.com
- How to GitHub: Fork, Branch, Track, Squash and Pull Request — http://gun.io/blog/how-to-github-fork-branch-and-pull-request
- Learn Git Online — http://learn.github.com/p/intro.html
- Git: The Simple Guide — http://rogerdudler.github.com/git-guide
- Git Immersion — http://gitimmersion.com
- Git Submodule Tutorial — http://pkp.sfu.ca/wiki/index.php/Gitsub-moduletutorial
- How to handle conflicts with git — https://github.com/Kunena/Kunena-2.0/wiki/How-to-handle-conflicts-with-git
- A simple git branching model with useful configuration hints — https://gist.github.com/jbenet/ee6c9ac48068889b0912
- Git alias commands for faster easier version control — https://github.com/GitAlias/gitalias