Official Git site
Setup a repository
New repository
Create a new repository:
git init
Add a remote (origin) (example for Microsoft VSTS):
git remote add origin https://{account}.visualstudio.com/{Team project collection eg. DefaultCollection}/_git/{name repository}
Convert a Subversion repository to Git
Clone a repository
git clone https://{account}.visualstudio.com/{Team project collection eg. DefaultCollection}/_git/{name repository}
Daily use
General
git status : shows the current status of the local repository.
git checkout: checkout a version
Pushing changes to a (remote) repository
git add hello.html : stage/index the file hello.html
git commit -m "First Commit" : commit the staged files to the (local) repository with the message "First commit".
git push: Copies all the changes committed to the local repository, to the remote repository
Push till a certain local commit:
git push origin 2dc2b7e393e6b712ef103eaac81050b9693395a4:master
Getting changes from a (remote) repository
Fetch gets the changes from the remote branch and copies them to your local repository, but doesn't merge them to your working copy.
Pull will merge remote branch change to local branch. So actually this is fetch + pull.
Undo local commits and go back to origin/master
git reset --hard origin/master
--soft: will undo the local commit, but leave the changes in the stage/index
--mixed: will undo the local commit, but leave the changes in the working directory
--hard: will undo the local commit and completely remove the local changes
Full explanation about git reset:
https://stackoverflow.com/questions/3528245/whats-the-difference-between-git-reset-mixed-soft-and-hard/3528483#3528483
Revert a commit already pushed to a remote repository
git push origin -f
http://christoph.ruegg.name/blog/git-howto-revert-a-commit-already-pushed-to-a-remote-reposit.html
Tags
https://git-scm.com/book/en/v2/Git-Basics-Tagging
Delete tag:
git tag -d tagname
Push tag to a remote:
git push origin <tag_name>
git push --follow-tags (will push all tags that are annotated and reachable (an ancestor) from the pushed commits)
Keep a clean history
Always pull before you commit.
If you forget and you have a conflict when you commit:
1. Revert the local commit: git reset —mixed origin/master
2. Stash your changes
3. Pull
4. Merge your stash
Change your commit message
https://gist.github.com/nepsilon/156387acf9e1e72d48fa35c4fabef0b4
Branches
Show branches
git branch : local branches
git branch -a : local and remote branches
Switch to a branch
git checkout branchname // Mind that branchnames are case sensitive!
Create branch
git branch branchname
Delete branch
git branch -d branchname
On remote:
git push [name remote] --delete [name branch]
Rebase
https://www.reddit.com/r/git/comments/6jzogp/why_am_i_force_pushing_after_a_rebase/
After rebase, push to the remote with
git push --force-with-lease
Move commits to another branch
git checkout branchToMoveCommitTo
git merge branchToMoveCommitFrom
git checkout branchToMoveCommitFrom
git reset --hard HEAD~3 # Go back 3 commits. You *will* lose uncommitted work.
git checkout branchToMoveCommitTo
Remotes
git remote -v : list all remotes
git remote add origin https://{account}.visualstudio.com/DefaultCollection/_git/{name repository}
Remark: The default name of a remote repository is origin.
Other commands
Show Git version
git --version
Configuration
git config —list : shows the Git configuration settings in a list
Get / set a configuration setting:
git config —global/local user.name "Your Name"
git config —global/local user.email "moc.revetahw|liame_ruoy#moc.revetahw|liame_ruoy"
Subversion
Copying changes to a SVN repository: SVN DCommit
Getting changes from a SVN repository: SVN rebase
Git clients
Overview of Windows clients
- Git GUI (comes default with GIT)
- TortoiseGit
- Smart Git
- Source Tree
- GitKraken
- Visual Studio
Git workflows
GitHub Flow
Too simple. Just check pull request.
GitFlow
GitLab Flow
Version control hosting services
See Subversion