Git Common Operations

Source: Internet
Author: User
Tags ssh server

References:

Http://stackoverflow.com/questions/315911/git-for-beginners-the-definitive-practical-guide

Http://www.kernel.org/pub/software/scm/git/docs/

Http://progit.org/book/

Install git, configure username mailbox, and set up an SSH server

Http://www.cnblogs.com/elfsundae/archive/2011/07/06/2099182.html

Create/LIST/remove a new project/Repository

$ Git init
A hidden directory named ". Git" will be created in the current directory.
$ Git init project1
Equivalent to $ mkdir project1 & CD project1 & git init
$ Git status
Check whether the current directory contains a git Repo
$ Ls. Git
View git directory
$ Rm-RF. Git/
Remove everything about git

Configure git to ignore files

The. gitignore file can define the file to be ignored. For detailed rules see http://www.kernel.org/pub/software/scm/git/docs/gitignore.html

Filter folders:/build/
Filter some types of files: *. tmp
Filter files:/build/products/test. app
! It indicates not to filter :! *. C ,! /DIR/subdir/
Wildcard characters are supported: *. [OA] filters all files with the. O or. A extension in the repo.

There are three ways to apply Filtering:

  1. Filter all user applications of the repo:
    Put the. gitignore file in the following directory of the working directory. After editing. gitignore, submit it.
    Git Add. gitignore
  2. Filter your own repo backups only:
    Add/edit $ git_dir/INFO/exclude in your working directory. For example, if your working copy directory is
    ~ /Src/project1, the path is
    ~ /Src/project1/. Git/INFO/exclude
  3. Global System Filter
    Create an ignore file with a random name, for example ~ /. Gitglobalignore and configure git:
    $ Core. excludesfile = ~ /. Gitglobalignore

. Gitignore file example:

.DS_Store
### build directory
iMochaApp/build/
iMochaSDK/build/
### Testing projects directory
/Testing/

Getting the latest code

$ git pull <remote> <branch> # fetches the code and merges it into 
# your working directory
$ git fetch <remote> <branch> # fetches the code but does not merge
# it into your working directory

$ git pull --tag <remote> <branch> # same as above but fetch tags as well
$ git fetch --tag <remote> <branch> # you get the idea

Checking out code (clone)

$ Git clone user@host.com/DIR/to/repo [target dirname]

Commit Changes

When you modify a file, you need to submit the changes.

$ Git commit source/Main. c
The main. c file under the./source/directory will be submitted.

$ Git commit-
-A indicates that all modified files are submitted, but no new files are submitted. The newly added file needs to be added to the GIT index using $ Git-add.

"Submit" only changes your local Repo. If you want to submit the changes to the server, you need to use push:
$ Git push <remote> <branch>

View Current Status

$ Git status: You can view the current job and branch, what to submit, and remind you to forget something...

Undo/revert/reset a commit

If you do not want the current change to take effect and return the previous commit, run the following command:
# Revert to a previous commit by hash:
$ Git-reset -- hard

You can use head ^ to quickly specify the previous hash submission:
# Revert to previous commit:
$ Git-reset -- hard head ^

File comparison

The comparison command is $ git diff.

# To compare 2 revisions of a file:
$ Git diff <commit1> <commit2> <file_name>

# To compare current staged file against the Repository:
$ Git diff -- staged <file_name>

# To compare current unstaged file against the Repository:
$ Git diff <file_name>

How do you see the history of revisions to a file?

$ Git log -- filename

Git Branch (Branch)

The default branch of git is master.

# Create a new branch
$ Git branch <branch-Name>
# To see a list of all branches in the cureent repoitory
$ Git Branch
# If you want to switch to another branch you can use
$ Git checkout <branch-Name>
# To create a new branch and switch to it in one step
$ Git checkout-B <branch-Name>
# To delete a branch:
$ Git branch-D <branch-Name>
# To create a branch with the changes from the current branch, do:
$ Git stash
$ Git stash branch <branch-Name>

How do you merge branches?

If you want to merge a branch (e.g. "Master" to "release "), make sure your current branch is the target branch you 'd like to merge into (use $ git branch or $ git status to see your current branch ).

Then use
$ Git merge master
(WhereMasterIs the name of the branch you want to merge with the current branch ).

If there are any conflicts, you can use
$ Git diff
To see pending conflicts you have to resolve.

Remote branch tracking

Assume that you have cloned a remote repo with the 'some _ branch' branch. The following command traces this branch locally:

# list remote branches
git branch -r

# start tracking one remote branch
git branch --track some_branch origin/some_branch

# change to the branch locally
git checkout some_branch

# make changes and commit them locally
....

# push your changes to the remote repository:
git push

Create remote Branch

# create a new branch locally
git branch name_of_branch
git checkout name_of_branch
# edit/add/remove files
# ...
# Commit your changes locally
git add fileName
git commit -m Message
# push changes and new branch to remote repository:
git push origin name_of_branch:name_of_branch

Delete remote Branch

Git push [remote name]: [branch name]

$ Git push origin: mybranchname

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.