Questions tagged [git]
Git is an open-source distributed version control system (DVCS). Use this tag for questions about Git usage and workflows. Do not use this tag for general programming questions that happen to involve a Git repository. Do not use this tag for GitHub/GitHub Actions questions that do not involve git usage; use [github] or [github-actions] instead. Do not use the [github] tag for Git-related issues just because a repository happens to be hosted on GitHub.
152,380
questions
26341
votes
106
answers
14.0m
views
How do I undo the most recent local commits in Git?
I accidentally committed the wrong files to Git, but didn't push the commit to the server yet.
How do I undo those commits from the local repository?
20386
votes
41
answers
11.5m
views
How do I delete a Git branch locally and remotely?
Failed Attempts to Delete a Remote Branch:
$ git branch -d remotes/origin/bugfix
error: branch 'remotes/origin/bugfix' not found.
$ git branch -d origin/bugfix
error: branch 'origin/bugfix' not found....
13819
votes
37
answers
3.5m
views
What is the difference between 'git pull' and 'git fetch'?
What are the differences between git pull and git fetch?
11604
votes
40
answers
5.4m
views
How can I rename a local Git branch?
How can I rename a local branch which has not yet been pushed to a remote repository?
Related:
Rename master branch for both local and remote Git repositories
How do I rename both a Git local and ...
11335
votes
38
answers
5.5m
views
How do I undo 'git add' before commit?
I mistakenly added files to Git using the command:
git add myfile.txt
I have not yet run git commit. How do I undo this so that these changes will not be included in the commit?
9644
votes
53
answers
8.5m
views
How do I force "git pull" to overwrite local files?
How do I force an overwrite of local files on a git pull? My local repository contains a file of the same filename as on the server.
error: Untracked working tree file 'example.txt' would be ...
8642
votes
43
answers
8.6m
views
How do I check out a remote Git branch?
Somebody pushed a branch called test with git push origin test to a shared repository. I can see the branch with git branch -r. How do I check out the remote test branch? I've tried:
git checkout ...
8254
votes
35
answers
2.4m
views
How do I make Git forget about a file that was tracked, but is now in .gitignore?
I put a file that was previously being tracked by Git onto the .gitignore list. However, the file still shows up in git status after it is edited. How do I force Git to completely forget the file?
8128
votes
42
answers
3.2m
views
How do I remove local (untracked) files from the current Git working tree?
How do I delete untracked local files from the current working tree?
7648
votes
27
answers
3.7m
views
How to modify existing, unpushed commit messages?
I wrote the wrong thing in a commit message.
How can I change the message? The commit has not been pushed yet.
7611
votes
41
answers
10.9m
views
How do I revert a Git repository to a previous commit?
How do I revert from my current state to a snapshot made on a certain commit?
If I do git log, then I get the following output:
$ git log
commit a867b4af366350be2e7c21b8de9cc6504678a61b`
Author: Me &...
6550
votes
23
answers
1.8m
views
Move the most recent commit(s) to a new branch with Git
How do I move my recent commits on master to a new branch, and reset master to before those commits were made? e.g. From this:
master A - B - C - D - E
To this:
newbranch C - D - E
/
...
6354
votes
33
answers
4.1m
views
How do I change the URI (URL) for a remote Git repository?
I have a repo (origin) on a USB key that I cloned on my hard drive (local). I moved "origin" to a NAS and successfully tested cloning it from here.
I would like to know if I can change the ...
6282
votes
40
answers
4.1m
views
How do I discard unstaged changes in Git?
How do I discard changes in my working copy that are not in the index?
5733
votes
37
answers
3.0m
views
How can I reset or revert a file to a specific revision?
How can I revert a modified file to its previous revision at a specific commit hash (which I determined via git log and git diff)?
5718
votes
27
answers
6.3m
views
Reset local repository branch to be just like remote repository HEAD
How do I reset my local branch to be just like the branch on the remote repository?
I tried:
git reset --hard HEAD
But git status claims I have modified files:
On branch master
Changes to be ...
5647
votes
19
answers
6.0m
views
How do I push a new local branch to a remote Git repository and track it too?
How do I:
Create a local branch from another branch (via git branch or git checkout -b).
Push the local branch
to the remote repository (i.e. publish), but make it
trackable so that git pull and git ...
5459
votes
26
answers
4.6m
views
How to determine the URL that a local Git repository was originally cloned from
I pulled a project with several forks on GitHub, but forgot which fork it was. How do I determine which fork I pulled?
5430
votes
46
answers
3.8m
views
How do I squash my last N commits together?
How do I squash my last N commits together into one commit?
5371
votes
36
answers
3.5m
views
How do I resolve merge conflicts in a Git repository?
How do I resolve merge conflicts in my Git repository?
5352
votes
37
answers
1.6m
views
How do I add an empty directory to a Git repository?
How do I add an empty directory (that contains no files) to a Git repository?
4882
votes
35
answers
3.2m
views
Undo a Git merge that hasn't been pushed yet
I accidentally ran git merge some_other_branch on my local master branch. I haven't pushed the changes to origin master. How do I undo the merge?
After merging, git status says:
# On branch master
# ...
4823
votes
31
answers
2.1m
views
How can I delete a remote tag?
How can I delete a Git tag that has already been pushed?
4782
votes
48
answers
1.8m
views
How do I clone all remote branches?
My master and development branches are tracked remotely on GitHub. How do I clone both these branches?
4719
votes
32
answers
1.3m
views
How do I update or sync a forked repository on GitHub?
I forked a project, made changes, and created a pull request which was accepted. New commits were later added to the repository. How do I get those commits into my fork?
4546
votes
41
answers
1.6m
views
How do I remove a submodule?
How do I remove a Git submodule?
Why can't I do
git submodule rm module_name?
4342
votes
40
answers
4.9m
views
How do I delete a commit from a branch?
How do I delete a commit from my branch history? Should I use git reset --hard HEAD?
4320
votes
20
answers
2.0m
views
Undoing a git rebase
How do I easily undo a git rebase? A lengthy manual method is:
checkout the commit parent to both of the branches
create and checkout a temporary branch
cherry-pick all commits by hand
reset the ...
4120
votes
141
answers
4.5m
views
Message 'src refspec master does not match any' when pushing commits in Git
I clone my repository with:
git clone ssh://xxxxx/xx.git
But after I change some files and add and commit them, I want to push them to the server:
git add xxx.php
git commit -m "TEST"
git push ...
4074
votes
52
answers
3.3m
views
How do I get the current branch name in Git?
How do I get the name of the current branch in Git?
4042
votes
23
answers
1.8m
views
Make an existing Git branch track a remote branch?
I know how to make a new branch that tracks remote branches, but how do I make an existing branch track a remote branch?
I know I can just edit the .git/config file, but it seems there should be an ...
4036
votes
7
answers
4.8m
views
How do I clone a specific Git branch? [duplicate]
Git clone will clone remote branch into local.
Is there any way to clone a specific branch by myself without switching branches on the remote repository?
3936
votes
14
answers
1.1m
views
Move existing, uncommitted work to a new branch in Git
I started some work on a new feature and after coding for a bit, I decided this feature should be on its own branch.
How do I move the existing uncommitted changes to a new branch and reset my ...
3887
votes
15
answers
1.2m
views
Remove a file from a Git repository without deleting it from the local filesystem
I want to remove a file from my repository.
git rm file_to_remove.txt
will remove the file from the repository, but it will also remove the file from the local file system. How do I remove this file ...
3875
votes
27
answers
2.4m
views
View the change history of a file using Git versioning
How do I view the history of an individual file with complete details of what has changed?
git log -- [filename] shows me the commit history of a file, but how do I see the file content that changed?
3767
votes
33
answers
1.2m
views
How do I stash only one file out of multiple files that have changed?
How do I stash only one of the multiple changed files on my branch?
3712
votes
26
answers
2.9m
views
How do I create a remote Git branch?
I created a local branch. How do I push it to the remote server?
UPDATE: I have written a simpler answer for Git 2.0 here.
3671
votes
33
answers
3.0m
views
Git refusing to merge unrelated histories on rebase
During git rebase origin/development the following error message is shown from Git:
fatal: refusing to merge unrelated histories
Error redoing merge 1234deadbeef1234deadbeef
My Git version is 2.9.0. ...
3599
votes
30
answers
656k
views
Commit only part of a file's changes in Git
When I make changes to a file in Git, how can I commit only some of the changes?
For example, how could I commit only 15 lines out of 30 lines that have been changed in a file?
3588
votes
30
answers
2.3m
views
How do I list all the files in a commit?
How can I print a plain list of all files that were part of a given commit?
Although the following lists the files, it also includes unwanted diff information for each:
git show ...
3472
votes
41
answers
1.5m
views
Git is not working after macOS update ("xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools")
I updated to the latest OS, and/or restarted my computer (this happens on every major update, but this time all I did was restart my computer on 2022-09-13).
This morning I navigated to my work's ...
3448
votes
13
answers
2.2m
views
How do you push a tag to a remote repository using Git?
I added a tag to the master branch on my machine:
git tag mytag master
How do I push this to the remote repository? Running git push gives the message:
Everything up-to-date
However, the remote ...
3448
votes
12
answers
2.1m
views
Difference between "git add -A" and "git add ."
What is the difference between git add [--all | -A] and git add .?
3354
votes
35
answers
1.4m
views
How do I make git use the editor of my choice for editing commit messages?
How do I globally configure git to use a particular editor (e.g. vim) for commit messages?
3345
votes
15
answers
2.3m
views
What does cherry-picking a commit with Git mean?
What does git cherry-pick <commit> do?
3209
votes
22
answers
1.5m
views
How do I modify a specific commit?
I have the following commit history:
HEAD
HEAD~
HEAD~2
HEAD~3
git commit --amend modifies the current HEAD commit. But how do I modify HEAD~3?
3206
votes
30
answers
1.1m
views
How do I find and restore a deleted file in a Git repository?
Say I'm in a Git repository. I delete a file and commit that change. I continue working and make some more commits. Then, I discover that I need to restore that file after deleting it.
I know I can ...
3194
votes
14
answers
2.6m
views
I ran into a merge conflict. How do I abort the merge?
I used git pull and had a merge conflict:
unmerged: some_file.txt
You are in the middle of a conflicted merge.
How do I abandon my changes to the file and keep only the pulled changes?
3156
votes
12
answers
2.3m
views
How can I see the differences between two branches?
How can I see the differences between branches branch_1 and branch_2?
3124
votes
31
answers
4.5m
views
`git fetch` a remote branch
The remote repository contains various branches such as origin/daves_branch:
$ git branch -r
origin/HEAD -> origin/master
origin/daves_branch
origin/master
How do I switch to daves_branch in the ...