I want to print the latest commit hash in my Node.js application.
I found a similar discussion here, but it's not what I have in mind. I want to read this information from within Node.js, without running git commands in a terminal.
So if possible, I want to read the .git folder with fs, find the latest commit hash and write it to a variable. This file looks relevant: .git/logs/refs/heads/master.
Can I simply read the last non-empty line of this file and parse the commit hash from there? Any pitfalls I should look for? Any better ways? It's safe to assume I have only one branch (master)
Thanks for any help!
To get the hash for the current (pointed to by the HEAD) commit:
git rev-parse HEAD
To get the hash for the last commit on master branch (in case you moved HEAD to a different commit using git checkout or git switch):
git rev-parse master
Related
I have multiple branches which are branched off the master (each in a separate subdirectory).
Branch1: new development, not yet completely finished
Branch2: hotfix for a problem, but still under test
Branch3: mess around branch, which I will not restore
Before testing of the hotfix is finished I would like to have the code already available in Branch1, so I can continue developing with the fix in place.
(But since my experience with git is not that much I first started to play around with merge in a 3rd branch, especially created to mess around in, before I mess up either Branch1 or Branch2)
In my 3rd branch I first tried the following:
git merge feature/Branch1
but this gave the following error:
fatal: 'feature/Branch1' does not point to a commit
I next did a commit -a in my Branch1 and tried again, but it keeps giving the same error.
What am I doing wrong? What should I do to merge the code from - in this case - Branch1 with Branch3?
First, checkout to your Branch3:
git checkout Branch3
Then merge the Branch1:
git merge Branch1
And if you want the updated commits of Branch1 on Branch2, you are probaly looking for git rebase
git checkout Branch2
git rebase Branch1
This will update your Branch2 with the latest updates of Branch1.
git checkout [branchYouWantToReceiveBranch] //checkout branch to receive branch
git merge [branchYouWantToMergeIntoBranch]
You can use these commands:
git checkout <the branch you want to merge into>
git merge <the branch you want contents from>
To merge one branch into another, such as merging "feature_x" branch into "master"* branch:
git checkout master
git merge feature_x
* Note that the original branch name is now commonly main instead of master. Choose the correct name based on your situation.
This page is the first result for several search engines when looking for "git merge one branch into another". However, the original question is more specific and special case than the title would suggest.
It is also more complex than both the subject and the search expression. As such, this is a minimal but explanatory answer for the benefit of most visitors.
Just in case you arrived here because you copied a branch name from Github, note that a remote branch is not automatically also a local branch, so a merge will not work and give the "not something we can merge" error.
In that case, you have two options:
git checkout [branchYouWantToMergeInto]
git merge origin/[branchYouWantToMerge]
or
# this creates a local branch
git checkout [branchYouWantToMerge]
git checkout [branchYouWantToMergeInto]
git merge [branchYouWantToMerge]
A slightly more long winded approach, but it works none-the-less:
In branch 3:
git fetch origin Branch1
git merge --no-ff origin/Branch1
At this point merge conflicts may occur, save all changes made to files containing merge conflicts
git add -A
git commit -m "Merge"
git push
DONE
I am using the git mergetool command to fix conflicts. However I have thousands of conflicts, is there way to simplify this so I get everything from the remote?
I am asked to enter c, d or a in the command.
{local}: deleted
{remote}: created file
Use (c)reated or (d)eleted file, or (a)bort?
Since I have thousands of files, I don't want to keep sending c. Is there way to just do this in bulk?
You can solve this outside of git mergetool: run git status --porcelain to get a list of all unmerged files and their states in machine-readable format.
If your Git is new enough, it will support --porcelain=v2. See the git status documentation for details on the output formats. Output format v2 is generally superior for all purposes, but you should be able to make do with either one.
Next, you must write a program. Unfortunately Git has no supplied programs for this. Your program can be fairly simple depending on the specific cases you want to solve, and you can use shell scripting (sh or bash) as the programming language, to keep it easy.
Since you're concerned about the cases where git mergetool says:
Use (m)odified or (d)eleted file, or (a)bort?
you are interested in those cases where the file name is missing in the stage 1 ("base") version and also missing in the stage 2 ("local") version, but exists in the stage 3 ("remote") version. (See the git status documentation again and look at examples of your git status --porcelain=v2 output to see how to detect these cases. Two of the three modes will be zero.) For those particular path names, simply run git add on the path name to mark the file as resolved in favor of the created file.
Once you have marked all such files, you can go back to running git mergetool to resolve additional conflicts, if there are any.
Note that your "program" can consist of running:
git status --porcelain=v2 > /tmp/commands.sh
and then editing /tmp/commands.sh to delete all but the lines containing files that you want to git add. Then change all of those lines to read git add <filename> where <filename> is the name of the file. Exit the editor and run sh /tmp/commands.sh to execute all the git add commands. That's your program!
supposing you want their change and modified yours you can do a pull as like:
git pull -X theirs
Other stackOverflow answers
git pull -X
git merge strategies this link will help understand any other merge strategies for the futuro
If you want that all the change you did will be deleted and you will be sync with the remote.
You should do the following:
git stash
git pull
And if you want to restore the change you did you should type:
git stash pop
Basically 'git stash' is moving the change to a temp repository.
you can learn more in:
NDP software:: Git Cheatsheet
I have a question about git, I tried to clone a tree but without success.
git clone https://github.com/cer/event-sourcing-examples/tree/d2077e21aa677a00095f90250470ff011c132ab8/java-spring
I cloned the project
git clone https://github.com/cer/event-sourcing-examples
and I tried to switch to that tree but no effect
Would you have any suggestions ?
Best regards
Git cannot clone a tree directly. You need to clone the entire repository, and then check out a commit that uses the tree you want. For the sake of reducing confusions, though, do note that there is a difference between the terms "tree" and "commit", though:
A tree is a Git object representing a directory, and contains links to blobs (files) and other trees. A tree is not necessarily the root directory of the repository.
A commit object contains a link to the root tree of the repository, and some extra information such as commit message, dates and other headers.
You can only check out commits. Few Git commands deal directly with tree objects (git cat-file and git ls-tree being among the exceptions). However, the object ID in your GitHub URL is indeed the ID of a commit, so that's not a problem.
What you can do, then, is check out the commit you want into a new branch after you've cloned the repository:
git checkout -b test-branch d2077e21
If the problem you're trying to solve is just fetching a single commit (or tree) from a remote repository, then you're out of luck, because Git's remote protocol does not support that operation. If anything, if you can insert a branch into the remote repository at the commit you want, you can clone that branch directly, without any history:
git clone -b test-branch --depth 1 https://github.com/cer/event-sourcing-examples
If you can't do that, however, then you're still out of luck. The remote protocol only allows referencing named refs, not arbitrary commits.
Check if below things helps.Am using a GIT bash here.
Clone the repository.
git clone https://github.com/cer/event-sourcing-examples.git
Enter that directory
cd event-sourcing-examples/
Switch the branch(i am assuming by tree you mean branch)
git checkout wip-vagrant wip-vagrant is a branch name
To get the update you have to issue a pull command.
git pull
If you directly want to clone the branch then follow the instructions in above comment(Micheal).
git clone -b <branch> <remote_repo>
Example:
git clone -b my-branch git#github.com:user/myproject.git
Alternative (no public key setup needed):
git clone -b my-branch https://git#github.com/username/myproject.git
If your goal is just to get a copy of the repo at a particular commit...
While you can't use clone, you can download a zip file of the repo at a particular commit.
This method works on GitHub.
This and other approaches can be found at:
https://coderwall.com/p/xyuoza/git-cloning-specific-commits
TL;DR
Navigate to the tree view of the sha you want.
https://github.com/<repo_name>/tree/<commit_sha>
Download the zip file.
Don't clone.
Github Tree View
Open the repo and click the "commits" link
(in the bar that says "commits branches packages, etc.)
Select the commit you want. This will take you to the view showing the changes.
In the url you will see something like this:
https://github.com/Colt/webpack-demo-app/commit/eb66c0dc93141080f5b1abb335ec998a1e91d72e
- Note the sha in the url is preceeded by the word "commit".
Replace the word "commit" with the word "tree" to put yourself in the
tree view.
- Finally, click on the green "Clone or download" button
and Download the ZIP. Don't try to clone.
This will download the entire repo as it was at that commit.
First, you need to get the complete repo and get checkout the repo to commit_sha.
git clone -n <repo_name>
git checkout <commit_sha>
I am working on project, in which a branch say feature/test is used by N number of developers. All developers currently commit their changes and push to same branch.
Now i want to find all files on which i have worked in branch feature/test.
Thanks in advance.
git log --stat --name-only --author="your_name" --pretty=format:"" --branches=your_branch|sort -u
Source - old question
I am having a situation here. I did some commit to master branch and after move these commit to
other branch x using and
git cherry-pick
and I run git cherry-pick on x branch itself. Now after this git cherry-pick, forgot to run git push command.
Almost at same time my colleague did the git cherry-pick for his commits and did the git push.
When I see the git log on his machine I couldn't find my commit and realize that I forgot to run git push.
I went to my machine, did the git push. But I am seeing this message now
Without a better log output it is hard to tell, what happend.
I guess you both had the same base commit, each of you did a cherry-pick, and you merged both cherry-picks in order to be able to push it.
The log above shows one commit from you (cf5b726), one commit from your colleague (1aa2ecd) and a merge (28529d0) of the first two commits, which seems to originate from a git pull.
And yes, the merge carries the changes of both merged commits. - After all that's exactly the point of a merge. ;)
You can use git log --graph or git log --format=raw (or magit for Emacs) to see the parents of a commit.