Some basic git commands that you will use usually

来源:互联网 发布:别墅网络布线方案 编辑:程序博客网 时间:2024/05/22 07:09

GIT OPERATION SUMMARY

Here are some tips/questions about the git operation that maybe we will meet in daily work.
The structure of the git and the basic commands

How to use the git aliases in shell

Execute the file:

    source /proj/lterbsSwdi/ltegit_admin_jenkins/aliases/.gitaliases

Then you can use the aliases of the git commands,here are the content of the file:

# git shortcutsalias gs 'git status'alias ga 'git add'alias gb 'git branch'alias gc 'git commit'alias gd 'git diff'alias go 'git checkout'alias gfp 'git fetch -p'#pretty git one line git logalias gh 'git log --pretty=tformat:"%h %ad | %s%d [%an]" --graph --date=short'#show only the file names changed in commitalias gsf 'git show --pretty="format:" --name-only'#run gitkalias gk 'gitk --all&'#show submodulealias gss 'cd $MY_GIT_TOP/;git submodule status;cd -'# submoduel updatealias gsu 'cd $MY_GIT_TOP/;git submodule update;cd -'# go to tp/root level dir in git repoalias cdgt 'cd $MY_GIT_TOP/'alias grr '/proj/lte_twh/x86_64-Linux2.6.16/ltetools/current/bin/grr'# show what DAILYBUILD/UP baseline is the current branch/commit based onalias gsb 'git log --oneline | grep -E "DAILYBUILD|(Import.+CXP)" | head -n 1'# show commits I have in my branch that are not in devalias gbv 'git merge-base origin/dev HEAD | tr -d "\n" | xargs -0 -I ancestor git log --oneline ancestor..HEAD'#show all git aliasesalias gas 'alias|grep git'# clone repo script clones and prepares the repo for usage, so pretty much clone and goalias clone_repo '/proj/lte_twh/x86_64-Linux2.6.16/ltetools/current/bin/clone_repo'#clean all but the stuff the stuff that we would like preserved like .ccache, xmls catalog etc#clean -dxf will wipe everything requiring user to source gitenv againalias gclean 'pushd $MY_GIT_TOP > /dev/null && git submodule foreach --recursive 'git clean -xdf' && git clean -xdf -e .ccache -e .flex_dbg -e remap_catalog\*.xml -e .baseline && popd > /dev/null'alias rmwhitespace 'perl -lpi -e '\''s/\s+$//'\'''#use recommneded version of RSA RTEalias start_rsa_rte_eclipse 'echo This eclipse installation has been deprecated. Please use "emca-ide"'alias plotziplog.pl '$MY_GIT_TOP/vobs/crbs/bbi/legacy/lpp/export/liblppgen/plotziplog.pl \!*' 

For example,you can just input ‘gb’ instead of ‘git branch’,’gs’ instead of ‘git status’.It is a good way to shorten your input number.

How to clean all of the untrack files and directories

git clean –xdf

If you want to specify an exclude pattern,you can add the ‘-e’ option,for example:

git clean –xdfe *.txt

If you want to delete all of the track and untrack modifications in the local branch,use:

git checkout . && git clean –xdf

then the local branch will be the same as the remote.or you also can use this one to attach your goal:

git stash

How to roll back the modification of the workspace(the red change)

git checkout -- <file name>

but sometimes it doesn’t work,you can try:

git submodule update

How to roll back the modification from the stage status to workspace

sometimes we have done the operation ‘git add’ but we want to roll back the modification,we can use:

git reset HEAD [file name]

How to roll back to a specific node you want

sometimes we have commit our modification and want to roll back,you can use:

  • roll back to the last version:
git reset --hard HEAD^
  • roll back to the last but one:
git reset --hard HEAD^^
  • roll back to a specific version you want:
git reset--hard [the commit id]
  • roll back to a specific version and remain the local modification
git reset --mixed [the commit id]
  • just create a local branch:
git checkout –b <local branch name>
  • create a local branch related to a remote branch,it will set the up-stream automatically:
git checkout –b <local branch name> <remote branch name>
  • create a remote branch related to the local branch(usually the same name)
git push origin <local branch name>:<remote branch name>

How to search the branch you want

  • search the specific branch,including the local and the remote,if you just want the remote,use ‘-r’:
git branch –a | grep [the key word]git branch –r | grep [the key word]

How to delete the local/remote branch

  • delete the local branch:
git branch –D <local branch name>
  • delete the remote branch,choose one you want:
git push origin :[remote branch name]git push origindelete [branch name]git branchdr [branch name]

How to set the up-stream of a local branch

git branch --set-upstream-to=<the remote branch name>

How to merge another branch’s/commit node’s modification to current branch

git merge <the branch name or the commit id you want to merge>

if meet some conflicts,use this command to solve them:

git mergetool

How to store the workspace and stage modification to a temp buffer

  • store the modification to the temp buffer:
git stash
  • show the info of the temp buffer stack:
git stash list
  • pop the modification from the buffer stack to the branch:
git stash pop

How to roll back a specific file to the node I want

sometimes I just want to roll back several files I want to a specific version,use:

git checkout <commit id> --<file1/to/restore>  <file2/to/restore>

How to fix the “unpack error” when push

you can try these methods below:
- push with –no-thin’:

git push --no-thin omnigerrit HEAD:<branch name>
  • check whether the git and gerrit version is OK and suited
  • switch branch and back:
    – Switch to another branch (e.g. develop).
    – Pull from the remote repository
    – Switch back to your new branch and push.
  • The local repository may be corrupted.To verify that the local repository is corrupted, you can clone your remote repository to another new directory and then push again. Running git repack might help in case the local repository is indeed corrupted.
  • Disk space in the server hosting Bitbucket Server is full.If disk space is indeed full for Bitbucket Server, clear them and push again.
    Some basic things about the merge
    the basic operation is:
    git merge <branch_name/commit_id>
  • usually we will add ‘squash’ parameter .The function is to package all of the modification node to one and merge to the branch you want,like:
git merge –squash <branch_name/commit_id>

Some basic things about the rebase

  • Usually we will use:
    git fetchp    git rebase [branch_name/commit_Id]
  • May we will meet conflicts,then you have to fix them:
    git mergetool [confict file]

Some other commands that used often

//create a new git repo

git init [project-name]

//download a git project

git clone [url]

//show the configuration of the git

git config –e [--global]

//delete the file of the workspace and put the modification to the stage state

git rm [file1] [file2]

//stop tracing the specific file but the file will remain in the workspace

git rm --cached [file]

//change a file’s name and put it to the stage state

git mv [file-origin] [file-renamed]

//show the differences when you commit

git commit –v

//add the new modification to the last commit.Whenever we push,there must only has one commit ahead of the remote

git commitamend [file]

//return to the last branch I have been,similar to ‘cd -’

git checkout –

//choose a commit and merge its modification to the current branch

git cherry-pick [commit id]

//show all of the tags:

git tag

//create a new tag in the specific commit:

git tag [tag name] [commit id]

//push the tag:

git push [remote] [tag name]

//push all of the tags:

git push [remote]tags

//search the ‘git log’ history

git log –S [key word]

//show the users who have commited,sorted by commit times:

git shortlog -sn

//show modification history of the person and the time with the specific file:

git blame [file]

//show differences between two branchs/commit id:

git diff [first branch/commit] [second branch/commit]

//show all of the remote repos:

git remote –v

//show the info of a remote repo:

git remote show [remote repo]

//push the local branch to the remote repo force,even with conflicts:

git push –remote --force
原创粉丝点击