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.

git
Filter by
Sorted by
Tagged with
26301 votes
106 answers
13.9m 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?
20387 votes
41 answers
11.4m 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....
13804 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?
Pablo Fernandez's user avatar
11585 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 ...
Forrest's user avatar
  • 125k
11317 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?
oz10's user avatar
  • 156k
9624 votes
53 answers
8.4m 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 ...
Jakub Troszok's user avatar
8617 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 ...
Juri Glass's user avatar
  • 89.7k
8222 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?
Ivan's user avatar
  • 101k
8108 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?
readonly's user avatar
  • 349k
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.
Laurie Young's user avatar
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 &...
Crazy Serb's user avatar
  • 76.3k
6540 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 / ...
Mark A. Nicolosi's user avatar
6318 votes
33 answers
4.0m 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 ...
Bite code's user avatar
  • 587k
6271 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?
readonly's user avatar
  • 349k
5728 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)?
Hates_'s user avatar
  • 67.6k
5705 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 ...
hap497's user avatar
  • 159k
5635 votes
19 answers
5.9m 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 ...
Roni Yaniv's user avatar
  • 58.6k
5445 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?
Tim's user avatar
  • 54.7k
5412 votes
46 answers
3.7m views

How do I squash my last N commits together?

How do I squash my last N commits together into one commit?
markdorison's user avatar
5366 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?
Spoike's user avatar
  • 121k
5343 votes
37 answers
1.5m 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?
Laurie Young's user avatar
4875 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 # ...
Matt Huggins's user avatar
  • 82.3k
4814 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?
markdorison's user avatar
4781 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?
Peter Coulton's user avatar
4711 votes
31 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?
Lea Hayes's user avatar
  • 63.3k
4533 votes
40 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?
R. Martinho Fernandes's user avatar
4332 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?
hap497's user avatar
  • 159k
4303 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 ...
webmat's user avatar
  • 59.5k
4105 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 ...
sinoohe's user avatar
  • 41.4k
4056 votes
52 answers
3.2m views

How do I get the current branch name in Git?

How do I get the name of the current branch in Git?
mike628's user avatar
  • 47.7k
4040 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 ...
Pat Notz's user avatar
  • 211k
4030 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?
Scud's user avatar
  • 43.1k
3932 votes
13 answers
1.0m 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 ...
Dane O'Connor's user avatar
3884 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 ...
mveerman's user avatar
  • 39.7k
3866 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?
Richard's user avatar
  • 40k
3764 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?
Rachel's user avatar
  • 102k
3709 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.
Jesper Rønn-Jensen's user avatar
3660 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. ...
Shubham Chaudhary's user avatar
3592 votes
30 answers
652k 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?
freddiefujiwara's user avatar
3583 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 ...
Philip Fourie's user avatar
3469 votes
40 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 ...
dustbuster's user avatar
  • 81.2k
3445 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 .?
cmcginty's user avatar
  • 115k
3438 votes
13 answers
2.1m 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 ...
Jonas's user avatar
  • 125k
3347 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?
brasskazoo's user avatar
  • 77.3k
3333 votes
15 answers
2.3m views

What does cherry-picking a commit with Git mean?

What does git cherry-pick <commit> do?
Rahul's user avatar
  • 46k
3205 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 ...
avdgaag's user avatar
  • 41.7k
3202 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?
Sam Liao's user avatar
  • 44.7k
3192 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?
Gwyn Morfey's user avatar
  • 33.1k
3143 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?
isuruanu's user avatar
  • 31.5k
3121 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 ...
David's user avatar
  • 35.4k

1
2 3 4 5
3045