Repositories
List remote repositories
1 |
git remote -v |
Add new remote repository
1 |
git remote add <REMOTE_REPO_NAME> https://github.com/<OWNER>/<REPOSITORY>.git |
In case of fork, it’s useful to add the original repo as a remote repo named ‘upstream’
1 |
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git |
Sync fork repo with original repo
First, fetch original repo
1 |
git fetch upstream |
Then, switch to fork/branch to sync with original branch (eg. master)
1 |
git checkout master |
Merge original/branch with fork/branch
1 |
git merge upstream/master |
Finally, push synced fork/branch in remote fork/repo (eg. master)
1 |
git push origin master |
Branches
List Branches
only local branches
1 |
git branch |
both remote-tracking and local branches
1 |
git branch -a |
Create new branch and switch to new Branch
Command
1 |
git checkout -b <branch_name> |
Clone a specific Branch
Command
1 2 |
git branch -a git checkout <name_of_branch> |
Create local Branch linked to remote Branch
Command
1 2 |
git branch -a git checkout -b <branch-name> <origin/branch_name> |
example
1 2 3 4 5 6 7 |
git branch -a * master origin/HEAD origin/enum-account-number origin/master origin/rel_table_play origin/sugarfield_customer_number_show_c |
1 |
git checkout -b enum-account-number origin/enum-account-number |
After you hit return the following happens:
1 2 |
Branch enum-account-number set up to track remote branch refs/remotes/origin/enum-account-number. Switched to a new branch "enum-account-number |
Switch Branch
Command
1 |
git checkout <branch_name> |
Remove Branch
remove local branch
1 |
git branch -d <branch_name> |
remove remote branch once removed local branch
1 |
git push origin :<branch_name> |
Push changes to Branch
Command
1 |
git push origin <branch_name> |
Merge Branches
swithc to destination branch
than commit selected branch into current branch
–no-ff option generate a merge commit with a message
1 |
git merge --no-ff <branch_name> |
Tags
List Tags
Command
1 |
git tag |
Create Tag
Command
1 |
git tag <tag_name> <commit_id> |
commit id be retrieved via:
1 |
git log |
example
1 |
git tag 0.1 1b2e1d63ff |
Push Tag
Push all tags
1 |
git push --tags |
Push a single tag
1 |
git push origin <tag_name> |
Undo
Discharge changes on file or restore deleted file
1 2 |
git reset HEAD <file> git checkout <file> |
Undo a commit
1 |
git revert de4bbc49eab |