SHANYIN'S SPACE

Logo

Blog | Notebooks | Sharing | Everything

View My GitHub Profile

Git

Learning notes from Learn Git Branching (Github)

Basic Commands

Commit

Branch

Merge

Rebase

Take out series of modification records and duplicate to store in another place (make more linearlized committing history)

Features

Move on the commit tree

HEAD always points to the latest commit record. Most of the commit commands start from changing the direction of HEAD. Usually HEAD points to the branchname.

Splitting HEAD:

Relative Reference

From the previous section, if want to specific a node in the commit log, Hash value is needed. (Not convenient)

Relative reference: (e.g., git checkout HEAD^)

Forced branch position change (-f)

Reset Changes

Organize Commit Tree

Cherry-pick

Interactive Rebase

Used for: (actually creates a new “current” branch)

Techniques and Tips

Local Stack Commit

When debugging some debug sentences were added and printed in the bugFix branch, but they shouldn’t be included in the main branch after debugging. (If use fast-forward)

Solution: Only duplicate the final commit which solved all problems (in the bugFix branch)

Modify

On a node a commit is done, a new branch is created and another commit on that branch is done. But we want to modify this old node.

Tags

Sometimes we want some permanent links pointing to some specific commits. (Role of anchor points)

Describe

Output: <tag>_<numCommits>_g<hash> (when ref has a tag, the output will be only the tag name)

Parental Commit

^ can also be followed by some digits. It means the specific parental commit node (after merging there will be several parental nodes)

^n & ~n can be combined to use in complicated branch networks. This also supports chain operation (e.g., HEAD^2~3)

Remote (Push & Pull)

Clone

When using GitHub, we want a copy from the remote repository

Remote Branch

After git clone, a new branch origin/main in the original repository is generated. It is so called “remote branch”. It reflects the state from the latest communication. Another feature of remote branches is that when log out, HEAD will be splitted automatically.

If in the current repository, for origin/main, git commit only creates commit with a splitted HEAD and both main & orgin/main does not move. Neither does main in the remote repo. The update will be done only after the corresponding branch in the remote repository updates.

Fetch

Fetch data from the remote repository. Usually through http:// or git:// protocol

This only downloads what is missing and modifies the remote branch pointers. It doesn’t modify the local files, or update the local main branch

Pull

After fetching data, we should update to the practical work

Conventionally, the operations can be

To combine the fetching and merging operations:

Push

Deviation

If there is too much deviation from the previous sync. from between the local and the remote repository (or another commit has already written at the same position), push can be failed.

Locked Main

Remote Rejected: If working in a big team, this can be a reason of the locked main. It requires some Pull Request processes to modify. If just commit to local and push, error messages can appear.

Solution: Create a new branch and apply for the pull request

Practically

The process should be:

  1. Modify the files locally
  2. git add <file_name>
  3. git commit or git commit --amend
  4. git push (better to pull before push)

Advanced Operations in Remote

Rebase (in Remote)

In practical work, usually developers work in a feature branch splitted from the main branch. Only one time of combination should be applied. But sometimes some developers only push/pull on the main branch.

The following workflow integrates 2 steps:

  1. Integrate all feature branches into main
  2. Push and update the remote branch

Operations:

  1. git pull --rebase: rebase our work to the latest commit of the remote branch
  2. git push

Merge (in Remote)

Differences between rebase & merge

Tracking

main is set to track o/main (This property is configured when cloning)

In default:

  • local branch "main" set to track remote branch "o/main" (when clone succeeded)

If want to specify a new branch name:

Push Parameters

Fetch Parameters

Similar to the push parameters, but the uploads become downloads

### Empty Source

No <source>, but remain :<destination>

e.g.,

Pull Parameters

pull only focus on the final commit position