One of the best feature about GIT Source Control Management(SCM) is its distributed. Git repositories can be hosted in a file server in the web which can be your backup (or) a repository you are contributing to (or) someone else is contributing to yours.
To collaborate in any project in the current world, zipping your development folder and sending it over an email (or) sharing the zip via any file sharing platforms is unacceptable. You need to know how distributed SCM system works.
Good news is, git is a distributed SCM, so you just need to know how to manage your remote repositories which basically involves setting up remote repositories, cloning, push, pull and handling conflicts.
This is the syntax to add a remote git repository git remote add <shortname> <url>
When you try to create a git repository in Github by default it will ask you run few git commands like below,
If you are setting up a new repository with name bootstrap5alpha2. You can run the below commands in your local system and notice git remote add
command with origin(is just a short name for your remote repo) and url. Here main
is the new master
branch, default branch name has been changed.
1# …or create a new repository on the command line2echo "# bootstrap5alpha2" >> README.md3git init4git add README.md5git commit -m "first commit"6git branch -M main7git remote add origin https://github.com/bobbydreamer/bootstrap5alpha2.git8git push -u origin main
If you already have the repository 'bootstrap5alpha2' in your local system and you want to just upload it to remote then you can do the below,
1# …or push an existing repository from the command line2git remote add origin https://github.com/bobbydreamer/bootstrap5alpha2.git3git branch -M main4git push -u origin main #or use this if github just has initial commit : git push --force -u origin main
git push -u origin new-feature
: -u
or --set-upstream
flag adds the new branch new-feature
to the upstream(tracking) reference, so next time git push
can be invoked without any parameters to automatically push the new-feature branch to the central repository.Cloning a repository automatically downloads all the data from that repository including its history and git makes local master branch as a tracking branch for the master branch of the origin repository (short: origin/master ) by Git.
If tracking branches need to be created manually, you need to run below commands
1git checkout -b newbranch origin/new-feature2
3# (or)4
5# create branch based on remote branch6git branch new-feature origin/master7git branch --track new-feature origin/master
So, if remote repository is updated with new developments after you cloned, you can do a git fetch
and download all the latest data but does not merge that data with your current working directory. Thats the job for git pull
, it does all the merging automatically.
In the below git clone
command, i have used '.' to initialize current folder as a git repository and if i have not done that, in that case, git clone
command would have created a new folder 'HelloWorld` and git initialized that.
1Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test32$ git clone https://github.com/bobbydreamer/HelloWorld.git .3Cloning into '.'...4remote: Enumerating objects: 74, done.5remote: Total 74 (delta 0), reused 0 (delta 0), pack-reused 746Unpacking objects: 100% (74/74), done.
git remote
with -v
shows the remote details(remote name, url)
1Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)2$ git remote -v3origin https://github.com/bobbydreamer/HelloWorld.git (fetch)4origin https://github.com/bobbydreamer/HelloWorld.git (push)5
6Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)7$ git remote show8origin9
10Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)11$ git branch12* master13
14Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)15$ git branch -a16* master17 remotes/origin/HEAD -> origin/master18 remotes/origin/master19
20Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)21$ git remote show origin22* remote origin23 Fetch URL: https://github.com/bobbydreamer/HelloWorld.git24 Push URL: https://github.com/bobbydreamer/HelloWorld.git25 HEAD branch: master26 Remote branch:27 master tracked28 Local branch configured for 'git pull':29 master merges with remote master30 Local ref configured for 'git push':31 master pushes to master (up to date)
If you want to share your code changes or back it up, you have to do git push
which basically uploads all your committed data to your remote repository.
1Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)2$ cat readme.MD3# Push Testing4Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)5$ git add .6
7Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)8$ git commit -m "Push Testing"9[master f1db835] Push Testing10 1 file changed, 1 insertion(+), 2 deletions(-)11
12Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)13$ git push origin master14Counting objects: 3, done.15Delta compression using up to 8 threads.16Compressing objects: 100% (2/2), done.17Writing objects: 100% (3/3), 285 bytes | 285.00 KiB/s, done.18Total 3 (delta 1), reused 0 (delta 0)19remote: Resolving deltas: 100% (1/1), completed with 1 local object.20To https://github.com/bobbydreamer/HelloWorld.git21 d33855d..f1db835 master -> master
Push tags using below command
1git push --tags
git fetch
command will fetch all the changes on the server that you don’t have yet, it will not modify your working directory at all. It will simply get the data for you and let you merge it yourself. However, there is a command called git pull
which is essentially a git fetch
immediately followed by a git merge
.
What git pull
does is, it will look up what server and branch your current branch is tracking, fetch from that server and then try to merge in that remote branch.
Now, i have updated the readme.MD directly at git as 'Pull Testing' and currently local system is not aware of the new changes.
1Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)2$ git lol -n 33* f1db835 - (HEAD -> master, origin/master, origin/HEAD) Push Testing (6 minutes ago) <Sushanth Bobby Lloyds>4* d33855d - 13. Updating readme.md (2 years, 8 months ago) <Sushanth Bobby Lloyds>5* aac86ea - 12. Adding readme.md (2 years, 8 months ago) <Sushanth Bobby Lloyds>
There is no easy porcelain command to see if there are any latest commits at remote. Below is one of the approaches.
1Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)2$ git ls-remote https://github.com/bobbydreamer/HelloWorld.git3311577e12ae8b60fe1b3ef9abf6580922a2c3c13 HEAD4311577e12ae8b60fe1b3ef9abf6580922a2c3c13 refs/heads/master
Every other commands gets data from local only like below and after git fetch
they can show the remote commits.
1Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)2$ git rev-parse origin/master3f1db83597d8adba36d22262644c0381c36e595644
5Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)6$ git rev-parse HEAD7f1db83597d8adba36d22262644c0381c36e59564
First we will try git pull
then we will do another update and try git fetch
1Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)2$ git pull -at3remote: Enumerating objects: 5, done.4remote: Counting objects: 100% (5/5), done.5remote: Compressing objects: 100% (2/2), done.6remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 07Unpacking objects: 100% (3/3), done.8From https://github.com/bobbydreamer/HelloWorld9 f1db835..311577e master -> origin/master10Updating f1db835..311577e11Fast-forward12 readme.MD | 2 +-13 1 file changed, 1 insertion(+), 1 deletion(-)14
15Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)16$ git lol17* 311577e - (HEAD -> master, origin/master, origin/HEAD) Pull Testing (37 minutes ago) <Sushanth Bobby Lloyds>18* f1db835 - Push Testing (39 minutes ago) <Sushanth Bobby Lloyds>19* d33855d - 13. Updating readme.md (2 years, 8 months ago) <Sushanth Bobby Lloyds>20* aac86ea - 12. Adding readme.md (2 years, 8 months ago) <Sushanth Bobby Lloyds>
Now again we have updated readme.MD in github and this time try to fetch.
1Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)2$ git lol -n 33* 311577e - (HEAD -> master, origin/master, origin/HEAD) Pull Testing (40 minutes ago) <Sushanth Bobby Lloyds>4* f1db835 - Push Testing (41 minutes ago) <Sushanth Bobby Lloyds>5* d33855d - 13. Updating readme.md (2 years, 8 months ago) <Sushanth Bobby Lloyds>6
7Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)8$ git ls-remote https://github.com/bobbydreamer/HelloWorld.git95f2670d5823af2977a0bdbe8df08dfa6aae36bd7 HEAD105f2670d5823af2977a0bdbe8df08dfa6aae36bd7 refs/heads/master11
12Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)13$ git branch -vv14* master 311577e [origin/master] Pull Testing15
16Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)17$ git fetch --all18Fetching origin19remote: Enumerating objects: 5, done.20remote: Counting objects: 100% (5/5), done.21remote: Compressing objects: 100% (2/2), done.22remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 023Unpacking objects: 100% (3/3), done.24From https://github.com/bobbydreamer/HelloWorld25 311577e..5f2670d master -> origin/master26
27Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)28$ git branch -vv29* master 311577e [origin/master: behind 1] Pull Testing
Since its fetched, now you can do git merge
1Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)2$ git merge origin/master3Updating 311577e..5f2670d4Fast-forward5 readme.MD | 2 +-6 1 file changed, 1 insertion(+), 1 deletion(-)7
8Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)9$ git lol -n 310* 5f2670d - (HEAD -> master, origin/master, origin/HEAD) Fetch Testing (10 minutes ago) <Sushanth Bobby Lloyds>11* 311577e - Pull Testing (49 minutes ago) <Sushanth Bobby Lloyds>12* f1db835 - Push Testing (50 minutes ago) <Sushanth Bobby Lloyds>
The git fetch command updates only the remote-tracking branches for one remote repository. In case you want to update the remote-tracking branches of all your remote repositories you can use the following command.
1# this runs git fetch for every remote repository2git remote update3
4# the same but remove all stale branches which are not in the remote anymore5git remote update --prune
When all your developments on a feature is done. You can delete the remote branch.
1git push origin --delete FeatureBranch007
1Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)2$ git remote rename origin rem-HW3
4Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)5$ git remote -v6rem-HW https://github.com/bobbydreamer/HelloWorld.git (fetch)7rem-HW https://github.com/bobbydreamer/HelloWorld.git (push)
1Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)2$ git remote remove rem-HW3
4Sushanth@Sushanth-VAIO MINGW64 /d/GITs/test3 (master)5$ git remote -v
Forking is a popular workflow, mostly used in open-source projects. Fork operation clones the remote repository to your github/BitBucket account instead of downloadig it your local computer. Forked repositories are special they just git clone
on the server-side maintained by GH/BB. Later you can clone the personalized copy of that repository to your local system.
Below is the process when creating a fork
Forking workflow mostly would be following below steps,
origin
, remote is automatically added by git clone
which you can check by git remote -v
. git remote add upstream URL
, this will add that official open-source repository as upstream. git remote -v
you should two entires for origin and two rows for upstream.1git pull upstream master2# or 3git fetch upstream 4git checkout master5git merge upstream/master
git push origin feature-branch
git branch -D feature-branch
1git pull upstream master2git push origin master
Below is the process when submitting a pull request,
On the other side what the maintainer does to integrate is,
Now this contribution is now part of the bigger project and other developers should pull from the official repository to synchronize their local repositories.