forked from MicrosoftDocs/azure-devops-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhowto.yml
136 lines (107 loc) · 10.1 KB
/
howto.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
### YamlMime:FAQ
metadata:
title: Git frequently asked questions
titleSuffix: Azure Repos
description: Learn some tips and how-to address less-common Git tasks.
ms.assetid: b1262306-214c-4422-8eda-d03d05825241
ms.service: azure-devops-repos
ms.topic: faq
ms.date: 08/22/2024
monikerRange: '<= azure-devops'
ms.subservice: azure-devops-repos-git
title: Git FAQs
summary: |
[!INCLUDE [version-lt-eq-azure-devops](../../includes/version-lt-eq-azure-devops.md)]
In this article, find answers to frequently asked questions about Git, specifically tailored for Azure Repos. Whether you're looking to manage remote branches, identify your current branch, or handle other less-common Git tasks, this guide provides helpful tips and solutions. Dive into the following sections to enhance your Git workflow and resolve common issues.
sections:
- name: Ignored
questions:
- question: |
How can I easily download a remote branch to my local repository?
answer: |
First, ensure you have an `origin` repository configured. You should have this if you cloned your repo using [`git clone`](clone.md). When you check out a branch that doesn't exist locally, Git checks if there's a remote branch with the same name. If there is, Git creates a local branch that references the remote branch. Use `git pull` to download the commits and update the branch history locally.
- question: |
How can I find out which branch I'm working in?
answer: |
Run `git branch` with no arguments to show the local branches and highlight the one you've checked out. In Visual Studio, the status bar also displays the current branch when you're working with a project stored in a local Git repository.
- question: |
When should I make Git commits?
answer: |
It's best practice to make separate commits for logically distinct changes. Think of commits as entries in a logbook. Whenever you make a change worth noting, record it in a commit. A popular approach is to allow frequent local commits, but squash them through [rebasing](rebase.md) before pushing. This provides flexibility while keeping the commit history streamlined.
- question: |
If every branch retains its full commit history, doesn't that make the commit history of *main* hard to follow over time?
answer: |
Large projects with many commits and contributors can result in a `main` branch history that reflects the development of topic branches more than the overall project. Git allows you to condense commits on branches through [squashing commits and rebasing](rebase.md). Squashing commits makes the branch history less verbose, simplifying the commit history on the main branch once merged.
- question: |
How can I find out who made a specific change to a file?
answer: |
Use the `git blame` command to find out who made a particular change to a file. From your local repository, you can run `git blame` with the `-L` parameter, specifying which lines of interest. `Blame` produces formatted output showing the commit that last updated the line and the name of the person who made the commit.
> [!div class="tabbedCodeSnippets"]
```Git CLI
> git blame Example_repo -L 20,+40 # show the blame output for the next 40 lines starting at line 20
215d1108 (Example User 2015-11-21 09:54:23 -0800 20) line 20 of the code
215d1108 (Example User 2015-11-21 09:54:23 -0800 21) line 21 of the code
215d1108 (Example User 2015-11-21 09:54:23 -0800 22) line 22 of the code
```
`Blame` searches the commit history for you. You can also review a file's history in the web portal to determine
who made a change and when. Open Code Explorer for your repository and branch, then select the file of interest. Azure Repos shows a complete
commit history for that file on the current branch.
- question: |
I made changes to some files and now I can't check out to a different branch or rebase my work.
answer: |
Checking out to a different branch in Git affects the state of files on your file system. Git uses the commit history to make sure you're working with the files that represent the state of your branch. If you try to change branches while you have uncommitted changes, those changes would be overwritten during checkout. Because Git doesn't want you to accidentally lose your changes, it prevents the checkout from happening. You have two options:
- Abandon the changes and return to the most recent commit. See [undoing changes in Git](undo.md) for instructions on how to roll back to the most recent commit.
- Commit the changes. See [saving your work in Git with commits](commits.md).
- [Stashing](howto.yml#i-did-some-work-but-need-to-switch-to-something-else--how-can-i-save-my-work-for-later-without-committing-the-changes-) your current work, saving the changes for later and cleaning up the workspace to the last commit.
- question: |
Pull request is unable to merge with this message: 'Unable to merge automatically: One of internal git objects (blob, tree, commit or tag) is too large which caused TF401022 exception. You can try to use LFS, split your merge or big commit into several small.'
answer: |
This issue is related to merge conflicts in large binary files. The current limit for files is 100MB. The workaround is to resolve merge conflicts locally by merging the target into the source locally, resolving conflicts, and pushing the changes.
Git LFS (Large File Storage) is recommended for storing large binary files, not only to avoid conflicts but also to manage overall repository size, which affects clone and push times.
- question: |
I did some work but need to switch to something else. How can I save my work for later without committing the changes?
answer: |
If you want to save your changes without committing them, use Git `stash`. Stash saves the current staged and unstaged changes in your branch and reverts your branch to the state of the last commit. You can then switch to another branch, do your work, and later run `stash apply` to restore your changes.
```Git CLI
git stash
Saved working directory and index state WIP on feature1: be26067 updated endpoint docs
HEAD is now at be26067
```
When you run [`git stash apply`], the most recently stashed changes get applied to your current branch. If there's a conflict, [`stash`] restores the changes for the files that don't conflict and creates conflict markers in the files that do. You should merge the changes manually in this case.
Once you're done with the stash, delete it with [`git stash drop`]. This command removes the last set of stashed changes.
You can have multiple stashes, but managing them requires more manual manipulation as you have to explicitly apply and drop stashes. Learn more from the [Git Stash documentation](https://git-scm.com/book/en/v2/Git-Tools-Stashing-and-Cleaning).
- question: |
How can I change the default editor for Git command-line tools?
answer: |
By default, command-line Git uses a command-line editor when asking for commit messages, performing rebases, and other work that requires additional information to
complete. The default editor is configured using `git config`:
> [!div class="tabbedCodeSnippets"]
```Git CLI
> git config core.editor _path_to_editor_ _options_to_editor_
```
Git For Windows makes it easy to set notepad as the editor:
> [!div class="tabbedCodeSnippets"]
```Git CLI
> git config core.editor notepad
```
This command configures Windows Notepad to edit Git information as needed and properly pass through the text from Git to Notepad. You can also specify
> [!div class="tabbedCodeSnippets"]
```Git CLI
> git config format.commitMessageColumns 72
```
To keep the text columns in the commit messages to the preferred 72 and line wrap after hitting that character limit on a line.
- question: |
How can I change the username and email displayed in my commits?
answer: |
Git puts a user name and email address information inside each commit, and Azure Repos uses this information when viewing commits and when working with pull requests.
If you're working on the command line, you can update the name and email information displayed using the `git config` command:
> [!div class="tabbedCodeSnippets"]
```Git CLI
> git config --global user.email "example-user@example-site.com"
> git config --global user.name "Example User"
```
The `--global` option sets the email and name included in commits for all Git repositories on this system. If you want to change the settings for a single
repository, you must change to the directory where the Git repository is located and run the above commands without the `--global` flag.
You can also change the name and email settings from Visual Studio. From the **Git** menu, select **Settings** In the **Options** dialog, select **Git Global Settings** or **Git Repository Settings** > **General**.
Visual Studio 2019 version 16.8 and later versions provides a Git version control experience while maintaining the **Team Explorer** Git user interface. To use **Team Explorer**, uncheck **Tools** > **Options** > **Preview Features** > **New Git user experience** from the menu bar. You can exercise Git features from either interface interchangeably.
In Team Explorer, choose **Settings** and under **Git**, select the **Global Settings** or **Repository Settings** link.