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.

Filter by
Sorted by
Tagged with
25456 votes
103 answers
12.5m 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?
19983 votes
42 answers
10.8m 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....
13558 votes
38 answers
3.3m views

What is the difference between 'git pull' and 'git fetch'?

What are the differences between git pull and git fetch?
  • 277k
11151 votes
40 answers
4.9m views

How do I rename a local Git branch?

How do 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 ...
  • 121k
10950 votes
38 answers
4.9m 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?
  • 151k
9224 votes
50 answers
7.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 ...
8303 votes
42 answers
7.8m 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 ...
  • 86.5k
7912 votes
41 answers
3.0m 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?
  • 339k
7726 votes
33 answers
2.1m 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?
  • 94.8k
7651 votes
27 answers
3.4m 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.
7613 votes
41 answers
9.7m 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 &...
  • 76.4k
6276 votes
21 answers
1.6m 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 / ...
6039 votes
41 answers
3.8m views

How do I discard unstaged changes in Git?

How do I discard changes in my working copy that are not in the index?
  • 339k
5830 votes
34 answers
3.3m 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 ...
  • 572k
5552 votes
35 answers
2.8m views

How do I reset or revert a file to a specific revision?

How do I revert a modified file to its previous revision at a specific commit hash (which I determined via git log and git diff)?
  • 65.7k
5457 votes
18 answers
5.4m 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 ...
  • 56.6k
5383 votes
25 answers
5.5m 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 ...
  • 151k
5300 votes
36 answers
3.4m views

How do I resolve merge conflicts in a Git repository?

How do I resolve merge conflicts in my Git repository?
  • 119k
5223 votes
26 answers
4.1m 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?
  • 52.5k
5171 votes
37 answers
1.4m 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?
5035 votes
43 answers
3.2m views

How do I squash my last N commits together?

How do I squash my last N commits together into one commit?
4751 votes
35 answers
3.0m 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 # ...
  • 80.5k
4683 votes
47 answers
1.7m 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?
4575 votes
27 answers
1.8m views

How can I delete a remote tag?

How can I delete a Git tag that has already been pushed?
4575 votes
31 answers
1.2m 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?
  • 61.7k
4330 votes
37 answers
1.4m views

How do I remove a submodule?

How do I remove a Git submodule? Why can't I do git submodule rm module_name?
4116 votes
37 answers
4.2m 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?
  • 151k
4095 votes
19 answers
1.7m 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 ...
  • 57.2k
3961 votes
23 answers
1.7m 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 ...
  • 206k
3899 votes
131 answers
4.0m 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 ...
  • 39.3k
3845 votes
7 answers
4.4m 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?
  • 41.1k
3768 votes
13 answers
939k 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 ...
3749 votes
49 answers
2.8m views

How do I get the current branch name in Git?

How do I get the name of the current branch in Git?
  • 44.5k
3713 votes
26 answers
2.3m 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?
  • 38.2k
3701 votes
13 answers
1.1m 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 ...
  • 37.9k
3649 votes
32 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?
  • 99.3k
3640 votes
25 answers
2.7m 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.
3454 votes
28 answers
579k 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?
3451 votes
30 answers
2.1m 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 ...
3419 votes
32 answers
2.6m 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. ...
3380 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 .?
  • 112k
3340 votes
37 answers
1.4m 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 ...
  • 78.4k
3188 votes
32 answers
1.3m 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?
  • 74.9k
3164 votes
12 answers
1.8m 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 ...
  • 118k
3164 votes
13 answers
2.0m views

What does cherry-picking a commit with Git mean?

What does git cherry-pick <commit> do?
  • 44.1k
3147 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 ...
  • 40.9k
3123 votes
14 answers
2.5m 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?
  • 32.4k
3048 votes
19 answers
1.3m 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?
  • 42.6k
3018 votes
31 answers
4.1m 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 ...
  • 34k
2917 votes
21 answers
2.7m views

How do I clone a Git repository into a specific folder?

The command git clone git@github.com:whatever creates a directory named whatever containing a Git repository: ./ whatever/ .git I want the contents of the Git repository cloned into my ...
  • 37.3k

1
2 3 4 5
2956