Git
- Adding a remote
- View remote
- Removing remote
# initialize git in the path
git init
# clone a existing repo
git clone [email protected]:<github-username>/<repository-name>.git
# global config
git config --global user.name <github-username>
git config --global user.email <[email protected]>
git config --global color.ui true
Before going to terminal, create new repository in github website & note down the
<repository-name>
# steps
mkdir <new-git-project>
cd <new-git-project>
git init
touch README
git add README
git commit -m "first commit message"
git remote add origin [email protected]:<github-username>/<repository-name>.git
git push origin master
Navigate to your file location on terminal and add remote
cd <existing-git-repo>
git remote add origin [email protected]:<github-username>/<repository-name>.git
# generating SSH
cd `./ssh`
ssh-keygen -t rsa -C "[email protected]"
# copying SSH Key
pbcopy < ~/.ssh/id_rsa.pub
Stashing is the term git uses for saving your changes before taking pull.
# save / stash your changes
git stash save "<stash-name>"
# list of stashed changes
git stash list
# retrieve / put back stashed changes
git stash apply stash@{index} # where index is the position of stash shown in stash list
# delete stashed/saved changes
git stash drop "<stash-name>"
Note:
<stash-name>
is needed if you work on multiple saves otherwise you can skip and type git stash
to save and git stash apply
or git stash pop
to retrieve.# create a new branch
git checkout -b <branch-name>
git push origin <branch-name> --set-upstream
# show branches
git branch
# show local & remote branches
git branch -a
# get a remote branch
git fetch origin
git checkout --track origin/<branch-name>
# merge branch
git merge <branch-name>
# list merged branches
git branch -a --merged
# delete local remote-tracking branches
git remote prune origin
# delete local branch
git branch -D <branch-name>
# delete remote branch
git branch -r -d <branch-name>
# delete local & remote branch
git push origin --delete <branch-name>
Branches are good for developing features to your software, the below steps walk you through creating, rebasing and merging.
Creation
# from master branch create your feature branch
git checkout -b <feature-branch>
# checkout/navigate to your branch
git checkout <feature-branch>
# make changes, commit and push
git add -A
git commit -m "new feature updates"
git push origin <feature-branch>
Rebasing — Rebase to make sure we sync our feature branch with master
# navigate/checkout to master
git checkout master
# pull to get latest code update
git pull origin master
# navigate back & rebase
git checkout <feature-branch>
git rebase master
# incase of conflict, make changes and just add (don't commit)
git add -A
git rebase --continue
# once the rebase is completed, do force push
git push origin <feature-branch> --force-with-lease
Squash — all you branch commits to one commit with meaningful message
# Method 1
git reset origin/<branch-you-branched-from> # usually `git reset origin/master`
git add --all
git commit -m "new feature / bug detail"
git push origin <branch-name> --force-with-lease
# Method 2
git checkout master
git merge --squash <feature-branch> # at this point everything is merged, not committed
git add -A
git commit -m "Feature"
git push origin master
Merge your branch to master
# checkout/navigate to master branch
git checkout master
# merge your code to master. PS. using this will auto-close PR
git merge <feature-branch>
git push origin master
# delete your branch
git branch -D <feature-branch>
# delete last remote commit
git reset --hard HEAD~1
git push origin HEAD --force-with-lease
# delete local commits
git reset --hard HEAD~1
# rollback latest remote commit
git reset --hard HEAD^
# rollback to a specific commit
git reset --hard <commit-sha>
# amend last commit message
git commit --amend --author="<github-username>"
# amend last commit message
git commit --amend -m "New commit message"
# create a new tag
git tag -a v1.0.0 -m "my version 1.0.0"
# show tag details
git show <tagname>
# push tag to remote
git push origin <tagname>
# list tags
git tag
git tag -l "v1.8*" # show all tags starting with 1.8
# delete tag
git tag -d <tagname> #local
git push origin --delete <tagname> #remote
git reset --soft HEAD^ # Reset branch head to previous commit.
git stash # Stash the last commit.
git push -f origin <branch-name> # Force push to remote. The remote now doesn't have the last commit.
git stash pop # Pop your stash.
git commit -a # Commit cleanly.
git push origin <branch-name> # Push to remote.
# add new account for company
ssh-keygen -t rsa -C "[email protected]"
~/.ssh/id_rsa_company
# create config
touch ~/.ssh/config
vim config
# Config file content should have
# default account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
# company account
Host github-COMPANY
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_company
# Do it on github website - add ssh keys to github account
# add origin for projects
git remote add origin git@github-COMPANY:<github-username>/<repository-name>.git
Adding a remote
To add a new remote, navigate to project path in terminal.
The
git remote add
command takes two arguments:- A remote name, for example,
origin
- A remote URL, for example,
https://github.com/user/repo.git
# Set a new remote
git remote add origin https://github.com/user/repo.git
# Push to repo
git push -u origin master
View remote
git remote -v
# returns existing remote
origin https://github.com/user/repo.git (fetch)
origin https://github.com/user/repo.git (push)
Remove remote
git remote remove origin
# or
git remote rm origin
A common mistake is cloning using the default (HTTPS) instead of SSH. You can correct this by going to your repository, clicking the ssh button left to the URL field and updating the URL of your origin remote like this:
git remote set-url origin [email protected]:username/repo.git
Navigate to the old repo path eg.
cd /works/old-repo/
# remove old remote
git remote rm origin
# make sure remote is empty
git remote -v
# add new repo remote
git remote add origin [email protected]:<github-username>/<new-repo-name>.git
# push your changes to new repo with commits
git push -u origin --all
# add origin for projects
git remote add origin git@github-COMPANY:<github-username>/<repository-name>.git
Last modified 6mo ago