Once a repository has been created, and is on a server like GitHub, you can also download it from other computers/locations. Downloading the repository for the first time is done via the git clone command. From then on you can get updates via the git pull command. git pull is the inverse of git push, and you will use it to pull in updates you don’t already have locally.
Open a second terminal window, navigate to the parent directory of your original git repository and create a directory where you will clone a duplicate of the repository:
Execute the git clone command to get the online repository (just change the url to your own repository)
You now have 2 clones from the same repository on your computer. One in the project folder and one in the project2 folder. We’ll use these two to test syncs and merges.
Push and pull
We simulate collaboration with 2 users by synchronizing in two different folders with the same remote (online repository). For the following actions, pay attention to the folder in which you execute the commands (from now on the project folder will always be in front of the dollar sign in the command to be executed).
Tip: you can open 2 terminal windows, set them side by side.
In folder 1 we create a new file project.txt, we add, commit and push to the remote:
In folder 2 we create a new file project2.txt, add & commit and try to push to the remote:
We get an error: there are new commits on the remote that we haven’t pulled into our project2 clone yet. We have to pull these first before we can do a push:
We are presented with a vim or nano editor to perform a merge. You can enter a custom message here. Enter :quit to close the vim editor and continue the merge.
When you execute git status, you will see that you are 2 commits ahead of the remote: 1 commit is the merge commit, and a second commit is the project2.txt commit:
Push the commits again to the remote. It succeeds this time.
Now also get these commits in your first folder (don’t forget to switch folders) via a git pull command:
rebase
We will work with a centralized workflow for the time being: we work with several people on 1 main branch via 1 remote. There are other more advanced workflows that work through branches and forks. However, these are a bit more complex.
In the previous scenario, we added a merge commit to fast-forward an out-of-date repository. This is not optimal in a centralized workflow: it is better to use a rebase with the git pull. A rebase will execute all commits from the remote first, and execute your commits after it, instead of executing a merge commit by default.
Delete project.txt in the project folder:
Then delete project2.txt in the project2 folder. We will also have to do a pull here before we can push. The difference with the pull is that we pass a rebase flag, to ensure that all commits from the remote repository are executed first, and then our own commits:
This way, no merge commit is created. When you execute git status you see that you are now only 1 commit ahead of the remote repository, instead of 2:
Push to the remote repository. Also pull in the projects folder so that both repositories are up-to-date.