每一个错误的经验积累,就是通向成功的阶梯。
Each mistake I made shall become one of the stairs towards success.
Showing posts with label Git. Show all posts
Showing posts with label Git. Show all posts
Friday, 24 July 2020
To convert a bare repo to a normal one
To convert a bare repo to a normal one:
$ git clone -l <path_to_bare_repo> <new_normal_repo>
Thursday, 26 April 2018
Move a tag on a git Branch to a Different Commit
You might forget to do something before you tag a commit. Then you realize OMG I gotta make that small change or delete something etc. You would have a new commit after the tag you've just made so you want to move the tag to this new commit. Here is how.
ref:
https://stackoverflow.com/questions/8044583/how-can-i-move-a-tag-on-a-git-branch-to-a-different-commit
- Delete the tag on any remote before you push
git push origin :refs/tags/<tagname>
- Replace the tag to reference the most recent commit
git tag -fa <tagname>
- Push the tag to the remote origin
git push --tags
ref:
https://stackoverflow.com/questions/8044583/how-can-i-move-a-tag-on-a-git-branch-to-a-different-commit
Thursday, 8 February 2018
Git delete tag locally and remotely
To delete a local tag:
git tag -d tag-name
To delete a remote tag:
git tag -d tag-name // first: delete it locally
git push origin :refs/tags/tag-name // then push the empty tag to origin
ref:
http://www.manikrathee.com/how-to-delete-a-tag-in-git.html
Sunday, 7 January 2018
Discard unstaged changes in Git
There could be times that you just temporarily tried something, or showing other people something and you have some garbage code.
When using Git, it is common to make changes that you want to remove entirely before the staging phase. For example, after working on a few files, you realize that you want to revert the changes made to one specific file. To discard the changes before staging and committing, use the $git checkout command.
To unstage one file :
$ git checkout <path-to-file>
Remember to replace <path-to-file> with the actual file name.
When using Git, it is common to make changes that you want to remove entirely before the staging phase. For example, after working on a few files, you realize that you want to revert the changes made to one specific file. To discard the changes before staging and committing, use the $git checkout command.
To unstage one file :
$ git checkout <path-to-file>
Remember to replace <path-to-file> with the actual file name.
To unstage all files:
$ git checkout -- .
ref:
https://forum.freecodecamp.org/t/discard-unstaged-changes-in-git/13214
$ git checkout -- .
ref:
https://forum.freecodecamp.org/t/discard-unstaged-changes-in-git/13214
Friday, 24 November 2017
Create a New Branch from a History Commit
When we want to checkout a new branch we do:
This is actually shorten for:
git checkout -b name-of-new-branch
git checkout -b name-of-new-branch current-branch
That is to say, if we don't specify the starting point of this new branch, it starts by default from the current active branch. Since every commit has an SHA1 (Hash value) as its ID, we can use these IDs as the start pointer when we are using checkout command. For example:
git checkout -b name-of-new-branch 169d2dc
In this way, the active branch is now switched to this new branch and things are the same with branch 169d2dc.
Note that we might need to use the long full SHA1 ID in case the short one conflicts with others.
ref:
https://liam0205.me/2015/04/29/git-checkout-history-version/
Note that we might need to use the long full SHA1 ID in case the short one conflicts with others.
ref:
https://liam0205.me/2015/04/29/git-checkout-history-version/
Git Workflow for Embedded Systems
The special thing about embedded system is that it sits in between software and hardware. Therefore, you might hit walls if adopting the traditional git workflow. The following blog specified the obstacles really well:
https://medium.com/jumperiot/how-to-use-git-flow-in-embedded-software-development-dbb2a78da413
I met the problem of different hardware configurations and I have to go back to history versions to branch out and do some redundant work. But most importantly, keep the following three things in mind:
(1) Split the code base into unrelated libraries/modules that support different configurations, manage them separately and then do a configuration management. Note that you’ll need to invest in proper software architecture and abstraction layers.
https://medium.com/jumperiot/how-to-use-git-flow-in-embedded-software-development-dbb2a78da413
I met the problem of different hardware configurations and I have to go back to history versions to branch out and do some redundant work. But most importantly, keep the following three things in mind:
(1) Split the code base into unrelated libraries/modules that support different configurations, manage them separately and then do a configuration management. Note that you’ll need to invest in proper software architecture and abstraction layers.
(2) Control different configuration with features flags on the same branches.
(3) Create isolated and long lived branches for each version/hardware configuration.
ref:
https://medium.com/jumperiot/how-to-use-git-flow-in-embedded-software-development-dbb2a78da413
https://liam0205.me/2015/04/29/git-checkout-history-version/
ref:
https://medium.com/jumperiot/how-to-use-git-flow-in-embedded-software-development-dbb2a78da413
https://liam0205.me/2015/04/29/git-checkout-history-version/
Wednesday, 18 October 2017
You really count on using things like GitKraken in the Server? Git itself is powerful enough:
https://stackoverflow.com/questions/1838873/visualizing-branch-topology-in-git
git log --graph --full-history --all --color \
--pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"
ref:https://stackoverflow.com/questions/1838873/visualizing-branch-topology-in-git
Wednesday, 15 March 2017
The branch 'xxxx' is not fully merged.
The error code says:
This worked for me:
warning: not deleting branch 'second' that is not yet merged to
'refs/remotes/origin/second', even though it is merged to HEAD.
error: The branch 'second' is not fully merged.
If you are sure you want to delete it, run 'git branch -D second'.
Force deleting is never a good idea.
It seems that git just wanted me to push my changes to the second branch before deleting it.
This worked for me:
$ git checkout second
$ git push origin second
$ git checkout first
$ git branch -d second
ref:
http://stackoverflow.com/questions/12495756/why-doesnt-git-allow-me-to-safely-delete-a-branch/12520718
How to Pull Remote Branches which Don't Exist Locally
git pull can do some tricks.
More detailed operation:
git checkout -b new_branch origin/new_branch
ref:
https://segmentfault.com/q/1010000000367843
More detailed operation:
git checkout -b new_branch origin/new_branch
ref:
https://segmentfault.com/q/1010000000367843
Sunday, 26 February 2017
Sync Remote Deleted Branches
I finished a feature on Friday and deleted the feature_xxx branch on bitbucket (remote) and my office computer (local). But I want to continue working during the weekend at home with my home computer. I find that the feature_xxx branch is still shown on my home computer, remotely and locally, which means if I do: git branch -a, I get:
develop
* feature_xxx
master
remotes/origin/develop
remotes/origin/feature_xxx
remotes/origin/master
Got:
Pruning origin
URL: https://boris@bitbucket.org/xxx/yyy.git
* [pruned] origin/feature_xxx
step 2:
Now that the deleted remote branch has been synchronized, we can delete the local branch which doesn't exist anymore.
git branch -d feature_xxx
Note that
(1) you might need to checkout to another branch before deleting it (eg develop).
git checkout develop
(2) you might also get:
error: The branch 'feature_xxx' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature_xxx'.
This is because the local didn't have the updated data yet. Just do:
git pull
Finally, check it out: git branch -a
Got:
* develop
master
remotes/origin/develop
remotes/origin/master
ref:
https://higoge.github.io/2015/07/07/git-remote05/
develop
* feature_xxx
master
remotes/origin/develop
remotes/origin/feature_xxx
remotes/origin/master
Of course, if I login my bitbucket with a webpage, I could see there are only develop and master left.
How could I keep up to date?
I tried git pull, things seem to be updated with a bunch of green +++++s and red ------s but still showing the feature_xxx branch.
The reason is: the branches tracked by remotes/origin/* are only a cache of the remote server, and the deleted branch cannot be updated by git fetch.
///////////////////////////////////////////////////////////////////////////////////////////
Solution:
step 0:
git remote show origin
Got:
* remote origin
Fetch URL: https://boris@bitbucket.org/xxx/yyy.git
Push URL: https://boris@bitbucket.org/xxx/yyy.git
HEAD branch: master
Remote branches:
develop tracked
master tracked
refs/remotes/origin/feature_xxx stale (use 'git remote prune' to remove)
Local branches configured for 'git pull':
develop merges with remote develop
feature_xxx merges with remote feature_xxx
master merges with remote master
Local refs configured for 'git push':
develop pushes to develop (up to date)
master pushes to master (up to date)
step 1:
git remote prune origin
Got:
Pruning origin
URL: https://boris@bitbucket.org/xxx/yyy.git
* [pruned] origin/feature_xxx
step 2:
Now that the deleted remote branch has been synchronized, we can delete the local branch which doesn't exist anymore.
git branch -d feature_xxx
Note that
(1) you might need to checkout to another branch before deleting it (eg develop).
git checkout develop
(2) you might also get:
error: The branch 'feature_xxx' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature_xxx'.
This is because the local didn't have the updated data yet. Just do:
git pull
Finally, check it out: git branch -a
Got:
* develop
master
remotes/origin/develop
remotes/origin/master
ref:
https://higoge.github.io/2015/07/07/git-remote05/
Tuesday, 14 February 2017
Practical Stuff: Git Branches and Git Flow
Nothing fancy, just my daily mostly used commands.
a. create develop branch
git branch develop
git push -u origin develop
b. create a new Feature branch
git checkout -b some-feature develop
# Optionally, push branch to origin:
git push -u origin some-feature
# make some changes...
git status
git add some-file
git commit
c. Finish a working Feature
git pull origin develop
git checkout develop
git merge --no-ff some-feature
git push origin develop
git branch -d some-feature
# If you pushed branch to origin:
git push origin --delete some-feature
d. To Relase
git checkout -b release-0.1.0 develop
# Optional: Bump version number, commit
# Prepare release, commit
e. Finish a Release
git checkout master
git merge --no-ff release-0.1.0
git push
git checkout develop
git merge --no-ff release-0.1.0
git push
git branch -d release-0.1.0
# If you pushed branch to origin:
git push origin --delete release-0.1.0
git tag -a v0.1.0 master
git push --tags
f. To make a Hotfix
git checkout -b hotfix-0.1.1 master
g. Finish a Hotfix
git checkout master
git merge --no-ff hotfix-0.1.1
git push
git checkout develop
git merge --no-ff hotfix-0.1.1
git push
git branch -d hotfix-0.1.1
git tag -a v0.1.1 master
git push --tags
# if later, a tagged version wants to be checked out.
git checkout v0.1.1
# if want to check the information in a tag
git cat-file tag <tagname>
h. Stash
git stash
git stash list
git stash apply
git stash apply stash@{2}
i. Undo a merge
git checkout the branch you were on when merging
git stash apply stash@{2}
i. Undo a merge
git checkout the branch you were on when merging
git reset --hard
j. dump current local changes
git checkout .
k. Your PM created a branch in the Remote Server, now you need to work on it. Or upon pull request, don't pull (fetch + merge) but fetch first.
git fetch origin SOF-123-new-branch:SOF-123-new-branch
l. wanna see the diff?
$ git diff tag1 tag2
or show log between them:
git checkout .
k. Your PM created a branch in the Remote Server, now you need to work on it. Or upon pull request, don't pull (fetch + merge) but fetch first.
git fetch origin SOF-123-new-branch:SOF-123-new-branch
l. wanna see the diff?
$ git diff tag1 tag2
or show log between them:
$ git log tag1..tag2
sometimes it may be convenient to see only the list of files that were changed:
sometimes it may be convenient to see only the list of files that were changed:
$ git diff tag1 tag2 --stat
and then look at the differences for some particular file:
and then look at the differences for some particular file:
$ git diff tag1 tag2 -- some/file/name
diff from a branch to a tag:
- switch to that branch (checkout)
- $ git diff tag-v1.0.0
diff from a branch to a tag:
- switch to that branch (checkout)
- $ git diff tag-v1.0.0
ref:
http://www.codeceo.com/article/how-to-use-git-flow.html !!!!!!!!!!!
https://higoge.github.io/2015/07/07/git-basic06/ !!!!!!!!!!!!!!!!!
http://nvie.com/posts/a-successful-git-branching-model/ !!!!!!!!
https://github.com/geeeeeeeeek/git-recipes/wiki !!!!
https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6
https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E5%BC%80%E5%8F%91%E5%B7%A5%E4%BD%9C%E6%B5%81
https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E5%82%A8%E8%97%8F%E4%B8%8E%E6%B8%85%E7%90%86
https://segmentfault.com/q/1010000000140446
http://blog.csdn.net/leedaning/article/details/51304690
http://xigua366.iteye.com/blog/2400324
https://www.oschina.net/translate/git-fetch-and-merge?print
https://stackoverflow.com/questions/3211809/git-diff-between-given-two-tags
https://stackoverflow.com/questions/4185888/how-do-i-read-tagger-information-from-a-git-tag
Monday, 13 February 2017
Start a Empty Branch in Git
We are trying to create an EMPTY branch called gh-pages:
$cd repo
$ git checkout --orphan gh-pages
# create an orphan branch and it is independent
Switched to a new branch 'gh-pages'
git rm -rf .
# delete all the files under the original source tree
rm '.gitignore'
Note that you won't see the current new branch if you do git branch until the first commit happens. So let's now add something.
$ echo \"My GitHub Page\" > index.html
$ git add .
$ git commit -a -m \"First pages commit\"
$ git push origin gh-pages
After commit, we can now see the new branch with git branch and we can push it to the remote repo.
ref:
http://skyao.github.io/2015/03/18/git-create-empty-branch/
How to Solve CONFLICT When Trying to merge branches
We would like to merge branch issue2 and branch issue3 to master.
So first, switch to master and merge issue2:
$ git checkout master Switched to branch 'master' $ git merge issue2 Updating b2b23c4..8f7aa27 Fast-forward myfile.txt | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
This is fast-forward merge.

Now let's merge issue3:
$ git merge issue3 Auto-merging myfile.txt CONFLICT (content): Merge conflict in myfile.txt Automatic merge failed; fix conflicts and then commit the result.
We can see the automatical merge failed because there is conflict on the same lines from two different branches. And we can see the conflict files become:
old stuff add some new stuff <<<<<<< HEAD did something before ======= pull something from some one else >>>>>>> issue3Git has generated the differences where the conflicts happened. We must change the file to:
old stuff add some new stuff did something before pull something from some one elseBasically, manually fix the conflict and commit again:
$ git add myfile.txt $ git commit -m "merge issue3" # On branch master nothing to commit (working directory clean)
We can see this is not fast-forward but non fast-forward.

ref:
http://backlogtool.com/git-guide/cn/stepup/stepup2_7.html
Saturday, 14 January 2017
How to Use diff and patch to Compare and Merge Things/Versions
I was working on a few test branch versions this week and I've discovered some bugs. When I want to return to the developing branch versions, I'd like to find out what did I do to fix the bugs and patch them into the developing versions. Therefore, I need to use diff and patch to do that.
(1) diff
Suppose I am comparing directory/folder/file A and B:
diff -c -a -b -x *.tag -x *.log A B > AB.diffexplanation:
> -c output difference with content format
> -a compulsory text mode
> -b ignore blank difference
> -x filter the files don't want to be compared (more patterns apply)
(2) patch
to be continue...
ref:
http://blog.csdn.net/magicpang/article/details/3030089
(1) diff
Suppose I am comparing directory/folder/file A and B:
diff -c -a -b -x *.tag -x *.log A B > AB.diffexplanation:
> -c output difference with content format
> -a compulsory text mode
> -b ignore blank difference
> -x filter the files don't want to be compared (more patterns apply)
(2) patch
to be continue...
ref:
http://blog.csdn.net/magicpang/article/details/3030089
Monday, 28 November 2016
Bitbucket Daily Use
Daily Use:
git status
git branch
git add -A // if you put something inside a sub-directory "git add ." won't detect
////////////////////////////////////////////////////////////////////////////////////////////////////////
Troubleshoots:
(0)
error: failed to push some refs to 'https://xxxxx@bitbucket.org/yyyyyyy/zzzzzzzz.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
Force Push...
////////////////////////////////////////////////////////////////////////////////////////////////////////////
ref:
https://www.douban.com/note/525306465/
http://stackoverflow.com/questions/20939648/issue-pushing-new-code-in-github
Subscribe to:
Posts (Atom)