what I want is quite simple, I think.. (?) - We are using commitizen as commit messages formating tool.
And, using husky for git hooks.
I would like to add a hook for editing the commit messages (with nodejs), as follows:
Now, let's say someone created this commit message:
fix(prepare-commit-msg) adding ticket number as prefix to this commit from branch name
Our branch's naming convention looks something like that:
userName2424/sc-27172/and-this-is-the-ticket-title
The output for the commit I want to achieve is:
27172: fix(prepare-commit-msg) adding ticket number as prefix to this commit from branch name
Thanks in advance
Related
For a nodejs command-line tool I add an empty commit to a repo and then want to remove it later.
Later I have at least 3 commits. The first one is a merge commit, the second one is the empty one I created and the third one is likely one from another now merged repo. Now that my tool has done it's task I want to remove the empty commit.
git rebase --onto emptyCommitID^ emptyCommitID
resulted in: fatal: Does not point to a valid commit 'emptyCommitID^'
(since the ID is the correct one I assume the commit is invalid due to it being empty)
git rebase --keep-base --onto thirdCommit^ headCommit
resulted in fatal: cannot combine '--keep-base' with '--onto'
trying rebase -i HEAD~3 after the tool had done it's main job resulted in:fatal: invalid upstream 'HEAD~3' which might be due to either the empty commit or the merged unrelated histories idk.
I do not want to use git filter-branch --prune-empty, because the tool shall leave other potentially empty commits untouched.
(The tool is for merging repos with unrelated histories. I create the empty commit so that files are staged when merged instead of committed directly which also happens when the --no-commit flag is set in an just initialized repo without prior commits)
maybe it is possible to solve this with git rebase --interactive, but I had the described problem with the invalid upstream and view this as very difficult to implement with a command line tool, mostly using exec to execute it's commands. Do you know a solution?
I think git rebase --onto emptyCommitID^ emptyCommitID should work.
fatal: Does not point to a valid commit 'emptyCommitID^' means that the emptyCommitID has no parent. It contradicts that the second one is the empty one and the first one is its parent.
I use GitHub in my project in Android Studio 3.3.1.
I write code in local master branch and push them to remote origin/master.
The Submit_3 Image is my latest work and submit result snapshot.
I find the code after Submit_1 Image snapshot are wrong, so I hope to return Submit_1 and write new code. I hope that I can do the new work still on master branch, how can I do?
BTW, at present I use Checkout Revision command to return to Submit_1 (see Current Image), and create a temp branch for Submit_1, then switch to master branch and merge temp branch.
But I find the result just like Result Image is the same Submit_3 Image, why?
Submit_3 Image
Submit_1 Image
Current Image
Result Image
The reason you have the same result after you merge temp is because you essentially merge what is in master (which includes the wrong thing) and the one in temp; thus, having both the wrong and correct code.
Solution to your initial problem:
git checkout master => Goes to master branch
git log -n [some number] => Shows you the list of commit messages; eg. git log -n 5 will show you last 5 commit messages.
Find the commit number of the commit you want to return to. Copy it.
git reset --hard [commit number here] => This will return master to be where you chose in your commit number. Be careful, this will erase everything that came after that commit.
I don't recall if you need to git push or it does it for you. Check by doing git status
Continue writing your code. If you have new stuff in temp, at this point, if you do git merge temp. It will get your new stuff and not have the wrong code that was in master before. If anything goes wrong, do what I described above to return to any commit.
Good luck!
Also, official doc here: https://git-scm.com/docs/git-reset
I am not sure I understand your problem but I think what you want is Reset Current Branch to Here.... This will delete Submit 2 and Submit 3 and go back in time to Submit 1. You will have in your tree:
Submit 1 (master)
Initial Commit
Then, to make this real in GitHub you need to Push Force when you push your changes. And the final tree will be:
Submit 1 (origin & master)
Initial Commit
PLEASE do this carefully. After you push force, Submit 2 and Submit 3 will be permanently deleted.
If often find myself writing out helpful messages in my code when developing locally. See this code snippet as a simple example:
# Public API
namespace :api do
namespace :v1 do
# REMOVE THE FIRST LINE WHEN DONE TESTING
get :delivery_report, to: 'delivery_report#index'
post :delivery_report, to: 'delivery_report#index'
end
end
The point here is that when I am done and I feel like I can finally commit my work I need to remember that I have to remove the line get :delivery_report, to: 'delivery_report#index'
Is it somehow possible to tell git that before accepting a commit or maybe before staging it, it should warn me about a line in my code that has some content? In this case that would be my comment or any other pre-defined line.
Git supports a "pre-commit hook", in which you can test what the user has proposed committing, and force the commit to fail if some condition(s) apply.
See git pre-commit hook code formatting with partial commit? and How do I properly git stash/pop in pre-commit hooks to get a clean working tree for tests?
I have the following setup. I want to check that commits have a certain commit message format (line limit, etc.). I want this for all repos, but one or two.
How can I tell this gitolite without having to define a group #check_msg that contains most repos. This would be cumbersome...
In my BADCOMMIT VREF, I already included some lines to not check the message if it is called with NOCHECK.
Any ideas??
repo testing
RW+ = #all
? VREF/BADCOMMIT/NOCHECK = #all
repo #all
- VREF/BADCOMMIT = #all
repo gitolite-admin
RW...
Instead of using git notes, I suggest you do this:
repo testing
RW+ = #all
option badcommit.nocheck = 1
repo #all
- VREF/BADCOMMIT = #all
Then, at the start of the VREF, you say
gitolite git-config -q $GL_REPO gitolite-options.badcommit.nocheck && exit 0
Much cleaner, and the knowledge of the exceptions stays within gitolite.conf.
(Assuming v3 of course).
Considering:
that a VREF has no idea of the git repo name or path it is called from, adding:
fallthrough means success (which means, as matt comments, that it will check all the VREFS)
The only solution seems for the VREF script "BADCOMMIT" to somehow detects it is in a git repo it should not test.
If, by convention, said git repo were to contain a **git notes (**which you can add at any time, without changing the git history, see man git notes) with a certain title on this note, it could decide to not go on with its check.
Since there are few git repos which shouldn't be tested with BADCOMMIT, you could add a note to the first commit of that repo.
That way, you don't need a NOCHECK, and you don't need to stop after a certain rule.
I have very poor knowledge about git and would like to ask for help.
I have a linux(-only) application which shall only be "downloaded" (i.e. cloned) with git. On startup, the app shall ask the git "master server" (github) for whether there are updates.
Does git offer a command to check for whether there is an update (without really updating - only checking)? Furthermore, can my app read the return value of that command?
If you do not want to merge, you can just git fetch yourremote/yourbranch, the remote/branch specification usually being origin/master. You could then parse the output of the command to see if new commits are actually present. You can refer to the latest fetched commit as either yourremote/yourbranch or possibly by the symref FETCH_HEAD.
Note: I was reminded that FETCH_HEAD refers to the last branch that was fetched. Hence in general you cannot rely on git fetch yourremote with FETCH_HEAD since the former fetches all tracked branches, thus the latter may not refer to yourbranch. Additionally,
you end up fetching more than strictly necessary.
also refer to Jefromi's answer to view but not actually downloaded changes
the following are not necessarily the most compact formats, just readable examples.
That being said, here are some options for checking for updates of a remote branch, which we will denote with yourremote/yourbranch:
0. Handling errors in the following operations:
0.1 If you attempt to git fetch yourremote, and git gives you an error like
conq: repository does not exist.
that probably means you don't have that remote-string defined. Check your defined remote-strings with git remote --verbose, then git remote add yourremote yourremoteURI as needed.
0.2 If git gives you an error like
fatal: ambiguous argument 'yourremote/yourbranch': unknown revision or path not in the working tree.
that probably means you don't have yourremote/yourbranch locally. I'll leave it to someone more knowledgeable to explain what it means to have something remote locally :-) but will say here only that you should be able to fix that error with
git fetch yourremote
after which you should be able to repeat your desired command successfully. (Provided you have defined git remote yourremote correctly: see previous item.)
1. If you need detailed information, git show yourremote/yourbranch and compare it to the current git show yourbranch
2. If you only want to see the differences, git diff yourbranch yourremote/yourbranch
3. If you prefer to make comparisons on the hash only, compare git rev-parse yourremote/yourbranch to git rev-parse yourbranch
4. If you want to use the log to backtrack what happened, you can do something like git log --pretty=oneline yourremote/yourbranch...yourbranch (note use of three dots).
If you really don't want to actually use bandwidth and fetch new commits, but just check whether there is anything to fetch, you can use:
git fetch --dry-run [remote]
where [remote] defaults to origin. You'll have to parse the output, though, which looks something like this:
From git://git.kernel.org/pub/scm/git/git
2e49dab..7f41b6b master -> origin/master
so it's really much easier to just fetch everything (git fetch [remote]), and then look at the diff/log e.g. between master and [remote]/master.
I'd say git fetch is a potential solution. It only updates the index, not working code. In cases of large commit sets, this would involve a download of compressed files/info, so it may be more than you want, but it is the most useful download you can do.