Git status content is different in different linux account - linux

There are two accounts in linux server, root and super.
I can git pull to latest code by root account
I cannot git pull to latest code by super account
I found the status content is different by execute git status
super#api-dev:~/dnmp/www/localhost$ git status
On branch develop
Your branch is up to date with 'origin/develop'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
../../data/
../../docker-compose.yml
../../services/
../
nothing added to commit but untracked files present (use "git add" to track)
...
root#api-dev:~/dnmp/www/localhost# git status
On branch develop
Your branch is up to date with 'origin/develop'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: composer.lock
no changes added to commit (use "git add" and/or "git commit -a")
There is a git repository root in ~/dnmp/
There is another git repository root in ~/dnmp/www/localhost
I want to change git status of super account to the same as root content.
Any idea to fix the difference?

git is different from other source control systems. When you clone a repository, it creates a local repository copy on your computer. So you have 1 copy as root, and another copy as super.
Then you can modify the files in your local copy, even commit these changes. But the main repository will not know of these until you push them into it.
So, when you run git status it shows you the state of the local repository, not the state of the main git repository.
To sync your local repositories with the main one, you can:
delete your local repository and clone from scratch.
pull the files from the main repository with git pull.
If you want to keep some changes you made locally, you will have to commit and push these up to the main repository first.
You can read on git, like this simple page: https://rogerdudler.github.io/git-guide/
More in depth sites exist to explain the whole of git, like:
https://www.atlassian.com/git/tutorials
https://www.freecodecamp.org/news/what-is-git-and-how-to-use-it-c341b049ae61/

Related

How to commit changes on server and pull updates from code repository without conflict?

I pushed latest codes from my GitLab repository. The changes was coming from my local development machine. I've successfully done it using this commands:
git push
to push my latest changes to my GitLab repository. Now I pulled the updates on server but unfortunately it did not happen successfully since I have uncommitted changes from the server.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: .htaccess
modified: composer.json
modified: composer.lock
Untracked files:
(use "git add <file>..." to include in what will be committed)
php.ini
php_errorlog
These changes makes my git pull failed thus need to commit it first before pulling the updates from GitLab repository. How should we commit it the right way to prevent conflict on server side.
This is actually the scenario:
First I push my changes from local development machine to Gitlab repository. git command git push
Second I pull code updates from Gitlab repository to my server. git command git pull
Third I get conflict due to uncommitted changes in server.
Error conflict: after done the second step.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: .htaccess
modified: composer.json
modified: composer.lock
Untracked files:
(use "git add <file>..." to include in what will be committed)
php.ini
php_errorlog
NOTE : If multiple people are working on the same file, then there is more possibility of getting conflicts. It will be easy to resolve conflicts with Visual Studio Code Editor
Steps to resolve conflicts
Check whether all your change set are correct , validate once and remove /discard all the wanted files which you don't want to commit
Enter the command in current work folder git add .
Then check the staged file with the next command git status and validate once like whether the required file only got staged up.
Then do git stash, which will store your current code with another index
Now do git pull to pull the changes from the server, which will make your current working repository latest with server
Now do git stash pop , this will stash the changes which you have done
Since you have poped out your changes , if any conflict occurs check the changeset
if both are needed - do Accept Both,
if only your changes needed - do Accept Incoming,
if the current changes present in server is needed - Accept Current.
Again check git status, by this you can reverify whether only your changeset is correct
Now you can commit your changes to the server
Hope this would be helpful

git on Linux uploading files from another repository too

I have 2 git repositories, one with 3 files and another one with the same 3 files but similar content. (Like README.md, index.html, etc.)
I used "git add" to add the files and created 2 remotes named "view" and "music".
I added the 3 first files in the first folder with "git add" and commited+pushed them to the view repository.
Then I went to the other folder, added those 3 files with "git add" too and commit+pushed them too, but i suddenly have the other 3 files pushed too.
(The repository "music" has the files from "view" too)
TL;DR: How can I seperate git repositories on Linux
Running CentOS 7 and the newest git update available via yum.
I already tried making 2 different folders and writing "git init" before using the "git add" and other commands to commit + push.
They still returned an error instead of posting duplicates.
The following was used in both folders, but errors happened on the second folder
echo "# testing" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git#github.Username/reponame.git
git push -u origin master
I expected that git would somehow distinguish between multiple repositories, and that I can simply have 2 folders with 2 repositories.
I thought after a git push / commit the "git add array" of files would be empty again and I could add new files for another git commit / push.
How can I commit/push to multiple repositories via the Linux Command Line and push different files to different repositores without them getting mixed up.
EDIT:
The folder structure is as follows:
[usr#servr~]$ ls music/
index.html README.md songs
[usr#servr~]$ ls view/
index.html README.md pics
The exact steps I did:
cd music
git init; git add README.md; git add index.html; git add songs
git commit -m "Initial Commit"; git remote add origin <link2music>; git push -u music master
cd ../view
git add README.md; git add index.html; git add songs; git commit -m "Initial Commit"; git remote add origin <link2view>; git push -u view master
Then the git repository of view suddenly had the README.md of the music repository, and the folder songs too (even though it shouldnt).
I deleted the view repository but kept the music repository on github. (No folder deleted on my Linux machine). I then tried to exactly enter the code from the first snippet (the github offical one) in both folders (with git init too in each folder) and now the music repository, which already existed, threw me the error "! [rejected] master -> master (fetch first)", even though i never deleted the music repository, neither from my machine nor from github. (files are the same on both)
Someone recommended me to try git status, and with that I found out that my problem was the upper-level folder. I accidentally made the upper level folder, above my 2 "repo"-folders, a git repository.
How it should be:
Home
->music(repo .git)
->view(repo .git)
Hot it was (bad):
Home (repo .git)
->music
->view
That means that I didn't push the 2 folders to their respective repositories, but I instead pushed the upper-level folder (my home folder) to the repositories, which of course contained both of my other folders.
I deleted the .git folder from my home, cloned my git repos in a different folder again, and now they work as intended.

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

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

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.

Git confusion. "fatal: failed to open 'app/lang/en/homepage.php': File exists"

The other day I had to have a full image restore of one of our production servers due to a problem encountered with git and not having any clue how to solve the issue. The standard workflow, which has worked for almost 2 years without a problem has been:
Make local changes
Commit
Finish feature (GitFlow)
Push to remote origin
Login to server, go to http root and issue the following command:
git pull master Version_1.x.x
However, this time instead of this working as usual I was presented with this error.
fatal: failed to open 'app/lang/en/homepage.php': File exists
That file was updated in dev as were several other files. I immediately got server monitoring notification that the website was no longer responding. So I loaded it up and I got a 404 error??
Returning to the console I did git status and was blown away with what was shown.
On branch master
Changed but not updated:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: app/views/layouts/landingBody.blade.php
modified: app/views/layouts/trailblazers.blade.php
modified: app/views/mobile/UI/landingBody.blade.php
modified: public/images/spashPage_full_new.jpg
Untracked files:
(use "git add <file>..." to include in what will be committed)
public/images/3rd_Party/
public/images/UI_bg_login_summer2015.jpg
public/images/spashPage_full_1024_summer2015.jpg
public/images/spashPage_full_1200_summer2015.jpg
public/images/spashPage_full_blur_summer2015.jpg
public/images/spashPage_full_summer2015.jpg
no changes added to commit (use "git add" and/or "git commit -a")
How is this possible that files have been changed in production and files that were part of the latest update were reporting as untracked files?
So I tried stashing the changes which predictably returned the following on the next git status
On branch master
nothing to commit (working directory clean)
I tried everything from reset hard to reset to a specific commit and it progressively got worse to the point I was getting.
I got errors such as
git reset --hard ORIG_HEAD
error: git checkout-index: unable to create file app/3rdParty/Mobile_Detect.php (File exists)
error: git checkout-index: unable to create file app/config/app.php (File exists)
Not knowing what else to do, I did a full restore from a snapshot. Now that I am back to the last backup, which was less than 16hrs old, and no other changes made since then I do a git status on the same production machine. Just to check before actually issuing another git pull command and messing everything up again. The following was result:
On branch master
Changed but not updated:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: public/.htaccess
Untracked files:
(use "git add <file>..." to include in what will be committed)
app/config/auth.php
app/config/cache.php
app/config/compile.php
app/config/database.php
app/config/remote.php
app/config/session.php
app/views/emails/
app/views/hello.php
bootstrap/compiled.php
readme.md
no changes added to commit (use "git add" and/or "git commit -a")
Again how is this possible? Nothing is ever done in production except git pull <local branch> <origin branch>, yet there are untracked files and modified files.
I have updates which need to be "pushed" into production but I can't have the website go down again. Not being a git expert makes this especially difficult.
I did check the git log on production and compared it with the origin, and dev and the commits all match up. This is all very perplexing.
Any help would be greatly appreciated.
The problem was within dropbox. The .gitignore file for one of the sub directories was removed and replaced with an alternate version and was unintentionally pushed in a previous release. The next time it was time to update everything was completely out of whack. Replacing the .gitignore file with the correct version and doing a hard reset, followed by a pull resolved the issue. Lesson: Dropbox can be dangerous when playing in the same sandbox as git.

Resources