I have a desktop PC and a laptop, I use both daily to commit to the same repository on GitHub (via Git). I have just moved over and started using Git, which is great. However, what command do I use when, for example, I have made changes on my desktop PC and committed them to GitHub, and then I want to move all of the changes currently on GitHub onto my laptop files to resume my work on my laptop where I left off on my desktop PC / GitHub?
Thanks in advance.
Clone a repository into a new directory ;
git clone uri_repository.git
Incorporates changes from a remote repository into the current branch.
git pull
Show the working tree status.
git status
Update the index using the current content found in the working tree.
git add .
Record changes to the repository.
git commit -m 'message'
Update remote references (refs) along with associated objects.
git push
To fetch your code from your remote repository from GitHub you use the command git pull. This fetches the latest changes and merges them with your local code so you have the most recent code. You can find a Git cheat sheet with most of the commands you will need here
You can use the following command to syn your remote repository to your local repository
/* origin points to your remote repository, master is the branch in which your working */
git pull origin master
Incase you dont have remote ie origin setup then
/* Here your aliasing your remote repository location to origin, you can name it anything */
git remote add origin your_git_repo_url
In case if you dont know which branch your currently working then
/* This will list all the branches with * preceded which is active one */
git branch
Once you do your local changes then you may want to push the code to remote repository which can be done as follows
git add .
OR
git add file1.txt file2.txt
git commit -m "You commit description"
git push origin master
Related
I am working with a open source repo that I have cloned in my development environment. When I want to update the internal git repo, I enter the folder /home/pranav/Desktop/ and do:
git pull upstream master
Make edits, save, git add , and git commit all in your local repo
Push changes from local repo to your fork on gitlab.com ( git push origin master )
Update the central repo from your fork ( Pull Request )
But then, every morning I have to do the same thing for the updating the repo. This is quite troublesome.
Can I put this in a Bash script? I mean, if I write these git commands in the shell script, and run it, will I be able to update the repo automatically whenever there are any commits in the original repo?
other way: 1. git fetch- check for any commits
2. if commits found, then git pull
3. Go with stashing for any local changes.
I was thinking of writing something like this...
1.Pull the latest code[
2.Commit the new code
3. When tried to push the code to bitbucket repository getting message "Local repository out of date[![enter image description here]
**
tried bothway Xcode push and cmd based push but no luck still getting this weird message
Note:
Xcode 12.4,
bit bucket
I don't understand, the image shows after the "pull"? If not, please edit your question and place the image in the correct place.
That message can be because someone else have pushed to your repo after the last time you pulled from it. You will need to revert all the X commits that you have done after your last succed push (save the work in another branch or whatever):
git reset --hard HEAD~X
Or reset your local branch to directly match your remote branch:
git reset --hard origin/remote-branch-name
Pull from the repo, make your commits and then you will be able to push again.
You have another option: PULL FORCE!!!
If you alredy known the "push --force" command, then pull force will sound you familiar, but in reality doesn't exist something like "pull --force", but exist a way to replicate that functionality in pull requests:
git fetch --all
git reset --hard origin/master
git pull origin master
(Replace "master" by your branch name)
With that command you overwrite your git history to match exactly the history of your remote repository branch. That will overwrite local changes (the ones that you have not pushed yet) and then you can make your commits and push them.
You can avoid this ackward situation by always working in a new branch only for your work, and when you need to integrate your changes you can make rebase or merge.
There are changes in your local repository that are not on the remote and vice versa, causing a conflict in your history.
You could try the following in your local repository (assuming your remote is called origin):
Make sure your local changes are committed (git commit -am "Some commit message")
Create a new branch (git branch new-branch-name)
Fetch the remote branch (git fetch origin)
Reset the target branch to the version on the remote (git reset --hard origin/target-branch-name)
Merge your newly created branch (git merge new-branch-name)
Push your changes to the remote (git push origin target-branch-name)
Finally resolved it by push the commits to the new PR and then merged the code with old PR
I need to push my modified new java code to my old git repository in github but I do not have old code in my pc. How to do that?
I had push a code before my github account before. Now I don't have that old code in my pc. How do I pull the project into my pc and after making changes, push again to the same repository?
I do not have much experience in github, so please help me to improve skills on github.
Check your remote first to see where it is pointing to by
$ git remote -v
origin ssh://git#<old-git-url>/<project>.git (fetch)
origin ssh://git#g<old-git-url>/<project>.git (push)
Change the pointing to GitHub
$ git remote set-url origin ssh://git#<github-url>/<project>.git
Now your repo is pointing to Github
Now you can make your changes and then add them and do a commit and finally push to remote branch; say you are on master.
git add <file>
git commit -m <commit message>
git push origin master
I had to do just that and achieved it like this.
Setup. I assume you have a new remote repository with files that may not cause a conflict with the directory you currently have locally.
Git clone from the git repository you need to push to. Just make sure you create a new directory for the cloned code.
Copy the contents of the cloned repository into the local directory that has your current code. Make sure to copy the .git (hidden) file.
cd into your local directory and run git remote -v. You should see the remote repository git address.
git add -A to add whatever change you require and commit it.
Finally git push
You need to make sure that your local repository (the one that is on your computer) is connected to the remote repository (the one that is on the GitHub servers).
After this, you need to add the modified file to the staging area. Say, you have a file test.txt that you have modified, you would add it to the staging area by typing
git add test.txt
After that you would need to commit those changes. You can do that by
git commit -m "commit message"
And that's it, you have now saved those changes and recorded them in the version control. But the changes that you made have only been recorded in your local repository and you would need to push these changes to the remote repository (the GitHub servers). You can do this by
git push origin master
It would take a few seconds (depending on your internet speed and the project file size) to push these changes to the remote servers. Once it's done, you can open that repository on GitHub and see the changes for yourself.
Just to amend this a bit. I believe "master" is now "main" in some instances or platforms. If this answer isn't working for you, try swapping that out. It worked for me.
I have some projects I would like to upload on my bitbucket private profile. I performed the following steps but I get an error.
Convert my project to git with: git init
Next:
git add
git commit -m "some message"
I created a bitbucket repository and version control system is GIT.
I then type this (of course with real account name and reponame and repo owner):
git remote add origin https://<repo_owner>#bitbucket.org/<accountname>/<reponame>.git
Finally,
git push -u origin master
I did all of this and my terminal gives me this error:
To https://bitbucket.org/milosdev_me/lfs.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://milosdev_me#bitbucket.org/milosdev_me/lfs.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Try:
git push origin master --force
This works for me.
Your master branch has some code that you don't locally have, and to prevent you from conflicts, it doesn't let you to push further changes, before you have them locally. To resolve this, pull all the changes to your local repository (your project):
git pull origin master --allow-unrelated-histories
After that, you will have all the code that is available on your master branch.
NOTE: Be careful, pulling the code from remote branch might mess up all the changes locally. Make sure to save those changes somewhere, or create another branch locally where you will pull your origin master branch, so it doesn't mess up your changes.
Your issue is that you have different things between your local repository and your git repository (probably a readme file that you created automatically), so you have two options:
use git pull origin master, with this command, you will pull from your git repository, but it will cause a merge conflict, which you have to resolve using an IDE (recommended to beginners) or through cmd.
use git push origin master --force, this way, you will force your push from your local repository to your git repository, ignoring any merge conflict by overwriting data. I'm not sure, but I think it will overwrite all git repository data with you local repository data (which is what you want).
Adding --force option is a bad idea
Either rebase it or pull the code, merge it and push it.
git pull --rebase origin master
git push -u origin master
Your remote repository and local repository have differences, something new in remote repository. So for pushing you need pull changes, from remote, previously. Try do:
git pull
git push -u origin master
The issue is because your remote repository and local repository have differences.I had same issue and i tried all the above mentioned solution but nothing worked for me.
For me the scenario was :- I already had my branch in remote repository.If you have the same scenario then follow below steps:-
run command 'git pull'
delete branch from local repository
checkout that particular branch using "checkout as a new local branch" from
the Remote repository.
run command 'git branch -u <your_branch_name>'
run command 'git push or git push --force'
or you can try directly from step 4 first , if it does not work then follow entire steps.Hopefully it will help someone.
If you have your bitbucket account linked to jira.
(this answer will work for you, only if you have your jira account linked to bitbucket)
I was having the same problem trying to push my current branch with the origin.
for example:
my branch name was:
feature/PREFIX-000-new-name-branch.
previous commit:
git commit -m "Write your commit here"
so far it was not working.
you have to mentioned the ticket name in the commit.
if you have made the commit, make an --amend to rename your commit and retry it.
git commit --amend -m "PREFIX-000 Write your commit here"
try the push again.
If you are using BitBucket, this issue occured when I tried to push to a branch but that branch has writing disabled via the repository settings.
if you have already created a project locally and you want to upload it to git,
you will then need to do:
git status to see the changes you need to upload
git add . to add those changes to your repo
git commit -m "" to add a commit message
git push origin master
that way I solved the very same problem I
was having.
it might be a configuration issue
I fixed this issue after updating the global user.email value with my email
git config --global user.email my#email.com
Note: You need to delete the previous commit because it had the wrong email in the commit
I have committed and pushed several changes for a project using git on the command line, but when I log in to GitHub, the branches and commits I made are not shown on my account. Why?
I can review all commit history I have made using git on the command line.
Results of git remote -v command
origin https://github.com/felixtan/guessing-game.git (fetch)
origin https://github.com/felixtan/guessing-game.git (push)
Once committed locally, you still need to push those commits to github:
git push
(since your remote is named origin, you don't need to specify its name: it pushes by default to 'origin')
This assumes you are the owner or one of the collaborators of the repo felixtan/guessing-game.
If it is the first time you push your current branch:
git push -u origin yourCurrentBranch
That establish a tracking relationship between your branch and 'origin/yourBranch', as detailed in "Why do I need to explicitly push a new branch?".
Once that first push is done, the subsequent pushes are simple 'git push'.
If you are not the owner/collaborator, you won't have the right to push to that repo.
You need to make a fork (See GitHub forking), and in your local cloned repo you are already working on:
git remote rename origin upstream
git remote add origin https://YourLogin#github.com/YourLogin/guessing-game.git
That way, you will push to your fork (that you own), and will make pull requests from there (See GitHub pull requests).