How do you edit a branch in Git? - version-control

Coming from a SVN world, wrapping my head around Git has been a little weird, and I'm having trouble letting go of some of the practices ingrained in me from using Subversion for so long. So, for example, in SVN a branching structure might look like this:
-Trunk
--Master
-Branches
--SomeFeatureBranchA
--SomeFeatureBranchB
So, in this situation my Master branch has it's own set of code, and once I check out SomeFeatureBranchA & SomeFeatureBranchB, they'll have their own code. However, when I create a branch in Git, I see my branch listed, but at this point I'm unsure of how to edit the code for that branch.
Do I clone that branch down & simply rename it like:
-Trunk
--Master
--SomeFeatureBranchA
Or is there some command I'm missing that handles this for me?
Thanks for taking your time to help me out!

git branch will tell you which branch you're currently on (as will git status). Just edit as you normally do, if you're on the right branch. If you're on the wrong branch git checkout some branch will get you there. The top of the git repository is the equivalent of subversion /trunk or /branches/SomeFeatureBranchA.

Forget everything you know about SVN when working with Git. You described your problem by comparing it with SVN, which makes it pretty difficult to understand what your problem actually is.
You created a branch in git (either with git branch <name> or git checkout -b <name>) and made it your active branch. Simply start hacking away at the code to edit your branch. When you're done, stage (read "add") and commit your changes, then push them at a later time if you're working with a remote tracking repository.
Does that answer your question? If not, can you please clarify what exactly it is you're trying to do?

Related

Git merge conflict issue, gir merge conflict while pulling

My coworker and I are working in the same branch which was cut from develop. There are many files to be taken care of by both of us.
The scenario I’m facing is that
I did some changes and pushed my code to the remote repo. My coworker also did some changes in his local and and now he is not able to take a pull. It shows merge conflict while pulling.
Can someone let me know how can we resolve this?
Based on my understanding, I think we made some changes in the same files and now to merge them, git is causing that conflict.
First, make sure to use:
git config --global pull.rebase true
git config --global rebase.autoStash true
That way, a simple git pull will:
stash your work in progress
rebase your local commits (not yet pushed) on top of the updated remote upstream branch (instead of merging). That keep the history linear.
You can also activate rerere in order to not resolve the same conflict over and over again.
Finally, in case of merge/rebase conflict, you will need to chose between "our" and "their" version in each file with conflict markers. As shown here, an IDE can help facilitate the visualization and resolution of such conflicts.
I used tortoise git
git tortoise > git diff
Then I manually resolved the git merge conflict by removing or modifying the code I didn’t want.
Then I committed the code and the issues were gone.

Why can't I revert a GitHub commit?

I am a newbie in the GitHub world. I've been working on a project for my coding bootcamp. I had everything working just right to the specifications of the project and I was all done. Then I foolishly seem to have committed an old early version of the project, thus covering over the final version. I have tried to revert the last commit from the GitHub website, but the interface does not seem to follow the instructions. I tried the "git revert {commit#}'" command from my terminal, but that did not work either. I need suggestions. I'd like to get it done from the command line if possible.
git revert commits a reverse change, so from the history point of view you will have two unnecessary commits that cancel each other. The {commit#} in your case should be the ID of the commit that you want to undo (= the last one). This should work as long as there are no other commits on top of it, otherwise you might get conflicts which require more work.
If you don't have any other commits apart from the one you want to undo, there is also a better way - simply move the branch back to point to the last commit you want to keep (= one before last).
Something like this (I assume you are working on master, that you didn't do revert yet and that there are no other people involved):
git checkout -b tmp_branch master~1
git branch -f master tmp_branch
git checkout master
git branch -D tmp_branch
git push -f origin master
And voilà. If your master is protected in GitHub, you will have to unprotect it. You can repeat this to go further back (or just use ~2, ~3 etc.)

What is the best practice to follow if two people made changes in the same file and want to commit there code to git?

I am working in a team on Selenium WebDriver in Java. For eg. If I and one of my team member are working on the same file. Every time someone commits some changes into the repository and when I pull the code, eclipse shows conflicts even if a new line of code has been added to the file. Is there any way to merge both the codes without this conflict?
Maybe try to do it that way :
git add [your file]
git commit "fix"
git pull origin master
git push origin master
The conflicts may occur because you don't commit your changes before pulling from the repository.

How to merge to a branch that is behind in network in Gitlab?

I am quite new with Gitlab and I'm having an issue for merging in Eclipse.
We're working as a team, and we all have development branches that we are trying to merge into a single one. Unfortunately, when I did my merge, I have done a stupid mistake. Instead of merging my development branch to the main one, I have merged the main one into my development branch.
I have reversed the commit/merge on gitlab, but now as I try to merge back my development branch into the main one on Eclipse, it seems like I am 9 commits ahead of this branch (described as the arrows on Eclipse here: ), so the potential merge would basically replace everything by my code, when I should actually have merge conflicts to solve.
I am not quite sure how to merge properly so that I get back these merge conflicts.
Here is a screenshot of my network:
The ['1'] commit in the network on the left branch (my branch) corresponds to the merge from Week6AllIssues to my dev branch (the wrong merge). The last commit on this left branch is me reversing the commit.
Thanks a lot for your help !
If you're not using the remote branch with anyone else, the following series of steps might help.
First, remove the superfluous commits from the local branch. It can be achieved with git reset --hard <the commit before you merged master into your branch> command (see this link on how to do this with Eclipse).
Now make the remote branch match your local branch. You can do this with git push --force command. In Eclipse, this command corresponds to configure push - enable "force update" option.
Now the superfluous commits are gone.

GitHub: What does checkout do?

I'm coming from a perforce background, trying to understand gitHub.
In Perforce when I "checkout" something it lets other users know I am working on that file. Does checkout work the same way in Git?
It seams like you don't check out files but branches?
Thanks
checkout in Perforce would roughly equate to clone in git, but in VCS like git ( and even SVN ), the checkout doesn't signal lock to others that you are working on it. Anyone can clone a git repo and work on it without thinking about who else is working on it.
In general, checking out a branch does not mean anyone gets notified. It simply means you are switching from one branch of code to another, potentially also creating a new branch in the process depending on your arguments.
For more info about checkout, see git checkout documentation.