Create Local Repository
- Download GitHub for Windows or use something similar, which includes a shell
- Launch Git Shell
- In the shell, navigate to the directory with
cd
-
git init
to create a local repository
Alternatively, we can clone from a remote repository: git clone https://user@bitbucket.org/team-or-user-name/myproject.git folder_name
Add All Files and Directories to Git
- After
git init
, do git add .
- List the added files which are not yet committed
git diff --cached --name-only --diff-filter=A
git commit -m 'message'
Show status of the upstream repository & disconnect
If the local repo has code updated from a repo from a git pull
or git clone
, we can rename the repo as upstream and add our own remote repo as origin.
git remote -v
to get the remote path for fetch and push remote. If your local repository is connected to a remote, it will show something like this:
origin https://user@bitbucket.org/team-or-user-name/myproject.git (fetch)
origin https://user@bitbucket.org/team-or-user-name/myproject.git (push)
If it's not connected, it'll show origin
only.
If we do not need the upstream remote, it can be removed from local repository by using git remote rm origin
.
If we need it for future code update, we can add a new upstream remote and reset the origin to our remote repository:
Note: In case we are using cloud service like bitbucket, we need to create a project on bitbucket first. After creation, bitbucket will display all required git commands to push your repository to remote, which look similar to the next code snippet. However, this works for other repositories, too.
$ git remote add upstream https://user@bitbucket.org/team-or-user-name/myproject.git
$ git remote set-url origin git@bitbucket.org:emstars/mis.git
Check again with git remote -v
to make sure:
$ git remote -v
origin git@bitbucket.org:emstars/mis.git (fetch)
origin git@bitbucket.org:emstars/mis.git (push)
upstream https://github.com/kiatng/platform-application.git (fetch)
upstream https://github.com/kiatng/platform-application.git (push)
Push changes to remote origin repository
Now that your old remote repository is disconnected, you can add the new remote repository. Use the following to connect to your new repository.
$ cd /path/to/my/repo
$ git push -u origin --all # pushes up the repo and its refs for the first time
$ git push -u origin --tags # pushes up any tags
We can also use TortoiseGit (Win10) to push. For SSH mode, set the remote key, look it up at home dir: the C:\Documents and Settings\New user.ssh\id_rsa.ppk Set the push URL.
Example
C:\wamp\www\emihe [master]> git remote -v
origin
C:\wamp\www\emihe [master]> git remote add origin https://user-name@bitbucket.or
g/user-name/****.git
C:\wamp\www\emihe [master]> git remote -v
bitbucket https://em@bitbucket.org/user-name/****.git (fetch)
bitbucket https://em@bitbucket.org/user-name/****.git (push)
origin
C:\wamp\www\emihe [master]> git push -u origin --all
Counting objects: 941, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (725/725), done.
Writing objects: 100% (941/941), 10.12 MiB | 42.00 KiB/s, done.
Total 941 (delta 192), reused 0 (delta 0)
To https://em@bitbucket.org/emstars/stars.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
C:\wamp\www\emihe [master]> git push -u origin --tags
Everything up-to-date
C:\wamp\www\emihe [master]>