Git: Repo Collaboration And Update

Step-by-Step Collaboration and Update

Mi'kail Eli'yah
7 min readMay 3, 2022

To share collaboration, if you are the program owner, initiate a remote project.
Apart from our own local machines, we can allocate a remote repo to share for collaboration. Assume a remote repository location of e.g. `/s/remote-project/1/`, add the remote location to share changes:

e.g. 
$ git remote add origin /s/remote-project/1
$ git remote add origin https://github.com/<user_id>/<repo_name>

Add files:

$ git push -u origin master"""
After emending the files, instead of just `git pull`, always try first:
> git pull --rebase
# git config --global alias.rebase "pull --rebase". This sets the command to be simplified as `git rebase`
> git rebase # git pull --rebase
This is to deter messy git commits.

If you do get a merge conflict,
> git rebase --abort
to un-do, and continue normally using git pull alternatively and resolve conflict.

If you forgot to add changes after `git commit`: git commit -mend

To view e.g. what is happening at a branch:

$ git log origin/<branch-name> -2 # view commit messages

To get the files to merge and synch from the remote repo to the local machine: $ git pull origin master # `pull` does `git fetch` and `git merge`

$ git log — grep=”#123" # find all the commits containing #123

`Fetch` and `merge` differs from `clone` and `checkout` in that they only bring in the changes (deltas).

$ git fetch$ git branch # check the branches and which branch you are at (marked by `*`) # switch between branches
$ git checkout <branch_name> # e.g. git checkout remotes/origin/master # access only specific branch in this case
Similarly to merge:
$ git merge remotes/<remote-name>/<remote-branch-name> master# In the case of conflicts:
If you go into the source files, it'll show you the conflicts. You'll see something kind of like:
"""
<<<<<<<< Head (What you have or the branch you are on)
========
>>>>>>>> commit hash
"""
What is conflicting, just have to clean it up by deleting those annotations and fixing the code. Then you have to commit and try merging again and if everything is fixed you will be able to merge cleanly. # to reverse merge
$ git merge --abort
There are cases where you do a [fork] on github, then you do a `git clone` and do a branch after: `git checkout -b <branch_of_forked>` to do the modification. If you have an existing version, you may do a `git merge`. After resolving conflicts, conduct tests, and:$ git add .
$ git commit -m 'merged conflicts for project 00'
$ git merge <forked_branch>
$ git push -u origin <forked_branch>
"""
If branch is created later, i.e. `git branch <new_branch_name> <existing_starting_branch>`, after `git commit`, switch to the new branch created and `git checkout <new_branch_name>`, then `git push -u origin <new_branch_name>`. Now, you can create a pull request:

Git performs merges 2 ways: fast-forward and 3-way merge.

Fast-forward merge: When merging the hotfix branch into the main branch, Git will move the main branch pointer forward to commit nr7jk.
After merging the hotfix branch, you can continue working on the feature1 branch. As you continue making commits on the feature1 branch, the commit history diverges.
3-way merge: To bring the feature1 branch into the main branch, (1) common commit both branches share (a90hb), (2) latest commit of the branch is (az84f), (3) commit of the branch to merge into (nr7jk)
# when working on your branch, other developers may update the main branch with their branch. This means your branch is now out of date of the main branch and missing content. Merge the main branch into your branch by checking out your branch:
$ git checkout <branch_currently_worked_on>
$ git merge main # sync with the main branch
# to pull or get the latest commits from the remote main branch:
$ git checkout main
$ git pull main
# switch to the branch you want to merge into
$ git checkout main
$ git merge <branch_to_merge (to branch currently on), e.g. user_id/feature1>
# push the branch code in the remote repository (this also creates the new branch on the remote if it does not exist)
git push --set-upstream origin <branch_name, e.g. user_id/bugfix_00 or user_id/for_project00_with_new_feature_00>

Given that people start contributing to the repo, check the changes:

$ git diff # alternatively: git difftool will load external tool to see the changes
$ git diff <file> # check the file changes / updates
$ git diff HEAD~1 HEAD # similar to `git log -p -n 2` (displays HEAD and HEAD~1): check changes between commits$ git blame <file> # shows the revision and author who last modified each line of a file. also: `git blame -L <from_line>,<to_line> <file>`
gitlens from vscode helps to see the diff quite clearly

To commit, do the usual git add.
To see the diff (view staged all changes (set in .git as a state before pushing to remote repo)) on the file added: git diff -staged

$ git log — pretty=format:”%h %an %ar — %s” $ or `git log`

To view the changes made in the commit:

$ git show #    $ git show HEAD 
# or $ git show <commit-hash>

Assume, to just share a piece of dependent code that is not to be pushed now, create a patch and share on a separate channel: git diff > <$filename>.patch
A coworker can download the patch file:

$ git apply <$filename>.patch # apply all the code changes made in the code base

To undo all changes in local, just reload all files by: $ git checkout .
If in the middle of a commit and have added files to the staging area but then changed your mind:

$ git reset HEAD . # move files back from the staging area to the working directory* Note: `git reset — hard HEAD` combine both git reset and git checkout

To un-do commits:

$ git revert HEAD — no-edit
# If changes have not been pushed, then git reset HEAD~1 has the same affect and will remove the last commit.
e.g. go back 2 commits: $ git reset HEAD~2
$ git reset HEAD -hard # Now remove those 2 commits

.

Caveat: While people may:
1. Pull the latest code from the repo.
2. Delete the committed file
3. Create another commit with the deleted committed file
4. Push the changes back to remote repo

This does not completely removed the committed from git. Git keeps a track of all the files committed. So anyone can still dig into the history of git commits and access the committed file.

To revert multiple commits at once we use the character `~` to mean minus (e.g. HEAD~2 := 2 commits from the head.). 
$ git revert HEAD…HEAD~2 — no-edit
$ git log
When reading to merge: git merge remotes/origin/master
Or `git pull — no-edit origin master`
To resolve conflict:
$ git checkout — theirs <file_name>
$ git add <file_name>
$ git commit — no-edit # stage the file

Steering Around Branches

List of all the remote branches : git branch -r # git branch -va.

`git branch <new_branch_name> <existing_starting_branch>` takes an existing branch and creates a separate branch to work in. At this point both branches are identical, e.g. `git branch branch_00 master`.To switch to a branch (create branch locally): 
$ git checkout <new branch name> # e.g. git checkout branch_00
# creates the new branch on the remote if it does not exist
# $ git push --set-upstream origin <new branch name> // after `git add` and `git commit`
# merge with main:
git checkout master
git merge <branch_to_merge_with_main>
# alternatively: Git moved HEAD to the new branch.
git checkout -b <new_branch_name>
# or,
git branch <new_branch_name> <identifier>
# Switch to a branch from a remote repo. To get a list of all branches from remote (* sometimes branches have to be seen on the github and pulled manually):
git pull
git checkout --track origin/<branch_name>
e.g. creating a new branch named `ff/hotfix1`
# Creating a Branch from Another Branch, e.g. creating the `feature_00` branch from the `develop` branch: 'git checkout -b feature_00 develop'
git checkout -b <new_branch_name> <branch_to_create_from>
# to pull some branch from Remote Repository
git pull origin <branch_name_to_pull, e.g. user_id/hotfix_01>
git branch
git checkout <branch_name_to_pull>
git branch
# delete branch (local branch)
git branch -d <branch_name> # -D : force delete
# see that the branch is being deleted.
git branch -a
# delete branch (remote branch)
git push origin --delete <branch_name>
# view branch(es)
git branch
[Further Notes]
git pull --rebase
git checkout <branch>
git rebase main

"""
git pull = git fetch + git merge
git pull — rebase = git fetch + git rebase
"""

If your partner requests you to take his/her branch, fork it and merge to your local repo branch collections:

"""
$ git remote add <remote> # e.g. <user_id>-remote git@github.com:<user_id>/<project_id>.git
$ git fetch
$ git branch
$ git checkout -b <user_id>-remote/<branch_name>
"""

… to be continued …

--

--