Failsafe Automated Git Update/Add/Commit - linux

I have a website that I need to commit system generated files and folders to an existing git repository via linux command line. I have an automated process that monitors a folder for new bash scripts and runs them. The website creates the scripts and saves them to the folder.
I keep getting issues where something has changed on either the remote repo or my local one that is stopping git from completing the following commands
git pull --rebase origin
git add [repo path to updated file(s)]/*
git commit -m "commit message"
git push master
I need to bullet proof this process so that it will just run and I can forget about it. Right now, permissions issues on files pulled down, or merge conflicts, etc... keep getting the repo out of sync. How can I bullet proof the commands above so they will pull down any remote changes and then commit any new ones necessary?

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 sync data from local code to heroku git

I build some website using nodeJS that host on heroku. I am using the heroku CLI git. When i want to upload the files i am using the git add . after that git commit -am "some text" and git push heroku master.
The client is update his site and upload new images and content, and when i make some changes in the code and git it again all the work he is done is deleted from server (not from DB).
Add the images folder to the .gitignore file
How can i sync files and code with my local version before i push new version to the heroku server
when you do "git add ." you are telling git to overwrite all your files from git with code on you local.
if you have made changes in only specific files You should always add only those files when doing git add .
you can follow this steps.
git status -> this will show you list of files which are changes and umerged. get hold of file which you changed and want to add in git
Suppose its abc.html (copy full path of file shown in git status)
git add abc.html
git commit -m "some message"
(optional) git pull remote_branch current_branch (this will pull latest code of remote branch to your local). Sometimes you may get conflicts so go ahead open that file and resolve conflicts.
If you are alone working on branch you may not get any conflicts
git push current_branch remote_branch

What is wrong with my git 1.8.4.2-1?

I have an old Synology DS-106j server where I tried to install git using ipkg command. The installation went smoothly, but git failed to work correctly. I am currently learning how to use git, so I don't know if it is a bug from git with the version I am using or something else is wrong.
What I did was create a new local repository with a specified name, add a new file, commit it, and got an error:
NAS_SERVER> git init Test
Initialized empty Git repository in /root/Test/.git/
NAS_SERVER> ls
Packages.gz git_1.8.4.2-1_powerpc.ipk
Test
NAS_SERVER> cd Test
NAS_SERVER> git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
NAS_SERVER> touch Test.cs
NAS_SERVER> ls
Test.cs
NAS_SERVER> git add *
NAS_SERVER> git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: Test.cs
#
NAS_SERVER> git commit -m "Test"
fatal: 57e2b8c52efba71d84c56bf6f37581686b9061a3 is not a valid object
I thought...maybe I did something wrong, so I used git on Windows OS and try a push. Still an error. Transfer the whole repository to the server and check the status. It seems fine. Try a commit. Still the same result. What worse is that I can't update git version without having to compile it, which I don't even know how to do so. Any suggestion to what might be wrong?
If your goal is to push into a git repo located on the synology disk(s) for backup purposes I'd recommend a different approach which would avoid having to install a rather old git version on the synology box itself (which could lead to problems if/when using a newer git version on the windows machine).
Export a samba share from synology, mount it on windows and use the windows git to create the backup repo (maybe even a bare repo, eventually group shared if you plan to share work with other people). Then push from your working repo into this backup repo - all on the windows box. In this scenario the synology box doesn't need git installed, it just serves files (i.e. its original job).
I'm using such setup but with a linux machine instead of a windows one and with the bare repo on the synology disks exported via NFS instead of Samba.

git post-receive checkout to remote machine?

If you add the following script as a hooks/post-receive hook to a bare git repository foo.git:
#!/bin/sh
GIT_WORK_TREE=/bar git checkout -f
then whenever someone pushes to the repository the current state will be updated in directory bar.
This requires bar and foo.git to be on the same machine.
What's the easiest way to modify it so that the checkout is made on a remote machine (say baz:/bar) ?
One way would be to:
#!/bin/sh
GIT_WORK_TREE=/tmp/bar git checkout -f
rsync ... /tmp/bar baz:/bar
Is there a better way? Perhaps not requiring an intermediate temp directory? (If not what are the correct options to pass to rsync such that the resulting directory is indentical to being checked out directly?)
GIT_WORK_TREE=/bar means that bar is a git repo.
If bar is a git repo on the remote side, then it can pull from a bare repo bare_bar.git (also on the remote side), to which you can push to.
In other words, your post-receive hook would push to bare_bar repo through ssh, and a post-receive hook on that bare repo would trigger the pull from the actual repo bar: see "Creating a git repository from a production folder".
I'd prefer a solution that keeps the remote server free of any .git dirs (and even the git package itself ideally)
In that case, your current post-receive hook and its rsync command seems to be the only way to incrementally copy new data to a remote working tree.
git archive would archive everything each time.
git bundle would require git on the other side.

Git - Syncing a Github repo with a local one?

First off, forgive me if this is a duplicate question. I don't know anything but the basic terminology, and it's difficult to find an answer just using laymen's terms.
I made a project, and I made a repository on Github. I've been able to work with that and upload stuff to it for some time, on Windows. The Github Windows application is nice, but I wish there was a GUI for the Linux git.
I want to be able to download the source for this project, and be able to edit it on my Linux machine, and be able to do git commit -m 'durrhurr' and have it upload it to the master repository.
Forgive me if you've already done most of this:
The first step is to set up your ssh keys if you are trying to go through ssh, if you are going through https you can skip this step. Detailed instructions are provided at https://help.github.com/articles/generating-ssh-keys
The next step is to make a local clone of the repository. Using the command line it will be git clone <url> The url you should be able to find on your github page.
After that you should be able to commit and push over the command line using git commit -am "commit message" and git push
You can use SmartGit for a GUI for git on Linux: http://www.syntevo.com/smartgit/index.html
But learning git first on the command line is generally a good idea:
Below are some basic examples assuming you are only working from the master branch:
Example for starting a local repo based on what you have from github:
git clone https://github.com/sampson-chen/sack.git
To see the status of the repo, do:
git status
Example for syncing your local repo to more recent changes on github:
git pull
Example for adding new or modified files to a "stage" for commit
git add /path/file1 /path/file2
Think of the stage as the files that you explicitly tell git to keep track of for revision control. git will see the all the files in the repo (and changes to tracked files), but it will only do work on the files that you add to a stage to be committed.
Example for committing the files in your "stage"
git commit
Example for pushing your local repo (whatever you have committed to your local repo) to github
git push
What you need to do is clone your git repository. From terminal cd to the directory you want the project in and do
git clone https://github.com/[username]/[repository].git
Remember not to use sudo as you will mess up the remote permissions.
You then need to commit any changes locally, i.e your git commit -m and then you can do.
git push
This will update the remote repository.
Lastly if you need to update your local project cd to the required directory and then:
git pull
To start working on the project in linux, clone the repo to linux machine. Add the ssh public key to github. Add your username and email to git-config.
For GUI you can use gitg.
PS : Get used to git cli, It is worth to spend time on it.

Resources