Using ssh to access repository
- Create a key pair
- Copy/paste the public key content to github.com
- Run the following commands before clone source code
eval $(ssh-agent)
ssh-add ~/.ssh/private_key
- Clone source code
Create
- Create new local repository
- Clone an existing repository
- gitdone ssh://user@domain.com/repo.git
Local Changes
- Changed files in your working dir
- Changes to tracked file
- Add all current changes to next commit
- Add some changes in <file> to next commit
- Commit all local changes in tracked file
- Commit previous staged changes
- Change the last commit
Commit history
- Show all commit, starting with newest
- Show changes over time for a specific file
- Who change what and when in <file>
Branches and Tags
- List all existing braches
- Switch HEAD branch
- Create a new branch based on current HEAD
- Create a new tracking branch based on a remote branch
- git checkout --track <remote/branch>
- Delete a local branch
- Mark the current commit with a tag
Update & Publish
- List all currently configured remotes
- Show info about remote
- Add new remote repository, named <remote>
- git remote add <short name> <url>
- Download all changes from <remote>, but don't integrate into HEAD
- Download changes and directly merge/integrate into HEAD
- git pull <remote> <branch>
- Publish local changes on a remote
- git push <remote> <branch>
- Delete a branch on the remote
- git branch -dr <remote/branch>
- Publish your tags
Merge and rebase
- Merge branch into current HEAD
- Rebase your current HEAD onto <branch>
- Abort a rebase
- Continue a rebase after resolving conflicts
- Use your configured merge tool to resolve conflicts
- Use your editor to manually resolve conflicts and (alter resolving) mark file as resolved
- git add <resolved-file>
- git rm <resolved-file>
Undo
- Discard all local changes in your working directory
- Discard local changes in specific file
- Revert a commit (by producing a new commit with contrary changes)
- Reset your HEAD pointer to a previous commit, ... and discard all changes since then
- git reset --hard <commit>
- ... and preserve all changes as untagged changes
- ... and preserve uncommited local changes
- git reset --keep <commit>
Add an existing project to Git project
1. Create new repository
2. Open terminal and change the current working directory to your local project
3. Initialize the local directory as a Git repository
$git init
4. Add the files in your new local repository
5. Commit the files that you've staged
$git commit -m "First commit"
6. Copy remote repository URL
7. Add the URL for remote repository
$git remote add origin <remote_url>
8. Download all change from <remote>, but don't integrate to HEAD
$git fetch origin master:tmp
9. Rebase your current HEAD onto branch
10. Publish local change
$git push origin HEAD:master
11. Delete branch tmp
$git branch -D tmp
Switch to branch
1. View all remote
$git remote -v
2. Download branches
2.1 View all branches
$git branch -r
2.2.1 Download all branches
$git fetch --all
2.2.2 Download a branch
$git fetch <remote> <branch_name>
3. Switch to branch
$git checkout -t <remote>/<branch_name>
Move commits to another branch
Branch A: a1--a2--a3 (2e11c5f) --a4 (2681270) --a5 (08b0f0e)
Branch A: b1--b2--b3--b4--b5
Move commits a3 and a4 to the branch B:
1. Check out the banch A
$git checkout A
2. Checkout the branch B
$git checkout B
3. Move the commit a3, a4 to branch B
$git cherry-pick 2e11c5f
$git cherry-pick 2681270
4. Reset branch A to last commit
$git checkout A
$git reset --hard 08b0f0e
Override tag
Use the -f option to git tag:
-f --force
Replace an existing tag with the given name (instead of failing)
You probably want to use -f in conjunction with -a to force-create an annotated tag instead of a non-annotated one.
Example
Delete the tag on any remote before you push
$git push origin :refs/tags/<tagname>
Replace the tag to reference the most recent commit
$git tag -fa <tagname>
Push the tag to the remote origin
$git push origin master --tags
Checkout pull request
pull request example: want to merge into <organization>:<branch> from <user>:<branch>
You want to checkout from <user>:<branch> but you don't have permission to access <user>:<branch>
1. checkout <organization>:<branch>
2. checkout pull request to a new branch
$git fetch origin pull/$ID/head:<new_branch_name>
3. checkout <new_branch_name>
$git checkout <new_branch_name>
When <user> push new commits to pull request, you want to pull these -> do again from step 1.