Contents

Git overview

Here have some good pictures that is helpful for understanding git and contain the record for git commend

Concept Map

Here is a concept map given by Udacity.com. This picture shows the relationship between these basic git concept.

/images/2019-06-17-gitLearningNote/conceptMap.png

Git Data Transport Map

Git have four working zones. This map shows that using which commend to shift your code from one zone to another.

/images/2019-06-17-gitLearningNote/git-transport.png

This photo is provide by osteele

Git commend and usage

Initialize the git repository

git init

Clone a repository by giving the URL (github)

git clone <repository URL>

Show the status (Status of Index)

git status

Show the log of commit

git log

show all logs in graph and each log in one line

git log --graph --oneline <branch name> <branch name>

show specific number of log

git log -n <number>

Compare workspace and index

git diff compare workspace and index area (basically just show what you are not add)

git diff head compare index area and head in local repository

git diff <commit ID> compare index area and given commit ID in local repository

git diff <commit ID> <commit ID> compare two given commit

Add

git add <filename>

git add . add all

Commit

git commit -m '...'

add and commit

git commit -am '...'

Branch

show all branch

git branch

Open a new branch

git branch <branch name>

Open a new branch and checkout

git checkout -b <new branch name>

Push

git push <remote name> <branch name>

Pull

git pull <remote name> <branch name>

1
2
3
4
5
       A---B---C master on origin
      /
D---E---F---G master
    ^
    origin/master in your repository

After rungit pull master

1
2
3
      A---B---C origin/master
     /         \
D---E---F---G---H master

Merge

git merge <branch> merge a given branch to current branch

Eg: git merge feature (when you on master branch) –> merge branch feature into master.

Remote

show all remote

git remote

show all remote detail

git remote -v

giving a new remote name and connect it to a URL

git remote add <remote name> <URL>

Fetch

git fetch <remote name>

git fetch origin b Without changing workspace, update local repository from remote repository

In another word, fetch will update origin/master

Roll back

git reset --soft HEAD~1 Roll back 1 commit into index area

git reset --hard HEAD~2 Roll back 2 commits into workspace

Remove some file in Index

git rm --cached <file name>

git rm -r --cached . This command will set file to be untraced.

-r means recursively

--cached means remove file in index

Update .gitignore. delete something that you add in gitignore and commit before

Rebase

Suppose we have this situation and you are currently on branch feature

1
2
3
      A---B---C feature
     /
D---E---F---G master

The base of feature is commit E. Therefore rebase means you change the base depends on your given branch

After you run git rebase master, branch will be like:

1
2
3
              A'--B'--C' feature
             /
D---E---F---G master

Fast-forward(ff) in merge

Warning
fast-forward is by default for merge command

Suppose you have two branches master and feature

1
2
3
      A---B---C feature
     /
D---E  master

What is ff merge?

If you run git merge feature in master branch, it will be come

1
2
3
      A---B---C feature & master
     /
D---E

HEAD of master branch will direct forward to TARGET_HEAD

Disable ff (–no-ff)

If you run git merge --no-ff feature in master branch, new commit will force to create.

1
2
3
      A---B---C feature
     /         \
D---E-----------F master

Why need ff merge?

It is related with git pull actually. git pull will call either git rebase or git merge to reconcile diverging branches.

A very common case is like:

1
2
3
4
5
      A---B---C master on origin
     /
D---E  master
    ^
    origin/master in your repository

And run git pull is similar with run git merge origin/master. The best(actual) result of this command:

1
D---E---A---B---C master & origin/master

pull is comprised of merge. ff by default means, pull will try to use ff merge first and then create new commit. Logically more reasonable.

Beside ff provide a fast and clean branch management.

Reference: https://git-scm.com/docs/git-merge#_fast_forward_merge

Why need –no-ff merge?

The biggest reason is keep clean branch history.

Consider there will more commit after feature branch:

If we use --no-ff

1
2
3
      A---B---C---G---H feature
     /         \
D---E-----------F master

You can see feature branch is fully traceable. Commits (A B C G H).

However, if ff happen.

1
2
3
      A---B---C---G---H feature
     /        ^
D---E         master

feature branch is incomplete when trace back. Only Commits(G H). Commits(A B C) will be consider as part of master branch.

For team collaboration, this lost is not a good practice

Compare merge, merge –squash and rebase

If we have two branches master and feature

1
2
3
      A---B---C feature
     /
D---E---F---G master

merge

If run git merge

1
2
3
      A---B---C feature
     /         \
D---E---F---G---H master

merge –squash

if fun git merge --squash and git commit

H combine A B C

1
2
3
      A---B---C feature
     /
D---E---F---G---H master

Now you have feature branch and update the master. You may delete feature later

rebase

After you run git rebase master when you on branch feature:

1
2
3
              A'--B'--C' feature
             /
D---E---F---G master

You can checkout master and run git merge feature:

1
D---E---F---G---A'---B'---C' master & feature

git revert

1
a - b - c - d   Master

Then run git revert HEAD

1
a - b - c - d - d'   Master

d' revert all change in d

git cherry-pick

1
2
3
a - b - c - d   Master
     \
       e - f - g Feature

Then run git checkout master & git cherry-pick f

1
2
3
a - b - c - d - f   Master
      \
        e - f - g Feature

Git config

Usually config your github email account and username in global

list all global config

git config -l

set user name and email

1
2
git config --global user.name "your name"
git config --global user.email "your@email.com"

set password cache

git config --global credential.helper cache and next password/token entering will be cache git config --global --unset credential.helper will unset cache state

How to manage multiple github accounts

https://www.heady.io/blog/how-to-manage-multiple-github-accounts

Delete submodule

  1. Delete the relevant section from the .gitmodules file.
  2. Stage the .gitmodules changes git add .gitmodules
  3. Delete the relevant section from .git/config.
  4. Run git rm –cached path_to_submodule (no trailing slash).
  5. Run rm -rf .git/modules/path_to_submodule (no trailing slash).
  6. Commit git commit -m “Removed submodule "
  7. Delete the now untracked submodule files rm -rf path_to_submodule

Source