After GIT commit, I did GIT push but the message said everything up-to-date - linux

I'm new to Git. We're in Linux, and I just inherited someone's project family.
I recently did:
git add Bom.xml Bom.csv N.cpp makefile ../mm
git commit -a
(said On branch Bom, your branch is ahead of master by 2 commits. use git push to publish your local commits. Untracked files: list of things I don't want to commit anyway). Nothing added to commit but untracked files present.
then
git push
But it said everything up-to-date. I'm not sure where to look to see that my content is pushed up to my branch on the server. I know I had file changes since my last commit. It's a tough thing to search for the answer online. I looked at up-to-date too, and added the git add and the -a to my commit, but it still says up-to-date when I try to push.
Thanks!
Mich

Like the link you mentioned, make sure you have added the file you want to commit into the staging area. Each time you want to commit
run git status to check.
And then run git add file to add file to staging area.
Run git status to check whether the file is added to the stage.
Then run git commit -m "some message" to commit
run git log to check your commit history check whether you have committed successfully
then check your remote branch by running git branch -a
if your remote branch doesn't have your local branch branch-name
then run git push origin branch-name to push your local branch to remote.

Related

problem with my git repo, I lost all my commits?

I was trying to upload a file via terminal.I am trying to learn git.
**
git add /Users/serra/Documents/useSDWebImage
git commit -m "learning on process"
git remote add origin
git push -u origin master
**
then I got this message error: failed to push some refs to ....
I tried those commands then I have only the file I want to upload
to repository I have lost rest of the repository.What should I do?
**
git pull --rebase origin main
git push origin main
git pull origin master
git push origin master
git push origin master --force
**
TL;DR
You need to find a backup, somewhere, of origin/master.
git reflog might help you.
The error
First, let's go over your first four commands and explain what the mistake was.
In the first two commands, a git add and git commit, you are creating a commit in your local sandbox, building on whatever was in your sandbox beforehand.
In the third command, you add your remote, which means that your commit was not made on top of that remote, but on top of something else.
In your fourth command, Git correctly tells you that you cannot push, because your local sandbox is not built on top of the remote. It's important to pay attention to Git's error messages, they're almost always informative.
Now, you're not showing outputs from any of the commands you ran, but I'm guessing git push origin master --force is what deleted stuff on your remote.
What you should have done
Normally, you clone the remote first, then commit stuff on top of it:
git clone <URL>
cd <SANDBOX_DIR>
git add <file inside this directory>
git commit
git push
How to fix this
There are several ways, but they're all based on recovering stuff from a backup that hopefully exists somewhere.
If your remote is GitHub, it keeps a history of references, you should be able to find that old master branch somehow there. If your remote is some other server, that is hopefully true too. If another team member has a sandbox that had the valid master you want to restore, they can fix things by doing a git push origin master --force in their own sandbox, but make sure they understand what they're doing before they do it.
My answer here is not complete, because you didn't provide enough information. I will update it if you update your question with more details about your remote.
Update - git reflog might help
When you did git pull origin master, assuming master is the branch you care about, you probably got a local copy of the correct master commit you want on origin. You can use git reflog to see the history of your local HEAD in your sandbox. If you find the good commit there, this solution could work:
Here I'm assuming these is not work you want to preserve in your sandbox.
git checkout master
git reset --hard <good commit> # WARNING: destroys any uncommitted local changes
At this point, check that your sandbox contains the files you want to restore to origin. If so, proceed with this command:
git push --force origin master
But be warned, this will overwrite what's on origin. It's only appropriate if you have carefully validated that master in your sandbox really contains the history you want to restore on origin.

How to copy commits from one Git repo to another?

Last week I created a Github repo and forgot to select a license for the repo. Now there are already 3 large commits.
I have asked the 3 contributors if it is ok if I delete the repo and then create it again with the same name and this time selecting the license when creating the repo, and they were fine with that.
Question
Is there a way I can get the commits into the new repo (this time the first commit is the LICENSE file) and still keep the commit meta info?
Is there a way I have get the commits into new repo (this time the first commit is the LICENSE file) and still keep the commit meta info?
Yes, by adding a remote and cherry-picking the commits on top of your first commit.
# add the old repo as a remote repository
git remote add oldrepo https://github.com/path/to/oldrepo
# get the old repo commits
git remote update
# examine the whole tree
git log --all --oneline --graph --decorate
# copy (cherry-pick) the commits from the old repo into your new local one
git cherry-pick sha-of-commit-one
git cherry-pick sha-of-commit-two
git cherry-pick sha-of-commit-three
# check your local repo is correct
git log
# send your new tree (repo state) to github
git push origin master
# remove the now-unneeded reference to oldrepo
git remote remove oldrepo
The rest of this answer is if you still want to add the LICENSE to your previous repo.
Yes. You can place your LICENSE commit as the first commit by rebasing.
Rebasing is gits way of rearranging commit order while keeping all the commit authors and commit dates intact.
When working on a shared repo, it's generally discouraged unless your entire team is git-fluent. For those that aren't, they can just clone a fresh copy of the repository.
Here's how you get your LICENSE commit as the first commit.
1. Update and rebase your local copy
Check out your project and place the LICENSE file in a commit ON TOP of your current 3 commit stack.
#create LICENSE file, edit, add content, save
git add LICENSE
git commit -m 'Initial commit'
Then do an interactive rebase on the master branch to REARRANGE the commits.
git rebase -i --root
It will open an editor. Move the bottom line (your "Initial commit" commit, the most recent commit) to the top of the file. Then save and quit the editor.
As soon as you exit the editor, git will write the commits in the order you just specified.
You now have your local copy of the repository updated. do:
git log
to verify.
2. Force push your new repo state to github
Now that your copy is updated, you have to force push it to github.
git push -f origin master
This will tell github to move the master branch to its new location.
You should only force push in rare occasions like this where everybody working with it is aware of the pending change, else it will confuse your collaborators.
3. Synchronize collaborators to github
Lastly, all the collaborators will have to synchronize to this repository.
First they must have clean repositories as the following command can be destructive if there are unsaved changes.
# make sure there are no unsaved changes
git status
# pull the latest version from github
git fetch
# move their master branch pointer to the one you published to github.
git reset --hard origin/master
That's it. Everybody should be in sync now.
I had a similar problem where I forgot to fork a repo to my github and added several commits before I realized my mistake.
I found a pretty simple solution.
First remove the remote to the original repo
git remote remove origin
Second add a remote to the new fork on my github
git remote add origin <my repo URL>
Then I pushed to origin master and all of my commits showed up on my github.
I used the following approach:
Clone the source repo to a folder like /c/SrcRepo
Clone the destination repo to a folder like /c/DstRepo and switch to the destination branch
In the root folder of the destination repo run the command:
git pull /c/SrcRepo srcBranch --allow-unrelated-histories
No necessary to create an additional remote reference
Based on #Moocowmoo's answer but trying to streamline it bit more
What this does differently is tries to avoid conflicts as much as possible, just assuming that the remote is correct.
It doesn't however handle deleted files well, so there is still a manual element.
# assuming you are already on the branch you want to be
git remote add oldrepo https://github.com/path/to/oldrepo
git fetch oldrepo
# take all or subset of changes from a branch
git cherry-pick --strategy recursive --strategy-option theirs oldestCommitHash^..latestCommitHash
# or take all changes included in a specific merge commit (easiest)
git cherry-pick --strategy recursive --strategy-option theirs mergeCommitHash^..mergeCommitHash
# handling deleted files/unhandled conflicts
# just keep repeating this section
git mergetool
# either c/m or d based on if you want to keep or delete the files
git cherry-pick --continue
Destination Git = UrlD (existing content doesn't matter)
SourceGit = UrlS
git clone UrlS
git remote add origin2 UrlD
git push -f origin2 master
Now the Destination will have the same data as Source(You can also use origin instead of origin2)
you can try this, it's easy and straightforward. This will push all commits before (and including) the hash you use as <last-commit-hash-from-old-repo> to the other repo:
git clone https://github.com/path/to/new-repo.git new-repo
cd new-repo
git remote add old https://github.com/path/to/old-repo.git
git remote update
git merge --allow-unrelated-histories <last-commit-hash-from-old-repo>
git push origin main
if anyone needs to push all commits from a repo to another as a single commit (like I needed), you can simply add --squash to the merge command like this:
git clone https://github.com/path/to/new-repo.git new-repo
cd new-repo
git remote add old https://github.com/path/to/old-repo.git
git remote update
git merge --squash --allow-unrelated-histories <last-commit-hash-from-old-repo>
git push origin main
In my case I needed to find out differences between the old and new repos. So in the new repo added the old one.
git remote add old https://gitlab.site.az/old-blog.git
Fetch all remotes
git fetch --all
Find different commits
git log --graph --oneline --pretty=format:"%h%x09%an%x09%ad%x09%s" --abbrev-commit --date=relative develop..old/develop
Get commits you selected
git cherry-pick SHA1 SHA2 SHA4

New file lost after git stash and then git stash apply in new branch

I've probably just lost 5 hours of work here... :(
I was on branch A: Created new file with other changes to existing files
I stashed my changes
Checked out master
Did pull on master
Checked out branch A
Did git stash apply
Did git checkout -b newBranchB
I then did git rebase master
Due to some conflicts I then did git rebase --abort
Prior to aborting and rebasing, I can see the new file.
I don't see my new file after aborting rebase! :(
see if git reflog has it.
git log records all commits that will be pushed to the remote repo.
get reflog shows all commits, even squashed commits - amended commits, overwritten commits, etc (but only since u last cloned).
git reflog
git reset --hard YOUR_REFLOG_COMMIT
i just hope u committed at some point!
note that git reflog is a local reference and is deleted if you delete the git folder locally. unlike git log, which will be available if you delete the git folder locally and then clone the repo again later.

How to handle this merge issue in GIT

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.

Error on git patch using git am

When I try to perform a patch using:
git am 0001-someFile.patch
but I get this error:
error: patch failed: src/***/file.c:459
error: src/***/file.c: patch does not apply
Patch failed at 0001 someFile.patch
When you have resolved this problem run "git am --resolved".
If you would prefer to skip this patch, instead run "git am --skip".
To restore the original branch and stop patching run "git am --abort".
I am trying to manually merge the conflicts using:
git mergetool --tool=meld
But I am getting:
No files need merging
How can I solve this problem?
I am getting the name of the file which holds the error, but have no idea about the line (it's a big file)
Maybe there is a better way to perform such patching?
I'm in charge of handling all patching at my work. I've had this happen many times. A patch cannot be merged. The reason this is happening is because the master branch has changes that the patch did not take into account, which prevents it from patching correctly. From all my experience, this could be caused by several things:
The person who made the patch failed to pull the master branch and rebase master onto their development branch.
Between the time that the person pulled and the patch was applied enough changes were made to the master branch to stop the patch from applying due to too many conflicts.
The person patched incorrectly.
Here is the flow I've had the most success with. (This is assuming the person is developing on a branch other than master)
Make sure you've added all files and commited all changes.
git checkout master
git pull
git checkout {development branch}
git rebase master (this will bring the development branch up to speed with master)
git checkout -b {submission branch} master
git merge --squash --no-commit {development branch}
git commit -am "Commit Comment Here" (NOTE: This commit comment will be the name of the patch)
git format-patch origin..HEAD
That makes sure your patch is up to date with the origin master branch. Send that patch and hopefully the patch is applied before too many changes are made on the master.
You need to do a 3-way merge:
git am -3 0001-someFile.patch
git mergetool -t meld

Resources