Many many days back, I wrote an article about some basics commands of git. It is a very basic article and is not sufficient to progress in a PROD environment. Here, I will try to resolve some confusion (How and What? ) when playing around with git. I would recommend you have a test repository. Also if you have some old repository cloned, it can be used as well. I hope this article will clear many of your confusions around git. I will be using GitHub as my git repository. You can use any git repository of your choice, such as Gitlab, GitBucket, Github, etc… I will be using Github. If you are completely new to Git or you have not been using Git for some time, I would advise you to read through this article to refresh your memory.
1. Synchronizing with a Git repository.
So, take a look at this diagram below. You have your Git repository and you have a folder created on your machine which has been synchronized. To start with git, there are two ways of doing so. Either you have an existing repository already on the online Git repository, or you have forked someone else repository and/or you have created a directory on your machine locally, initialize it, and copy it to your online repository using the API. In this example, we are cloning an existing blank repository using the following command:
# git clone [email protected]:jmutkawoa/mytestrepo.git
# ssh -T [email protected]
The arrow indicates data flow from the Github repository to your local machine.
2. Add data to your repository:
Once, you added some data to your repository, you will have to replicate that to your repository.
As you can see in the diagram below, data is flowing back to the repository.
3. Mirroring the repo to another folder
To mirror the repo to its mirrored directory, you will have to create another directory, perform a “git init”, “git remote add origin”, “git fetch”, and a “git checkout”
4. Adding more data to the repo and push to the repository
Go back to the directory you created in part 2, add some more data, and push to the repository.
To update your mirrored repo, do a git pull –rebase
What if you already push some stuff on the repository and you want to roll it back. This can be done using the command git revert.
Assuming you edited a file and its wrong:
You will need to find the last commit on the log and perform the revert:
Let’s say you fork someone repo to your account, then you cloned it. After fixing some bugs, you commit and push to your repo. Now you would want to send a pull request to the original repo. Assuming the commit has been accepter and that repo is having many other commits. Your repo on your account is not outdated. In any case, whether you send a pull request or not, you would want your repo to be updated with the original one. To do this, you would need to perform a reverse pull request.
- First, see what is in your origin and upstream. ‘Upstream’ should be blank
git remote -v
- Add the upstream. Example:
git remote add upstream git://github.com/jmutkawoa/check-firewall
- Now, ‘git remote -v’ should show you upstream is back. Fetch it.
git fetch upstream
- Then, perform a checkout. master here is the name of the branch. In your case, it might be something different.
git checkout master
git fetch upstream
- At this point, your local directory is equal to the original repo. Now push to your Git repository.
git push
I hope this article was useful. Please comment below for any questions.