Unique git url for github repo with multiple branches? - python

I'm looking at a flask app which I would like to import into an openshift project (https://github.com/lpolepeddi/intro-to-flask) . This contains a a series of 'checkpoints'(branches) as part of a tut. I want to grab the final code which is at https://github.com/lpolepeddi/intro-to-flask/tree/20_visibility_control for the starting point of a project . Is the a way to get a unique git url for this branch of the form
https://github.com/lpolepeddi/intro-to-flask.git
So that I can pull it in with a command like:
git remote add upstream -m master https://github.com/shekhargulati/flask-login-openshift-quickstart.git

Although as far as I know "single branch GitHub url" doesn't exist, you could just clone entire repo and later change branch:
git clone https://github.com/lpolepeddi/intro-to-flask.git
cd intro-to-flask
git checkout 20_visibility_control
or as stated in this answer, clone only one branch:
git clone --branch 20_visibility_control --single-branch https://github.com/lpolepeddi/intro-to-flask.git

Related

How does one .gitignore pickle files in python? [duplicate]

How can I delete "file1.txt" from my repository?
Use git rm.
If you want to remove the file from the Git repository and the filesystem, use:
git rm file1.txt
git commit -m "remove file1.txt"
But if you want to remove the file only from the Git repository and not remove it from the filesystem, use:
git rm --cached file1.txt
git commit -m "remove file1.txt"
And to push changes to remote repo
git push origin branch_name
git rm file.txt removes the file from the repo but also deletes it from the local file system.
To remove the file from the repo and not delete it from the local file system use:
git rm --cached file.txt
The below exact situation is where I use git to maintain version control for my business's website, but the "mickey" directory was a tmp folder to share private content with a CAD developer. When he needed HUGE files, I made a private, unlinked directory and ftpd the files there for him to fetch via browser. Forgetting I did this, I later performed a git add -A from the website's base directory. Subsequently, git status showed the new files needing committing. Now I needed to delete them from git's tracking and version control...
Sample output below is from what just happened to me, where I unintentionally deleted the .003 file. Thankfully, I don't care what happened to the local copy to .003, but some of the other currently changed files were updates I just made to the website and would be epic to have been deleted on the local file system! "Local file system" = the live website (not a great practice, but is reality).
[~/www]$ git rm shop/mickey/mtt_flange_SCN.7z.003
error: 'shop/mickey/mtt_flange_SCN.7z.003' has local modifications
(use --cached to keep the file, or -f to force removal)
[~/www]$ git rm -f shop/mickey/mtt_flange_SCN.7z.003
rm 'shop/mickey/mtt_flange_SCN.7z.003'
[~/www]$
[~/www]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: shop/mickey/mtt_flange_SCN.7z.003
#
# 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: shop/mickey/mtt_flange_SCN.7z.001
# modified: shop/mickey/mtt_flange_SCN.7z.002
[~/www]$ ls shop/mickey/mtt_flange_S*
shop/mickey/mtt_flange_SCN.7z.001 shop/mickey/mtt_flange_SCN.7z.002
[~/www]$
[~/www]$
[~/www]$ git rm --cached shop/mickey/mtt_flange_SCN.7z.002
rm 'shop/mickey/mtt_flange_SCN.7z.002'
[~/www]$ ls shop/mickey/mtt_flange_S*
shop/mickey/mtt_flange_SCN.7z.001 shop/mickey/mtt_flange_SCN.7z.002
[~/www]$
[~/www]$
[~/www]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: shop/mickey/mtt_flange_SCN.7z.002
# deleted: shop/mickey/mtt_flange_SCN.7z.003
#
# Changed but not updated:
# modified: shop/mickey/mtt_flange_SCN.7z.001
[~/www]$
Update: This answer is getting some traffic, so I thought I'd mention my other Git answer shares a couple of great resources: This page has a graphic that help demystify Git for me. The "Pro Git" book is online and helps me a lot.
First, if you are using git rm, especially for multiple files, consider any wildcard will be resolved by the shell, not by the git command.
git rm -- *.anExtension
git commit -m "remove multiple files"
But, if your file is already on GitHub, you can (since July 2013) directly delete it from the web GUI!
Simply view any file in your repository, click the trash can icon at the top, and commit the removal just like any other web-based edit.
Then "git pull" on your local repo, and that will delete the file locally too.
Which makes this answer a (roundabout) way to delete a file from git repo?
(Not to mention that a file on GitHub is in a "git repo")
(the commit will reflect the deletion of that file):
And just like that, it’s gone.
For help with these features, be sure to read our help articles on creating, moving, renaming, and deleting files.
Note: Since it’s a version control system, Git always has your back if you need to recover the file later.
The last sentence means that the deleted file is still part of the history, and you can restore it easily enough (but not yet through the GitHub web interface):
See "Restore a deleted file in a Git repo".
This is the only option that worked for me.
git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch *.sql'
Note: Replace *.sql with your file name or file type. Be very careful because this will go through every commit and rip this file type out.
EDIT:
pay attention - after this command you will not be able to push or pull - you will see the reject of 'unrelated history' you can use 'git push --force -u origin master' to push or pull
Additionally, if it's a folder to be removed and it's subsequent child folders or files, use:
git rm -r foldername
More generally, git help will help with at least simple questions like this:
zhasper#berens:/media/Kindle/documents$ git help
usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path] [-p|--paginate|--no-pager] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [--help] COMMAND [ARGS]
The most commonly used git commands are:
add Add file contents to the index
:
rm Remove files from the working tree and from the index
If you want to delete the file from the repo, but leave it in the the file system (will be untracked):
bykov#gitserver:~/temp> git rm --cached file1.txt
bykov#gitserver:~/temp> git commit -m "remove file1.txt from the repo"
If you want to delete the file from the repo and from the file system then there are two options:
If the file has no changes staged in the index:
bykov#gitserver:~/temp> git rm file1.txt
bykov#gitserver:~/temp> git commit -m "remove file1.txt"
If the file has changes staged in the index:
bykov#gitserver:~/temp> git rm -f file1.txt
bykov#gitserver:~/temp> git commit -m "remove file1.txt"
git rm will only remove the file on this branch from now on, but it remains in history and git will remember it.
The right way to do it is with git filter-branch, as others have mentioned here. It will rewrite every commit in the history of the branch to delete that file.
But, even after doing that, git can remember it because there can be references to it in reflog, remotes, tags and such.
If you want to completely obliterate it in one step, I recommend you to use git forget-blob
https://ownyourbits.com/2017/01/18/completely-remove-a-file-from-a-git-repository-with-git-forget-blob/
It is easy, just do git forget-blob file1.txt.
This will remove every reference, do git filter-branch, and finally run the git garbage collector git gc to completely get rid of this file in your repo.
Note: if you want to delete file only from git use below:
git rm --cached file1.txt
If you want to delete also from hard disk:
git rm file1.txt
If you want to remove a folder(the folder may contain few files) so, you should remove using recursive command, as below:
git rm -r foldername
If you want to remove a folder inside another folder
git rm -r parentFolder/childFolder
Then, you can commit and push as usual. However, if you want to recover deleted folder, you can follow this: recover deleted files from git is possible.
From doc:
git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>…​
OPTIONS
<file>…​
Files to remove. Fileglobs (e.g. *.c) can be given to remove all matching files. If you want Git to expand file glob characters, you
may need to shell-escape them. A leading directory name (e.g. dir to
remove dir/file1 and dir/file2) can be given to remove all files in
the directory, and recursively all sub-directories, but this requires
the -r option to be explicitly given.
-f
--force
Override the up-to-date check.
-n
--dry-run
Don’t actually remove any file(s). Instead, just show if they exist in the index and would otherwise be removed by the command.
-r
Allow recursive removal when a leading directory name is given.
--
This option can be used to separate command-line options from the list of files, (useful when filenames might be mistaken for
command-line options).
--cached
Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.
--ignore-unmatch
Exit with a zero status even if no files matched.
-q
--quiet
git rm normally outputs one line (in the form of an rm command) for each file removed. This option suppresses that output.
Read more on official doc.
The answer by Greg Hewgill, that was edited by Johannchopin helped me, as I did not care about removing the file from the history completely.
In my case, it was a directory, so the only change I did was using:
git rm -r --cached myDirectoryName
instead of "git rm --cached file1.txt"
..followed by:
git commit -m "deleted myDirectoryName from git"
git push origin branch_name
Thanks Greg Hewgill and Johannchopin!
Another way if you want to delete the file from your local folder using rm command and then push the changes to the remote server.
rm file1.txt
git commit -a -m "Deleting files"
git push origin master
According to the documentation.
git rm --cached file1.txt
When it comes to sensitive data—better not say that you removed the file but rather just include it in the last known commit:
0. Amend last commit
git commit --amend -CHEAD
If you want to delete the file from all git history, according to the documentation you should do the following:
1. Remove it from your local history
git filter-branch --force --index-filter \ "git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA" \ --prune-empty --tag-name-filter cat -- --all
# Replace PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA with the path to the file you want to remove, not just its filename
Don't forget to include this file in .gitignore (If it's a file you never want to share (such as passwords...):
echo "YOUR-FILE-WITH-SENSITIVE-DATA" >> .gitignore
git add .gitignore
git commit -m "Add YOUR-FILE-WITH-SENSITIVE-DATA to .gitignore"
3. If you need to remove from the remote
git push origin --force --all
4. If you also need to remove it from tag releases:
git push origin --force --tags
In my case I tried to remove file on github after few commits but save on computer
git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch file_name_with_path' HEAD
git push --force -u origin master
and later this file was ignored
To delete a specific file
git rm filename
To clean all the untracked files from a directory recursively in single shot
git clean -fdx
If you have the GitHub for Windows application, you can delete a file in 5 easy steps:
Click Sync.
Click on the directory where the file is located and select your latest version of the file.
Click on tools and select "Open a shell here."
In the shell, type: "rm {filename}" and hit enter.
Commit the change and resync.
First,Remove files from local repository.
git rm -r File-Name
or, remove files only from local repository but from filesystem
git rm --cached File-Name
Secondly, Commit changes into local repository.
git commit -m "unwanted files or some inline comments"
Finally, update/push local changes into remote repository.
git push
go to your project dir and type:
git filter-branch --tree-filter 'rm -f <deleted-file>' HEAD
after that push --force for delete file from all commits.
git push origin --force --all
https://stackoverflow.com/a/2047477/14508423
https://stackoverflow.com/a/19666677/14508423
Quite similar, tried both. Nothing. Git keeps tracking the file. (I use vscode so the file got instantly marked as U(untracked) or A(added))
Doing this https://stackoverflow.com/a/53431148/14508423 to whole category solves the problem.
Do this to delete a file from git and make git forget about it:
**manually add the file name to .gitignore**
git rm --cached settings.py
git commit -m "remove settings.py"
git push origin master
git rm -r --cached .
git add .
git commit -m "Untrack files in .gitignore"
ps: every time it failed I tried to delete file with sensitive info like tokens (.env and settings.py files). But it works fine with random files oO
For the case where git rm doesn't suffice and the file needs to be removed from history: As the git filter-branch manual page now itself suggests using git-filter-repo, and I had to do this today, here's an example using that tool. It uses the example repo https://example/eguser/eg.git
Clone the repository into a new directory git clone https://example/eguser/eg.git
Keep everything except the unwanted file. git-filter-repo --path file1.txt --invert-paths
Add the remote repository origin back :
git remote add origin https://example/eguser/eg.git.
The git-filter-repo tool removes remote remote info by design and suggests a new remote repo (see point 4). This makes sense for big shared repos but might be overkill for getting rid a single newly added file as in this example.
When happy with the contents of local, replace remote with it.
git push --force -u origin master. Forcing is required due to the changed history.
Also note the useful --dry-run option and a good discussion in the linked manual on team and project dynamics before charging in and changing repository history.
I tried a lot of the suggested options and none appeared to work (I won't list the various problems).
What I ended up doing, which worked, was simple and intuitive (to me) was:
move the whole local repo elsewhere
clone the repo again from master to your local drive
copy back the files/folder from your original copy in #1 back into the new clone from #2
make sure that the problem large file is either not there or excluded in the .gitignore file
do the usual git add/git commit/git push
After you have removed the file from the repo with git rm you can use BFG Repo-Cleaner to completely and easily obliterate the file from the repo history.
Just by going on the file in your github repository you can see the delete icon beside Raw|Blame and don't forget to click on commit changes button. And you can see that your file has been deleted.
New answer that works in 2022.
Do not use:
git filter-branch
this command might not change the remote repo after pushing. If you clone after using it, you will see that nothing has changed and the repo still has a large size. This command is old now. For example, if you use the steps in https://github.com/18F/C2/issues/439, this won't work.
You need to use
git filter-repo
Steps:
(1) Find the largest files in .git:
git rev-list --objects --all | grep -f <(git verify-pack -v .git/objects/pack/*.idx| sort -k 3 -n | cut -f 1 -d " " | tail -10)
(2) Strat filtering these large files:
git filter-repo --path-glob '../../src/../..' --invert-paths --force
or
git filter-repo --path-glob '*.zip' --invert-paths --force
or
git filter-repo --path-glob '*.a' --invert-paths --force
or
whatever you find in step 1.
(3)
git remote add origin git#github.com:.../...git
(4)
git push --all --force
git push --tags --force
DONE!
I have obj and bin files that accidentally made it into the repo that I don't want polluting my 'changed files' list
After I noticed they went to the remote, I ignored them by adding this to .gitignore
/*/obj
/*/bin
Problem is they are already in the remote, and when they get changed, they pop up as changed and pollute the changed file list.
To stop seeing them, you need to delete the whole folder from the remote repository.
In a command prompt:
CD to the repo folder (i.e. C:\repos\MyRepo)
I want to delete SSIS\obj. It seems you can only delete at the top level, so you now need to CD into SSIS: (i.e. C:\repos\MyRepo\SSIS)
Now type the magic incantation git rm -r -f obj
rm=remove
-r = recursively remove
-f = means force, cause you really mean it
obj is the folder
Now run git commit -m "remove obj folder"
I got an alarming message saying 13 files changed 315222 deletions
Then because I didn't want to have to look up the CMD line, I went into Visual Sstudio and did a Sync to apply it to the remote
if your file is sensitive (for example, settings or keys you accidently added and committed) then you can remove it from all versions.
To remove from all versions use the command below (warning: careful because you won't be able to restore the removed file in the repo if do not have a copy):
Using Git
git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch file.ext' HEAD
Using git-filter-repo (get it here, new recommended way by Git)
git filter-repo --path file.ext
If you need to remove files from a determined extension (for example, compiled files) you could do the following to remove them all at once:
git remove -f *.pyc
Incase if you don't file in your local repo but in git repo, then simply open file in git repo through web interface and find Delete button at right corner in interface.
Click Here, To view interface Delete Option

not able to Add file to git VIA windows 10 [fatal: not a git repository (or any of the parent directories): .git]

i am not able to add a file
to git via windows 10 and also it is giving me this error
for checking the status also
fatal: not a git repository (or any of the parent directories): .git
make sure the remote url exists in the directory where you are trying to add the file.
Run the following command:
git remote -v
If it still stays `fatal: not a git repository (or any of the parent directories): .git
`
then it means git is not configured to your code.
git init
git remote add origin <HTTPS URL FOR GIT REPO>
then you will be able to add the file

Push to remote repository

I have two repositories on github, using gitpython I'm trying to push a file from one repository to another remote repository. I've managed to do it using git but struggling with the gitpython code.
git remote add remote_to_push git#bitbucket...
git fetch remote_to_push
git checkout remote_to_push/master
git add file_to_push
git commit -m "pushing file"
git push remote_to_push HEAD:master
I've managed to create a repo object of the remote I think with the following
from git import Repo
repo = Repo('path/to/other/git/repo')
remote = repo.remotes.origin
I can't figure out how to add something to then push, if i call
remote.add("file_to_push")
Then I get errors about the create() function
TypeError: create() takes exactly 4 arguments (2 given)
Trying to follow what they have done in How to push to remote repo with GitPython with
remote.push(refspec='{}:{}'.format(local_branch, remote_branch))
I assume it should work with using master and master as the remote branches as they both must exist but it's giving me the error
stderr: 'error: src refspec master does not match any.'
Thanks
Solved it.
First created a remote of the other repo
git remote add remote_to_push git#bitbucket...
Then the gitpython code
from git import Repo
repo = Repo('path/to/other/git/repo') #create repo object of the other repository
repo.git.checkout('remote_to_push/master') #checkout to a branch linked to the other repo
file = 'path/to/file' #path to file to push
repo.git.add(file) # same as git add file
repo.git.commit(m = "commit message") # same as git commit -m "commit message"
repo.git.push('remote_to_push', 'HEAD:master') # git push remote_to_push HEAD:master
Aside from the docs, I found the following to be quite helpful if anyone's struggling with gitpython as I found it quite a pain
Python Git Module experiences?
http://sandlininc.com/?p=801
Git push via GitPython
How to push to remote repo with GitPython

Not transferring project to server when git push

I have a django project under development on my windows computer (dev-machine). I am using pyCharm for development.
I have set up a server (server-machine) running ubuntu. And now want to push my project to the server.
So in my project folder on the dev-machine I have done the git init:
$ git init
$ git add .
$ git commit -m"Init of Git"
And on the server-machine I have made a project folder: /home/username/projects
In this folder I init git as well
$ git init --bare
Back on my dev-machine, I set the connection to the server-machine by doing this
$ git remote add origin username#11.22.33.44:/home/username/projects
And finally pushing my project to server-machine by typing this command on my dev-machine
$ git push origin master
It starts to do ome transfer. And here's the problem.
On the server-machine when I check what's been transferred, it's only stuff like this
~/projects$ ls
branches config description HEAD hooks info objects refs
Not a single file from the project is transferred. This looks much like what the .git folder contains on the dev-machine.
What am I doing wrong?
What you see is the directory structure git uses to store your files and meta data. This is not a checked-out copy of the repository.
To check whether the data made it into the repository use git log in ~/project
Okay, so I understand now where I went wrong.
With git push I am not setting up the project.
To set up the project I need to do git clone.
This is how I did it.
1.
So I made a folder for git repositories on the server-machine. I called it /home/username/gitrepos/
2.
Inside there, I made a folder for my project, where I push the git repository into. So path would look like this for me /home/username/gitrepos/projectname/
3.
Being inside that folder I do a 'git init' like this
$ git init --bare
4.
Then I push the git repo to this location. First setting the remote adress from my dev-machine with this command. If adding a remote destination new use this:
$ git remote set nameofconnection username#ip.ip.ip.ip:/home/username/gitrepos/projectname
if changing the adress for a remote destination use this:
$ git remote set-url nameofconnection username#ip.ip.ip.ip:/home/username/gitrepos/projectname
To se with remote destinations you have set type this:
$ git remote -v
5.
Now go back to server-machine and clone the project into a project folder. I made a folder like this /home/username/projects/
When being inside that folder I clone from the gitrepo ike this:
$ git clone /home/username/gitrepos/projectname
Thank you all for the help! <3

Git merge and select local file in conflicts

I have an flask project which I have deployed to openshift, which I maintain locally. I deleted it from openshift and now would like to redeploy it. In the openshift gui, I created the a python app and grabbed the openshift git repo;
ssh://*******#myproject.rhcloud.com/~/git/myproject.git/
I changed origin to the new url with:
git remote set-url origin ssh://*******#myproject.rhcloud.com/~/git/myproject.git/
Now based on https://developers.openshift.com/en/knowledge-base.html#sync-a-new-git-repo-with-an-existing-git-repo I want to pull and merge.
I tried:
$ git pull ssh://*******#myproject.rhcloud.com/~/git/myproject.git/
From ssh://*******#myproject.rhcloud.com/~/git/myproject.git/
* branch HEAD -> FETCH_HEAD
Auto-merging wsgi.py
CONFLICT (add/add): Merge conflict in wsgi.py
Auto-merging .openshift/markers/README.md
CONFLICT (add/add): Merge conflict in .openshift/markers/README.md
Auto-merging .openshift/cron/README.cron
CONFLICT (add/add): Merge conflict in .openshift/cron/README.cron
Automatic merge failed; fix conflicts and then commit the result.
Basically at this point If I understand correctly, I want to choose my local files over the remote files in all cases. What is the best way to do this.
I'm suspecting a rebase may be in order but not sure.
If you want to choose your local files over the remote files, do:
git checkout --ours wsgi.py
git checkout --ours .openshift/markers/README.md
git checkout --ours .openshift/cron/README.cron
git commit -a

Categories