Merge conflicts
When merging two branches it’s not very incommon to have conflicts, a conflict means two branches getting merged have two different versions of a file for example and git doesn’t know which one to keep.
Therefore when we get a conflict we need to fix it in order to merge properly, but how is that?
When git generates a conflict, it writes “special characters” in the content of file to specify the problem, to resolve the conflict we remove the special syntax, and keep the changes made by the feature branch if we want to keep it, save the modified file, add it to the staging area, commit it, and then try mergin again.
Introduction to remotes
Remote repos are repos that are in the cloud this repos has many benefits like :
- Work is backed up
- Teams can collaborate on the same project
Companies can choose to not host their repos on the cloud if they have strict privacy rules regarding data.
Cloning repos
Cloning local repos
git clone path/to/project
We can also give it a name like this :
git clone path/to/project new_repo_name
Cloning remote repos
git clone URL_of_repo
When cloning remote repos git remembers where the original was, we can list all remotes associated with the repo like this :
git remote
or if we want to the result to be more detailed or “verbose”:
git remote -v
By default git will name the remote repo origin
, we can name the remote ourselves by using this command :
git remote add <name of remote repo> URL
Example :
git remote add hmed https://github.com/hmed/repo
Pulling from remotes
Fetching from the origin
remote to our local repo:
git fetch origin
This :
- Doesn’t merge the remote repo contents into our local repo
- It might add new branches that are present in the remote but not in the local
- It fetches ALL branches
If we want to fetch a single remote branch :
git fetch origin main
To synchronize content after fetching, we use :
git merge origin
This merges origin with our current local branch
The pull
command is more convenient it simplifies the operation by fetching and merging “at the same time”
git pull origin
As fetch
we can pull a single branch from the remote :
git pull origin dev
Pushing to remotes
To push changes to our remote repo from our local repo we use :
git push remote local
Example :
git push origin main
In a typical workflow we first pull changes from the remote repo, work on our changes locally, save, commit and then push to the remote repo, if we don’t pull first we might encounter an error when pushing stating that our local repo is behind and we need to pull from the remote before pushing.