Difference between revisions of "Git"
(One intermediate revision by the same user not shown) | |||
Line 7: | Line 7: | ||
https://wac-cdn.atlassian.com/dam/jcr:d60ca565-89db-4f07-98a0-dd35fae97293/git1.jpg?cdnVersion=945 | https://wac-cdn.atlassian.com/dam/jcr:d60ca565-89db-4f07-98a0-dd35fae97293/git1.jpg?cdnVersion=945 | ||
+ | |||
+ | =Install= | ||
+ | '''GitHub for Windows''' | ||
+ | https://windows.github.com | ||
+ | |||
+ | '''GitHub for Mac''' | ||
+ | https://mac.github.com | ||
+ | |||
+ | '''Git for All Platforms''' | ||
+ | http://git-scm.com | ||
+ | |||
+ | Git distributions for Linux and POSIX systems are available on | ||
+ | the official Git SCM web site. | ||
+ | |||
+ | =Configure tooling= | ||
+ | Configure user information for all local repositories | ||
+ | <source> | ||
+ | $ git config --global user.name "[name]" | ||
+ | # Sets the name you want attached to your commit transactions | ||
+ | |||
+ | $ git config --global user.email "[email address]" | ||
+ | # Sets the email you want attached to your commit transactions | ||
+ | |||
+ | $ git config --global color.ui auto | ||
+ | # Enables helpful colorization of command line output | ||
+ | </source> | ||
+ | |||
+ | =Branches= | ||
+ | Branches are an important part of working with Git. Any | ||
+ | commits you make will be made on the branch you're currently | ||
+ | “checked out” to. Use git status to see which branch that is | ||
+ | |||
+ | <source> | ||
+ | $ git branch [branch-name] | ||
+ | # Creates a new branch | ||
+ | |||
+ | $ git checkout [branch-name] | ||
+ | # Switches to the specified branch and updates the working directory | ||
+ | |||
+ | $ git merge [branch] | ||
+ | # Combines the specified branch’s history into the current branch. This is usually done in pull requests, but is an important Git operation. | ||
+ | |||
+ | $ git branch -d [branch-name] | ||
+ | # Deletes the specified branch | ||
+ | </source> | ||
+ | |||
+ | |||
+ | =Create repositories= | ||
+ | When starting out with a new repository, you only need to do itonce; either locally, then push to GitHub, or by cloning an existing repository | ||
+ | |||
+ | |||
+ | <source> | ||
+ | $ git init | ||
+ | # Turn an existing directory into a git repository | ||
+ | |||
+ | $ git clone [url] | ||
+ | # Clone (download) a repository that already exists on GitHub, including all of the files, branches, and commits | ||
+ | </source> | ||
+ | |||
+ | =The .gitgnore file= | ||
+ | Sometimes it may be a good idea to exclude files from being tracked with Git. This is typically done in a special file named .gitignore . You can find helpful templates for .gitignore files at github.com/github/gitignore. | ||
+ | |||
+ | =Synchronize changes= | ||
+ | Synchronize your local repository with the remote repository on GitHub.com | ||
+ | |||
+ | <source> | ||
+ | $ git fetch | ||
+ | # Downloads all history from the remote tracking branches | ||
+ | |||
+ | $ git merge | ||
+ | # Combines remote tracking branch into current local branch | ||
+ | |||
+ | $ git push | ||
+ | # Uploads all local branch commits to GitHub | ||
+ | |||
+ | $ git pull | ||
+ | # Updates your current local working branch with all new commits from the corresponding remote branch on GitHub. git pull is a combination of git fetch and git merge | ||
+ | </source> | ||
+ | |||
+ | |||
+ | =Make changes= | ||
+ | Browse and inspect the evolution of project files | ||
+ | |||
+ | <source> | ||
+ | $ git log | ||
+ | # Lists version history for the current branch | ||
+ | |||
+ | $ git log --follow [file] | ||
+ | # Lists version history for a file, including renames | ||
+ | |||
+ | $ git diff [first-branch]...[second-branch] | ||
+ | # Shows content differences between two branches | ||
+ | |||
+ | $ git show [commit] | ||
+ | # Outputs metadata and content changes of the specified commit | ||
+ | |||
+ | $ git add [file] | ||
+ | # Snapshots the file in preparation for versioning | ||
+ | |||
+ | $ git commit -m "[descriptive message]" | ||
+ | # Records file snapshots permanently in version history | ||
+ | </source> | ||
+ | |||
+ | =Redo commits= | ||
+ | Erase mistakes and craft replacement history | ||
+ | |||
+ | <source> | ||
+ | $ git reset [commit] | ||
+ | # Undoes all commits after [commit], preserving changes locally | ||
+ | |||
+ | $ git reset --hard [commit] | ||
+ | # Discards all history and changes back to the specified commit | ||
+ | </source> | ||
+ | |||
+ | CAUTION! Changing history can have nasty side effects. If you need to change commits that exist on GitHub (the remote), proceed with caution. If you need help, reach out at github.community or contact support. | ||
+ | |||
+ | =Backup/restore branch= | ||
+ | <source> | ||
+ | $ git stash [commit] | ||
+ | # backup changes locally | ||
+ | |||
+ | $ git checkout stash@{0} -- <filename> | ||
+ | # to unstash only certain files | ||
+ | |||
+ | # to save it under another filename: | ||
+ | $ git show stash@{0}:<full filename> > <newfile> | ||
+ | |||
+ | # If you want to select manually which changes you want to apply from that file: | ||
+ | $ git difftool stash@{0}..HEAD -- <filename> | ||
+ | |||
+ | $ git diff stash@{0}^1 stash@{0} -- <filename> | git apply | ||
+ | $ git diff stash@{0}^1 stash@{0} -- <filename> | ||
+ | |||
+ | # unstash | ||
+ | $ git stash pop | ||
+ | |||
+ | </source> | ||
+ | |||
=Others= | =Others= |
Latest revision as of 09:09, 10 April 2020
official website: https://git-scm.com/
Quick Reference Book / Basic Git Commands:
https://github.github.com/training-kit/downloads/github-git-cheat-sheet.pdf
https://wac-cdn.atlassian.com/dam/jcr:d60ca565-89db-4f07-98a0-dd35fae97293/git1.jpg?cdnVersion=945
Contents
Install
GitHub for Windows https://windows.github.com
GitHub for Mac https://mac.github.com
Git for All Platforms http://git-scm.com
Git distributions for Linux and POSIX systems are available on the official Git SCM web site.
Configure tooling
Configure user information for all local repositories
$ git config --global user.name "[name]"
# Sets the name you want attached to your commit transactions
$ git config --global user.email "[email address]"
# Sets the email you want attached to your commit transactions
$ git config --global color.ui auto
# Enables helpful colorization of command line output
Branches
Branches are an important part of working with Git. Any commits you make will be made on the branch you're currently “checked out” to. Use git status to see which branch that is
$ git branch [branch-name]
# Creates a new branch
$ git checkout [branch-name]
# Switches to the specified branch and updates the working directory
$ git merge [branch]
# Combines the specified branch’s history into the current branch. This is usually done in pull requests, but is an important Git operation.
$ git branch -d [branch-name]
# Deletes the specified branch
Create repositories
When starting out with a new repository, you only need to do itonce; either locally, then push to GitHub, or by cloning an existing repository
$ git init
# Turn an existing directory into a git repository
$ git clone [url]
# Clone (download) a repository that already exists on GitHub, including all of the files, branches, and commits
The .gitgnore file
Sometimes it may be a good idea to exclude files from being tracked with Git. This is typically done in a special file named .gitignore . You can find helpful templates for .gitignore files at github.com/github/gitignore.
Synchronize changes
Synchronize your local repository with the remote repository on GitHub.com
$ git fetch
# Downloads all history from the remote tracking branches
$ git merge
# Combines remote tracking branch into current local branch
$ git push
# Uploads all local branch commits to GitHub
$ git pull
# Updates your current local working branch with all new commits from the corresponding remote branch on GitHub. git pull is a combination of git fetch and git merge
Make changes
Browse and inspect the evolution of project files
$ git log
# Lists version history for the current branch
$ git log --follow [file]
# Lists version history for a file, including renames
$ git diff [first-branch]...[second-branch]
# Shows content differences between two branches
$ git show [commit]
# Outputs metadata and content changes of the specified commit
$ git add [file]
# Snapshots the file in preparation for versioning
$ git commit -m "[descriptive message]"
# Records file snapshots permanently in version history
Redo commits
Erase mistakes and craft replacement history
$ git reset [commit]
# Undoes all commits after [commit], preserving changes locally
$ git reset --hard [commit]
# Discards all history and changes back to the specified commit
CAUTION! Changing history can have nasty side effects. If you need to change commits that exist on GitHub (the remote), proceed with caution. If you need help, reach out at github.community or contact support.
Backup/restore branch
$ git stash [commit]
# backup changes locally
$ git checkout stash@{0} -- <filename>
# to unstash only certain files
# to save it under another filename:
$ git show stash@{0}:<full filename> > <newfile>
# If you want to select manually which changes you want to apply from that file:
$ git difftool stash@{0}..HEAD -- <filename>
$ git diff stash@{0}^1 stash@{0} -- <filename> | git apply
$ git diff stash@{0}^1 stash@{0} -- <filename>
# unstash
$ git stash pop