Git tutorial

Git

A brief overview of git:

When you clone the repository you pull all the "files" and a history in the local machine. Git works by putting files into a staging area (sometimes called "index area", or "cached area") before they actually become part of the repo. For those familiar with perforce: it is like choosing which files to push to the repo on submit. This is done through the git addcommand to add to the staging area and the git reset to remove from the staging area. After you append files into staging area you check yourself that all is fine via git statusand via git diff --cached.

If all is fine run git commit to make them a part of your local repo. Only files that are staged will be committed. This is important to understand. By committing to your local repo, it only makes it a part of your repo on your machine, not the repo you cloned from. To make it part of the repo you cloned from, also known as "origin", you will run the git pushcommand after you have committed locally. More specific commands on this below.

Git does not store branches in "separate folder" as it done e.g. as for svn, as for classic p4 branches. For git this conception was embedded into control version system.

Getting a repo:

git clone git://some-repo-url/a-repo.git # get repository via network, create remote repository with name "origin", track repository "origin/master" in local "master" branch
cd a-repo/

Getting top of tree changes on the repo you cloned from (origin):

git pull

Seeing what you've changed:

git diff

Seeing the commit history:

git log

Seeing the commit history colorful:

git log --graph --format=format:"%C(bold cyan)%h%C(reset) - %C(bold green)(%cr)%C(reset) %C(white)%s%C(reset) %C(bold white) - %cn%C(reset)%C(bold yellow)%d%C(reset)" --abbrev-commit --date=relative

Seeing the commit history on a different branch:

git log branchname

Seeing the commit history comfortable for grep:

git log --pretty=oneline

Seeing what is staged and what branch you are on:

git status

Pushing changes to the repo:

git add directory or filename(s)
git commit
git push origin HEAD:refs/heads/master

Reverting a change:

git reset commit-id or HEAD~number --hard

You can use a specific SHA1 hash (or just the first few characters of it) commit-id you get from git log or HEAD~N, where N is the number of commits you want to go back from HEAD. Note that without the --hard option you will keep all the files the same, they just won't be commited and they won't be in your staging area either. Previous link to HEAD to after git reset or git merge command is recorded in ORIG_HEAD.

Reverting a specific file:

git checkout commit-id or HEAD filename(s)

A word about branching:

Branching in git is lightweight and switching between branches is fast. The main branch is usually called "master." If you decide you need to switch contexts and work on something else, you would commit your changes to the branch you are on, and then create a new branch and switch to it. Once you are done with your changes on the new branch you can commit those and switch back and forth between branches quickly.

It is also exist stashing mechanism to store you changes in some stack.

  • git stash to save the changes to the internal patch
  • git stash list to see a list of accumulated patches
  • git stash apply to apply the last patch from the patches stack

Creating a new branch:

git branch branchname

Switching to a branch:

git checkout branchname

Deleting a branch:

git branch -D branchname

Pushing your branch to origin (the repo you cloned from): 

git push origin branchname

Deleting a branch on origin:

git push origin :branchname

Getting a branch from origin:

git checkout -t origin/branchname

Getting a single commit from one branch to another:

git cherry-pick commit-id

Getting several commits from one branch to another:

git merge branchname

Making several commits become one commit:

git rebase -i starting-commit-id

When you want to switch contexts to another branch or something but don't want to commit your code:

git stash

Then to get it back:

git stash pop

Troubleshooting

  • Unlink of file '.git/objects/pack/pack-04726570f47e5999bd01201243b28c8f06aa689f.pack' failed. Should I try again? (y/n)
    • Make sure no external diffs are open
    • On Windows, use ProcessExplorer to find the process holding the file (Find -> Find Handle or DLL...)

posted on 2018-12-19 10:16  cdekelon  阅读(86)  评论(0)    收藏  举报

导航