Vs 2017 Integrate the Remote Changes Before Pushing Again
Contents
- What trouble(s) does Git solve?
- How does Git basically work?
- What'due south a typical daily flow?
- Doing It In Visual Studio 2019
- Create and Checkout a New Branch
- Stage Changes for Commit
- Commit the Change
- Make a Couple More Commits
- Clean Upwardly the Commits
- Checkout and Synchronize Primary
- Checkout and Rebase Feature Onto Primary
- Checkout and Merge Feature Into Master
- Push Changes to the Remote Server
- Delete the Branch
- Common Operations
- Checkout
- Branch
- Add (phase)
- Commit
- Merge
- Rebase (changes history)
- Cheque Status
- Check History
- Revert
- Reset (changes history)
- Tag
- The Rules For Teams
- Solving Common Problems
- Pull, Merge and Rebase Conflicts
- Remote vs Local, plus Base and Backup
- Last Word
Note
Some of the information below may get outdated with the upcoming VS Git revamp, which you can read virtually hither: Improved Git Feel in Visual Studio 2019 | Visual Studio Weblog
What problem(s) does Git solve?
- Multi-user offline development
- Rapid, dependable branching and merging
- Decentralized version control
Git is a distributed version control system that was originally developed by Linus Torvalds in Apr 2005 to supplant BitKeeper, which was withdrawn from free use due to alleged reverse engineering by SourcePuller creator Andrew Tridgell.
Torvalds needed a system that could manage the Linux kernel development and scale to hundreds of merges performed in seconds. Git has been maintained by Junio Hamano since July 2005.
Git is currently the about-used version command system by far. Microsoft recommends Git, has contributed to its evolution, and hosts the Windows source code using Git--the largest such repository in the world.
How does Git basically work?
A folder, .git, contains the complete repository for files and folders underneath it. A clone of a repository contains the same history every bit the original.
The repository is a file arrangement. Each file is named with an SHA-1 hash. A file contains either the actual content of source code file, or contains a tree of hash file names. In this style, Git maintains references, sometimes called "pointers"--the hash file names--to content.
There's a file that contains the reference to the root of the solution. From here, all the links to other files tin can be traced, so this becomes the current state.
If whatsoever file changes, Git creates a new re-create of the file with a new hash reference, updates links to it, and creates a new root-level reference. This becomes the new betoken-in-time version.
Branches be past creating a file with the branch name, e.1000. "master", whose content points to a detail root reference file. When people talk nigh branching in Git beingness "cheap," it's because, to branch from another branch, all Git has to do is create some other file, east.g. "feature-1," with the same reference file proper noun.
If Charlene clones a repository from Blake, all she's doing is making a copy of the .git folder. Let's say Charlene and Blake each make a alter to the same file. Each of them volition become a different new reference hash. This is how Git knows to merge files: when information technology finds a divergence in the tree, it can trace astern in the references to notice the common ancestor, and so make up one's mind the changes via a three-mode comparison. Once the differences are resolved, a new version of the file is created with a new reference name.
The process is exactly the same with repositories located on remote servers. The only difference is that remote repositories are typically "bare," meaning at that place'south no working folder.
Anyone tin create a blank repository on their local file filesystem. It'due south really merely the contents of the .git folder moved to the root level.
Remote branch reference data is maintained in a .git folder named remotes. Where the remotes are located is maintained in a .git config file.
What's of import to understand at this indicate is that your repository can comprise branches for your local work, and branches for remote work. When you fetch a remote branch, the contents are merged into your local re-create of that branch, e.g. origin/master. You and then decide whether to merge those files into your local branch, i.e. master
When it comes to tracking local changes, there are potentially three versions of the file in play at any in one case.
First, at that place'southward the version of the file y'all're editing. This is the working file.
2nd, there'due south the version of the file you're going to commit. This is the staged file. (Also chosen being in the "index".)
Third, there'south the version of the file since the last commit. This is the repository file.
How does this look in practice? Let's say the final committed file named Test.txt has one line:
one You lot edit the file and add a line:
one 2 So, now your working file is changed, simply Git doesn't know almost the changes. You add the file to the index via git add Test.txt.
If you lot were to commit now, the repository file would exist updated to friction match the staged file. But what if you don't commit, and instead add a 3rd line?
one two 3 If you commit, Git will still simply update the repository file with the ii lines from the staged file. You'd have to git add together again to phase the file with all iii lines.
This is a very flexible approach, letting you commit only the changes that yous want. While I don't cover it in this guide, information technology'south even possible to simply commit portions of a file, called "hunks."
What's a typical daily flow?
Let'southward assume yous've already cloned a repository from a remote server, and accept configured your local Git to work easily with that remote. Your typical twenty-four hour period volition look something like this.
- Create and 'checkout' a local co-operative named 'feature-1' from the 'master' branch to work on a characteristic.
This is a local characteristic branch that's not replicated in the remote.
- Make a pocket-sized fix of changes to the files.
-
Addthe changes to the index. -
Committhose changes with a short message - Do this a agglomeration of times until the feature--or enough of the feature to brand available to everyone--is complete (tests run, etc).
- Interactively
rebasethose commits, combining them and updating the messages so the changes will be articulate in the remote repository history that volition exist shared with anybody. -
Checkoutthemasterbranch - Pull (fetch and merge) any changes from the remote's
masterbranch into your localmasterco-operative. - Checkout the
feature-onebranch. -
Rebaseonto themasterco-operative. This makes information technology seem as if all your changes are also the latest changes. - Checkout
master - Merge
feature-1intomain -
Pushyour localprincipalbranch to the remotemasterbranch. - Delete your local feature co-operative
- Create and
checkouta new feature branch and do the whole thing again.
While this is many discreet steps, the period soon becomes natural, and the result is a history of changes that's piece of cake to follow.
Here are the commands for the above menses, which are explained in the Common Operations section.
git checkout -b feature-1 git add together -A git commit -chiliad 'Try use existing GetAddress feature for GetCustomer' git commit -g 'Update GetCustomer with new collection' git commit -1000 'Prepare broken tests' git rebase -i master (issue is a single commit with message 'Allow git GetCustomer to return addresses') git checkout master git pull git checkout feature-1 git rebase master git checkout master git merge feature-ane --no-ff (--no-ff forces the feature to appear clearly equally a merge in the log) git push git branch -d feature-1 git checkout -b feature-one Doing Information technology In Visual Studio 2019
Here are the same operations from above done in Visual Studio 2019. Using Visual Studio 2017 is very like.
I don't recommend using Visual Studio 2015's Git features.
Create and Checkout a New Branch
git checkout -b feature-1
Stage Changes for Commit
git add -A
Note: In well-nigh cases you won't explicitly stage. You'll only Commit All.
static void Master(cord[] args) { Console.WriteLine("How-do-you-do Git World!"); }
Commit the Change
git commit -m 'Endeavor utilize existing GetAddress characteristic for GetCustomer'
Brand a Couple More Commits
git commit -chiliad 'Update GetCustomer with new collection
git commit -m 'Gear up broken tests'
[Note shown]
Clean Upwards the Commits
git rebase -i primary (result is a single commit with message 'Permit git GetCustomer to return addresses')
Note: Visual Studio merely gives the choice to "squash" the commits. This is what you'll want to do near of the time, anyway.
Select the commits to squash, right-click, choose "Squash commits..."
Note: Visual Studio's "Squash Commits" dialog is surprisingly terrible. You finer can't edit the text, because A) there'south no fashion to create a soft return, and B) you can't paste more than the summit line of copied text.
Until it'south improve, I recommend using the command line for squashing.
Checkout and Synchronize Master
git checkout master
git pull
Checkout and Rebase Feature Onto Main
git checkout feature-1
git rebase master
Checkout and Merge Feature Into Principal
git checkout master
git merge feature-i --no-ff
Note: Visual Studio doesn't appear to have a
--no-ffchoice. Because of the value to a proficient history, I recommend doing the terminal commit at the command line using an allonym.
Note: It possible to set Git to e'er utilize
--no-ffvia themerge.ff falseoption. Still, this volition merely work in your local instance unless a repository-specific config file is used and committed.
Here'due south how that commit looks later using --no-ff at the control line. Discover how much clearer it is in both the command window and Visual Studio
Push Changes to the Remote Server
git push
Delete the Branch
git branch -d characteristic-i
Common Operations
Checkout
Checkout means to set your working folder to a item point in time. You lot're e'er choosing a reference hash of a "root". Nevertheless, you can use branch and tag names because they "signal" to a detail reference.
git checkout master git checkout feature-1 git checkout origin\master git checkout head git checkout head~iii git checkout v2.35.9 git checkout f935ea4 Branch
With the to a higher place information about how Git works, you should now sympathize what people mean by "a branch is a pointer to a reference."
You may not utilise the branch command to create a branch very often. Typically, you lot'll create and checkout a branch in a single step using this command:
git checkout -b feature-1
That single control runs these two commands:
git co-operative principal feature-1 git checkout feature-one Other branch commands.
branch -d feature-1 branch -D featue-1 Add (stage)
Adds changes to the index, to be committed.
git add -A git add Test.txt git add Models/Customer.cs Commit
Commits staged changes (in the index) to the repository.
commit commit -g 'Allow GetCustomer to testify addresses' commit -better (changes history) You'll ofttimes amend a commit if, for example, you lot realized you forgot to add a file, or mispelled something in the commit bulletin.
For case, assume this history:
1234567 Prepare spelling erors 1234568 Set my wrong spelling prepare 1234569 Add marketing copy 123456A Remove temporary files 123456B Add history file After committing "Prepare spelling erors", you lot realize you lot not merely forgot to add the latest spelling file, but likewise mispelled "errors." Yous'd execute something similar
git add together spelling.dat git commit --improve The editor would open, and you could either change the message or--more likely--just close the editor. Here's how the result might look.
afgec73 Ready spelling errors 1234568 Fix my wrong spelling fix 1234569 Add together marketing copy 123456A Remove temporary files 123456B Add together history file Hither's the important affair to notice: The "fix spelling errors" commit's ref hash has changed.
Merge
Merges changes from another branch into the electric current branch. You must have the destination co-operative checked out. For example, in order to merge the changes from the feature-1 branch into the master co-operative, you must first be in the principal branch.
git checkout main git merge characteristic-1 Rebase (changes history)
Rebase has 2 principal uses. The starting time is to change a fix of commits, usually combining ("squashing") them, so that the resulting commits are "the way information technology should take been". For example, allow's say you have these five commits in a local feature co-operative.
1234567 Ready spelling errors 1234568 Fix my wrong spelling fix 1234569 Add marketing copy 123456A Remove temporary files 123456B Add history file Using interactive rebase, yous could revise this to 2 commits:
987ABCD Fix spelling errors 987AGF2 Add together history file Notice that the original file reference hashes were non reused. New commits were created. It'southward as if these new commits had always been the only commits.
However, those other five commits still be in the reflog if needed.
Here's the command:
git rebase -i The result is a screen that guides you through the changes.
The 2d utilise is making your branch'due south commits seem like they were but washed so that they appear as the latest commits on another branch. For example, you're working on your feature branch and have cleaned upwards your commits. Now you lot want to merge your changes into master, and then you checkout primary and pull.
The problem is, while yous were working, someone else committed changes to master. So now your changes are backside that other person'due south. If you were to merge your changes, so view history, they'd be kind of jumbled in. Instead, you desire your changes to appear after the other person's.
To do that, you rebase your changes onto principal. Git will temporarily reset your branch to when you lot originally branched, merge the latest changes from master into your branch (making them identical), then replay your commits on top of this history. Each of your commits is treated equally new, so it gets a new reference hash.
The command sequence to practice this is:
git checkout master git pull git checout feature-1 git rebase master In that location's a take a chance y'all'll need to resolve conflicts between your changes and the other persons.
Cheque Condition
Shows which files are modified, new, or deleted. Besides shows which files are staged.
git status
Cheque History
Shows information about commits.
git log git log --graph --oneline Revert
The revert control "unapplies" a set of commits that appear after a commit in history, and creates a new commit at the acme. Permit's say you have this history.
6293daae maybe ok fec62970 error 2 96ca7600 mistake 1 bac6d253 working fine // reverts the top three commits git revert 96ca7600^..6293daae // reverts the middle two commits git revert 96ca7600^..fec62970 Each commit that's reverted gets its own new commit. Subsequently each reversion, you need to enter git revert --continue until all reversions are complete.
Reset (changes history)
The reset command undoes a set of commits starting at the latest. It does not create a new commit. It finer throws away the changes.
Basically, the HEAD file is updated to indicate to the given ref hash. In the example below, Head would change from 6293daae to bac6d253. The simply other question is whether the commit changes are retained in some mode.
Given this history,
6293daae perchance ok fec62970 fault 2 96ca7600 fault one bac6d253 working fine Hither are the common means to use the command to remove the tiptop three commits.
git reset bac6d253
The default control executes in --mixed style. The alphabetize is reset, only the working folder is left lone. This means any changes are gear up to add and commit.
git reset bac6d253 --soft
The index is not reset, nor is the working folder. Any changes are already added and set up to commit.
git reset bac6d253 --difficult
Both the index and working folder are reset. All changes are lost.
Tag
There are ii kinds of tags: annotated and lightweight. Annotated tags are meant for release (such every bit pushing to the remote repository), while lightweight tags are proficient for local use and cannot be pushed.
Personally, I but use annotated tags.
List all tags
git tag
Create a lightweight tag at the HEAD
git tag temp1
Create annotated tag at the Caput, with a message of the aforementioned text
git tag -a v1.2.4 -yard v1.2.4
Delete a tag
git tag -d v1.ii.four
Forcefulness an existing tag onto a different commit
git tag -a -f 63fac95
The Rules For Teams
Git is powerful, and one of its powers comes with a risk. That is the power to "rewrite history." What this means in do is that a user could push button changes to a remote repository that conflict with existing log entries others take already pulled. I show an case below, but kickoff here are the rules.
- Don't push changes from
rebase,revert,commit, ortagif you've previously pushed the affected commits.- Rebase features onto main, so merge features into master.
Beginning, hither'southward an example of an easy mistake to brand. The user is on the chief branch, commits locally, pushes to the remote, and then amends the commit and pushes again.
git commit -yard "All done!" git log --graph --oneline afea725 All done! 7c934ag Laissez passer all tests git push --Forgot to add some files! git add --all git commit --amend -thou "All done!" <= gets a warning git pull <= thinks this is the correct thing to practice, but it isn't git commit --amend -thousand "All washed!" git log --graph --oneline 024ag7d All done! 7c934ag Pass all tests git button Notice that the "All washed!" commit's reference hash is different afterward beingness amended. The problem in a higher place would be compounded if the user didn't improve the commit for awhile.
What if developer B pulls from the server after user A'south first push? B will have a history that includes ref afea725. In the meantime, the A amends and pushes. At present B pulls. Does her ref afea725 magically disappear? No. She ends upwards with something like this.
024ag7d All done! afea725 All done! 7c934ag Pass all tests Or, it could be worse. User A could forcefulness the amended commit. This will lead B to getting an even worse history.
The problems arising from using the other commands that change history are similar.
Second, if a user doesn't rebase features onto chief, then this leads to an unclear history where the co-operative the user simply finished and is pushing to the remote server looks as if it was done a week ago (or whatever). That's because, co-ordinate to the history, it has. Only that's not what the user intended.
Solving Common Problems
Pull, Merge and Rebase Conflicts
The first time I encountered a merge conflict, I was utterly flummoxed. I didn't understand what to practise, and I was definitely confused past whether my local files were actually "local" or "remote".
Here's a message from trying to push button changes that conflict with local changes.
Error: hint: Updates were rejected considering the remote contains work that you do hint: not have locally. This is usually acquired by some other repository pushing hint: to the aforementioned ref. Y'all may want to first integrate the remote changes hint: (east.g., 'git pull ...') before pushing again. hint: Meet the 'Note about fast-forrad' in 'git push --help' for details. Error encountered while pushing to the remote repository: rejected Updates were rejected because the remote contains work that yous do not have locally. This is normally caused by another repository pushing to the same ref. You lot may want to first integrate the remote changes before pushing again. And so, we do what we're told and endeavour to pull.
Nosotros're now in the midst of resolving a merge disharmonize. At this point we take 2 options: resolve the conflict, or abort. If we abort, it'due south equally if the pull never happened.
To visually merge the files, click "Conflicts: one", and then select a file that has a conflict.
There are several options for comparing and merging files. The one nosotros'll look at is the three-fashion merge. Click Merge. This will run whatever merge tool you lot've configured Git to employ.
I've used KDiff3 for years considering it's very articulate almost where the conflict is. Nevertheless, since this article is about using Visual Studio, that's what I'll demonstrate. It looks like Microsoft has improved Visual Studio's unequal/merge quite a bit, which is good!
Choose which version you want to go along, the left or correct or both. You can besides type directly into the base. We'll take the left (our) change, Accept Merge, and close the editor.
Finally, commit the merge.
And, you're ready to button.
Remote vs Local, plus Base and Backup
The "base" file is easy to sympathise; it's the version that's common to both of the inverse versions prior to when they diverged.
If you lot're dealing with a merge disharmonize from another developer, that's easy, too. The Remote will be their file, and Local will be yours.
Simply what if there'south a conflict locally between ii branches? For case, what if yous
- Branch from master into feature, commit a change there
- Go back to master and commit a change there
- Endeavour to merge characteristic into master OR rebase characteristic onto main
Visual Studio makes this pretty like shooting fish in a barrel by using clearer terminology.
In the case of Merge, the master branch file is the Target, and the characteristic file is the Source. If you used the command git mergetool, chief would be LOCAL and feature would be REMOTE.
In the case of Rebase, it's the same: 'masteris Target/LOCAL, andcharacteristic` is Source/REMOTE.
Merge vs Rebase terminology is what confuses people, and so permit's repeat it:
- If I'm on
chiefbranch and merge,masteris Target/LOCAL- If I'chiliad on
featurebranch and rebase,masteris still Target/LOCALThe target is whichever branch you lot're merging into, or rebasing onto.
If Git is configured to keep backups of the file before merging begins (mergetool.keepBackup = truthful), afterward the merge is committed there will be files with a .fill-in extension. These need to be cleaned upward.
git clean -f
I suggest setting the config file'due south mergetool.keepBackup to false. Several times I've accidentally added the fill-in files to a commit.
Concluding Word
Git's powerful, at times disruptive, ever complex, and frequently complicated. Hopefully this article has given you a solid foundation in Git's basics.
Source: https://www.softwaremeadows.com/posts/git_basics_with_visual_studio_2019/
Postar um comentário for "Vs 2017 Integrate the Remote Changes Before Pushing Again"