git

git uses blob object to store the conent of file of each version, the blog file name is SHA1 value of content of file.

Git uses tree object to store the filename/path and corresponding blob data.

git ls-files -s, to list all files

git ls-remote, to list remotes and branchs 

 #git local development and set

Remote folder: C:\mydoc\documents\tech\self\meter\datasheet\used\remote

Local folder: C:\mydoc\documents\tech\self\meter\datasheet\used\local

1. setup remote

cd to remote folder

git init --bare test.git

2. setup local

cd to local folder

git clone C:\mydoc\documents\tech\self\meter\datasheet\used\remote\test.git

a test folder will be created.

cd to test folder to do normal git operations 

3 . merge 

git mege test_branch

git push origin master

 

 

#The most used commands in daily work

Usig git bash as command tool.
> list status : git status
> list log: git log --graph --oneline
> discard all changed files: git checkout -- .
> multiple commits without change message: git commit --amend
> modifiy the commit message only: git commit --amend -m "new message here"

> Rebase & push
  > (before rebase)get remote update at branch : git fetch orgin master
  > (rebase before push) rebase a branch : git rebase orgin:master
  > solve conflition manually
  > git add .
  > git rebase --continue
  > git push --up-stream = xxx

> list branchs local & remote : git branch -aav
> switch to a existing branch : git checkout branch_name
> create & switch to a branch : git checkout -b branch_name
> auto merge current branch: git pull
> delete local branch: git branch -D local_branch_name
> delete pre-stored branch of remote branch: git branch -d -r origin/feature/Cmxxx
> store temp work, for example, you changed the source code at master branch.
  > store: git stash
  > switch to a new branch : git checkout branch_name
  > apply: git stash apply
> working branch at feature-a, feature-a is going to review, there is a new task which is based on the feature-a, so you create a new branch feature-b which is based on feature-a
> feature-a is merged to master after review
> feature-b branch changes lot, if you want to rebase on master, there will be lots of confliction
-> solution, remmber the commits (e.g.18a288f)at feature-b, go to master and update to last changes by git pull
  -> git create a new branch feature-c based on master: git checkout -b feature-c
-> at branch feature-a : git cherry-pick -i 18a288f, all will be udpated.

#Wording

The default file .gitignore who contains the ignore files in each folder.

HEAD is a reference to the last commit in the currently check-out branch.

downstream when you copy (clone, checkout, etc) from a repository

upstream generally refers to the original repo that you have forked

 

#create a remote repository in local file

in folder, git init --bare

 

#local/client the first step

cd into your folder

git init

 

git remote add origin remote-address 

or 

git clone remote address

or git clone -b branchname remoteaddress

Note that, git clone also can clone two folders in local. for instance, git clone oldfolder newfolder

 

#show the remote

git remote

 

#show the currnt branch tracking remote branch 

git branch -vv

git status -bs

 

#add a new file

git add files

git commit -m"comments"

git push

While a already commited file is modified, just git commit and git push are enough.

Note that, git commit  will prompt a text editor for input the commit log.

 

#delete files

git rm files

git commit -m"comments"

git push

#rename file or folder 

git mv oldfile newfile

git push

#show status

git status, show the new folders/files under current folder

git status --untracked-files = all , show all folders incluing sub-folder

git branch -aav to see the branch information including the remote and local

#discard the local commits

 git reset --hard origin/<branch_name>

#delete all new generated file/folder or untracked files

git clean -fd

git clean -n : list the untracked files
git clean -f : clean the files
git clean -fd : clean the directories
git clean -fX : remove the ignored files
git clean -fx : remove the ignored and non-ignored files.

#checkout a file

git checkout the_file_path

#update repository

git pull :update local repository with merge 

git fetch: update local repository but it does not merge

#check the history

git log

git log --oneline, shows the change in one line comprehensive

git log -n 3, only show the 3 commits

git log 419f7455 -1 : only show the commit 419f7455

git log 419f7455 -3: will show the 3 commits from 419f7455

git shortlog, shows the short log.

git show 9da581d910c9c4ac93557ca4859e767f5caf5169/or short name to see what's the detail changes , a brief sha is also OK.

git show-branch, gets the sumary of current development branch 

git diff 9da581d910c9c4ac93557ca4859e767f5caf5169  9da581d910c9c4ac93557ca4859e767f5caf5199 to check the difference between two commits.

gitk the graph view of changes

git log -p filename, it view the history of the file.

git log --graph --oneline, graph way to display with log

git reset

reset the commit before push to service

git reset --hard xxxx, xxx is the commit tag.

tag & branch

Tag is most for version, for example v2.0.0

branch is most for development path, a new project or new spring can be a new branch

 

git tag: list all tags

git tag -a tagName, add a new tag

git push --tag: push the tag to remote

git checkout tags/thetag -b branch_name: to check a tag to new branch in local

 

git diff

git diff , it will show the current folder different with local responsitory

git diff --name-only master origin/master

git dfif --staged : show the differents only commit to local

git diff master : list the diff on master if the master is your local folder

Configuration

git config --global user.name "your name here"

git config --global user.email "your email here"

git config -l list all configured 

#.gitignore

.gitignore file

Ignoring the file everywhere by adding it to the .gitignore file in the topmost directory of  repository. Multi .gitignore file in different directories is accepted, it only applies for current directory and sub directoies.

To ignore .o files, place *.o in your top level .gitignore

To skip a folder : folder/ (*linux slash)

after git add .gitignore file, you can use the git status shows the status of files.

#branch

git branch newbranchname [start-commit], it will create a branch name, but still system still working in current branch, the [start-commit] is to select which branch as the starting of the new branch, it is option, the default the current working branch.

git show-branch, shows the branch in local and remote

git checkout newworkbranch switch to work branch

example: 

git branch Sprint-1

git show-branch

the current working branch is master (*)

git checkout Sprint-1

 now, it switches to Sprint-1(*)

commit somefile with git add ., git commit 

push to server, git push, the errro will be indicated: 

 

git checkout -b newbranchname [start-point], create and switch to new branch

git branch -d branchname, delete a branch

git branch -vv to track and show local and remote branchs

 

general working flow:

create a branch in local, git checkout -b newbranchname

work as you want, git add . git commit .

git push -u origin newbrachname, it will auto push to remote with newbranchname

 

git switch command is also for branch operation

git switch new_branch: switch

git swich -c new_branch : create a branch and switch

 

change the local branch name 

git branch -m old_branch_name new_branch_name

 

# list remote branch

git fetch

git branch -avv

 

#checkout a remote branch

git fetch

git checkout -b newbranch origin/newbranch

 or

git fetch origin branch_name

#merge

git merge development master //merge local development to local master, but now we still in the development branch
git push origin master //push to remote, the orgin master

 

#Show which tag/branch 

git show 46659f7deb962f55c728e70597e37c2a3ab6326d
commit 46659f7deb962f55c728e70597e37c2a3ab6326d (tag: v237)
Merge: 82c8e3e650 31751f7e2a
Author: Lennart Poettering <lennart@poettering.net>
Date: Sun Jan 28 16:58:17 2018 +0100

Merge pull request #8030 from keszybz/another-news-update

NEWS: reword one sentence

 

generate a patch between branches

git diff master Branch1 > ../patchfile
git checkout Branch2    
git apply ../patchfile

===========================================================

problems Solve

 

fatal: refusing to merge unrelated histories

git pull --allow-unrelated-histories

 

visit local reponsitory without proxy: fatal: repository 'http://server:8080/tfs/DefaultCollection/_git/PlayProject/' not found

git config --local remote.origin.proxy ""

the "origin" is alias of remote.

git config --list, to list all configurations

git remote show origin, it is to show the URL of remote/origin

in case the error is shown when run git remote show origin,

we can use command to fix it. git remote set-url origin theremoteurl

 ===========================================================

#change the commit message before push

git commit --amend

 

#rename a local branch name

git branch -m new_name

The current local branch will be changed to new_name.

 

#remove the last commit

git reset --soft HEAD~1

HEAD~1 : the last commit

--soft : When specifying the “–soft” option, Git is instructed not to modify the files in the working directory or in the index at all.

--hard: changes will be removed from the working directory and from the index, you will lose all modifications.

git revert HEAD

In order to revert the last Git commit, use the “git revert” and specify the commit to be reverted which is “HEAD” for the last commit of your history.

#revise the commit history

if you have not yet push the changes to remote, we can use git commit -amend

if you have been push to remote:

git rebase -i HEAD~[numbers of your commits], and then a editor will be prompt, you can select which one is to pick, which one is to squash

after rebase, git push original remotebranch --force  : the force is mandatory.

 

#git list the untracked files

git ls-files . --exclude-standard --others

 

#git list the files in gitignore

git ls-files . --ignored --exclude-standard --other

 

#git store the username and password of remote

git config --global credential.helper store

next time, when you input the username and password, it will store the username and password in the file ~/.git-credentials in plain text.

 

#error You need to resolve your current index first

root cause:

  • A merge failed and you need to address the merge conflict before moving on with other tasks.
  • There are conflicts in the files at your current (or targeted branch) and because of these conflicts, you will not be able to check out of a branch or push code

 solve:

method 1) : commit the changes if those files are really needed.

method 2) : discard it : git reset --merge

 #how to move the changes at master branch to a new branch

I made a mistake that I changed the source code at master branch, so I want to move to a new branch with the modificatons. The commands as below.

git stash
git checkout master
git checkout -b "New_branch"
git stash apply

 #how to reset a file changed to master

git checkout origin/master [filename]

#squashing commit

there are multiply commits before push to master, so it is better to squash the commits to one.

git log --one-line

 

git rebase -i <commit_id>

you need to select the commits for squash except the first one.

 

git rebase -i <that_commit_id>
871adf OK, feature Z is fully implemented --- newer commit --┐
0c3317 Whoops, not yet... |
87871a I'm ready! |
643d0e Code cleanup |-- Join these into one
afb581 Fix this and that |
4e9baa Cool implementation |
d94e78 Prepare the workbench for feature Z -------------------┘
6394dc Feature Y --- older commit

git rebase --interactive 6394dc

 #rename a branch

git branch - m old_name new_name

#delete a remote branch

 git push origin --delete <BranchName>

delete a local branch 

git branch -d the_local_branch

#git export source

git archive --format tar.gz --output "./output.tar.gz" master

#git export/apply commit patch

try:

git format-patch -1 <sha>

or

git format-patch -1 HEAD

According to the documentation link above, the -1 flag tells Git how many commits should be included in the patch;

-<n>

Prepare patches from the topmost commits.

Apply the patch with the command:

git am < file.patch

https://devconnected.com/how-to-create-and-apply-git-patch-files/

 

posted on 2019-06-27 14:18  荷树栋  阅读(269)  评论(0)    收藏  举报

导航