:::: MENU ::::

Saturday, April 16, 2022







Until now we've learned a lot of interesting and complex techniques on how to use git locally. In the previous parts, articles go through the basic Git workflow for modifying files, staging the changes, and then committing those changes. After that, delete or rename files, get more information about commits or skip the staging area altogether. 
Then in part 2, Advance git techniques such as undoing staged, unstaged or committed changes in the git repo. 
So now let's drive into part 3 which will deal with remote git repositories like GitHub.
Let's being introduced GitHub. Sometimes, it is heard that Git is distributed version
control system.
Here distributed means that each developer has a copy of the whole repository on
their local machine.

GitHub provides free access to a Git server for public and private repositories.
There are many internal based Git hosting providers, GitHub is one of them. Organization
can setup their own Git server on their own network to host private repositories.

For real configuration and development work, the organization should use a secure
and private Git server and limit the people authorized to work on it.

Basic Interaction with GitHub

 
When developers are working with remote repo, the workflow for making changes has some extra steps. 
In local, they still need add, modify, stage and commit the changes; after committing developers need to fetch any new changes from the remote repo, then merge if necessary and push their own changes to the remote repo.


$ git clone URL
Description: To get a local copy of the remote repository.
Note: when we clone a remote repo to our local machine, git assign 'origin' name to remote main branch.

$ git remote add <name> <url>
    Description: This will add a new remote repo URL named 'origin'. This command takes
two arguments: one - name e.g. origin/ destination etc. second - url ( remote repo. url )

Troubleshooting: how to override the URL value of origin?
    To fix this, we can do:
            1. use another remote name and url, command: $ git remote add <name> <url>
            2. renaming, command: $ git remote rename origin destination
            3. deleting, command: $ git remote rm <name>

$ git pull
    Description:

After adding new files or modifying the files. we have to stage those changes and
committed them and push those changes to the remote repo.
we have to use $git push

$ git push
    Description: The git push command gathers all the snapshots we've taken and sends them to the
remote repository.
$ git remote -v
Description: it will show configuration of remote repo.


It normally shows two URLs associated with origin remote. One will be use for fetching
data from remote repo, and second one for pushing data.
These two Urls are pointed in the same place. But can be different for security reason.

$ git remote show origin
Description: get detail configuration of remote repository
$ git branch -r
Description: to show remote branches of remote repo.
Imagine a scenario, where fetching from remote repo. is necessary.
Suppose, our colleague "Nahian' added some files to our repo. Now I want to see that changes
and make modification if necessary.
lets run ' $ git remote show origin'
It shows:



It said that 'local out of date'. This means I need to sync the updated changes.

$ git fetch 
Description: copies the commits done in the remote repository to the remote branches. So it's easy to see what other people have committed.

Note: git pull instantly merges while git fetch only retrieves remote updates.

$ git merge origin/main
Description: merging changes and synchronising with local to the remote repo.


Updating the local Repository

$ git pull
Description: fetch and merge together


$ git remote update
Description: This will fetch the contents of all remote branches so that we can just call checkout or merge as needed.



$ git log --graph --oneline --all
    Description: it will show commit history in graphical representation.

Solving merge conflicts

Three-way merge technique: pull-merge-push workflow is followed for resolving conflicts.



Pushing into remote branches

$ git push -u origin [branch-name]
    Description: -u flag means to create the branch upstream.

$ git push --delete origin [branch-name]
     Description: delete remote branches.







Git rebase that prevent three-way merge and makes commit linear. 

This makes debugging easier and prevents three-way merges by transferring the completed work from one branch to another.

Awesome! Rebasing instead of merging rewrites history and maintains linearity, making for cleaner code.

There are many use of rebase command, two of them are:
1. rebasing feature/mew branch and make on top of master branch.
e.g. (new branch) $ git rebase master
2. fetch-rebase-push that makes merging linear. It is beneficial than fetch-merge-push or pull-push, because these command may perform three-way merge. 
You can get the best of both worlds: rebase local changes before pushing to clean up your work, but never rebase anything that you’ve pushed somewhere.



$ git rebase master
Description: move the the current branch on top of the master branch. 
$ git rebase origin/master
Description:




Best practices should be exercised when we are supposed to collaborate with others:

  • Always synchronize your branches before starting any work on your own. This way, when you start changing code, you're starting from the most recent version, 
  • minimizing the chances of conflicts or the need for rebasing.
  • Avoid having very large changes that modify a lot of different things.
  • when working on a big change, it makes sense to have a separate feature branch. Have the latest version of the project in the master branch and the stable version of the project
        on a separate branch.
  • you shouldn't rebase changes that have been pushed to remote repos.

References:
https://git-scm.com/book/en/v2/Git-Branching-Rebasing
https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History
https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/addressing-merge-conflicts/resolving-a-merge-conflict-using-the-command-line
https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/incorporating-changes-from-a-pull-request/about-pull-request-merges
https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/addressing-merge-conflicts/about-merge-conflicts




1 comment: