In this post we will show you what are the Top 10 frequently asked questions on Git and what are the solution of that. These solutions will help you to resolve if you stuck while using Git.
1. How to edit an incorrect message in Git
This is a very common question for each individuals who have just stated using Git. Suppose you have written a wrong commit message and you want to edit you can do using the following commands.
git commit --amend
It will open a editor which will allow you to change the most recent commit.
If you directly want to change the commit message, you can do using
git commit --amend -m "Edited Message"
2. How to undo the last Git commit
Suppose you accidently committed some files and you want to undo that commit. At first discard the previous commit using,
git reset --hard HEAD~1
This command will take your HEAD pointer to the 2nd last commit. After this check all the files using 'git status'. It will show you the status of the files, then you can again add the files which you want to add using git add ...
3. How to undo 'git add'
Suppose you accidentally added some files using git add but you don't want to commit those files instead want to undo those files, you can do using the below commands,
git reset <FileName> // If you want to undo a single file name
git reset // If you want to undo all due changes
4. How to delete Git Branch (Both Local and Remote)
Suppose you would like to delete a local git branch, you can use
git branch -d yourLocalBranchName
If you want to delete a Remote branch, you can use
git push origin --delete yourRemoteBranchName
5. How to delete local files in the current branch
Suppose you would like to delete all local files which you dont need to commit or to keep in your working directory, you can use the below command
git clean -f Or
git reset --hard
6. How to rename a local Git branch
Suppose you want to rename a local branch you can do using the below commands,
For current branch, git branch -m newBranchName
For other branch which you are not currently in,
git branch -m oldBranchName newBranchName
7. How to create a remote Git branch
If you want to create a remote Git branch, you can use the below commands,
git checkout -b newBranchName // to create your branch locally
git push origin newBranchName // to push new branch to your remote server
8. How to change the URL of a remote Git repository
If you want to change the URL of a remote Git Repository (say origin) you can use the following commands.
git remote set-url origin newURL
9. How to change the author name of a commit
You can modify the author info in your repository's history, you can do with this script...
git filter-branch --env-filter '
an="$GIT_AUTHOR_NAME"
am="$GIT_AUTHOR_EMAIL"
cn="$GIT_COMMITER_NAME"
cm="$GIT_COMMITTER_EMAIL"
if[ "$GIT_COMMITTER_EMAIL" = "your@email.to.match" ]
then
cn="New Committer Name"
cm="New Committer Email"
fi
if[ "$GIT_AUTHOR_EMAIL" = "your@email.to.match" ]
then
an="NEw Author Name"
am="New Author Email"
fi
export GIT_AUTHOR_NAME="$an"
export GIT_AUTHOR_EMAIL="$am"
export GIT_COMMITTER_NAME="$cn"
export GIT_AUTHOR_EMAIL="$cm"'
Above Code Taken from https://help.github.com/articles/changing-author-info
10. How to clone a repository into a specific folder
To clone a git repositor to a specific folder, you have two option
Solution 1 : Either go into that folder and type git clone URL
Solution 2 : git clone URL yourFolderName
Please Like and Share the Blog, if you find it interesting and helpful.
1. How to edit an incorrect message in Git
This is a very common question for each individuals who have just stated using Git. Suppose you have written a wrong commit message and you want to edit you can do using the following commands.
git commit --amend
It will open a editor which will allow you to change the most recent commit.
If you directly want to change the commit message, you can do using
git commit --amend -m "Edited Message"
2. How to undo the last Git commit
Suppose you accidently committed some files and you want to undo that commit. At first discard the previous commit using,
git reset --hard HEAD~1
This command will take your HEAD pointer to the 2nd last commit. After this check all the files using 'git status'. It will show you the status of the files, then you can again add the files which you want to add using git add ...
3. How to undo 'git add'
Suppose you accidentally added some files using git add but you don't want to commit those files instead want to undo those files, you can do using the below commands,
git reset <FileName> // If you want to undo a single file name
git reset // If you want to undo all due changes
4. How to delete Git Branch (Both Local and Remote)
Suppose you would like to delete a local git branch, you can use
git branch -d yourLocalBranchName
If you want to delete a Remote branch, you can use
git push origin --delete yourRemoteBranchName
5. How to delete local files in the current branch
Suppose you would like to delete all local files which you dont need to commit or to keep in your working directory, you can use the below command
git clean -f Or
git reset --hard
6. How to rename a local Git branch
Suppose you want to rename a local branch you can do using the below commands,
For current branch, git branch -m newBranchName
For other branch which you are not currently in,
git branch -m oldBranchName newBranchName
7. How to create a remote Git branch
If you want to create a remote Git branch, you can use the below commands,
git checkout -b newBranchName // to create your branch locally
git push origin newBranchName // to push new branch to your remote server
8. How to change the URL of a remote Git repository
If you want to change the URL of a remote Git Repository (say origin) you can use the following commands.
git remote set-url origin newURL
9. How to change the author name of a commit
You can modify the author info in your repository's history, you can do with this script...
git filter-branch --env-filter '
an="$GIT_AUTHOR_NAME"
am="$GIT_AUTHOR_EMAIL"
cn="$GIT_COMMITER_NAME"
cm="$GIT_COMMITTER_EMAIL"
if[ "$GIT_COMMITTER_EMAIL" = "your@email.to.match" ]
then
cn="New Committer Name"
cm="New Committer Email"
fi
if[ "$GIT_AUTHOR_EMAIL" = "your@email.to.match" ]
then
an="NEw Author Name"
am="New Author Email"
fi
export GIT_AUTHOR_NAME="$an"
export GIT_AUTHOR_EMAIL="$am"
export GIT_COMMITTER_NAME="$cn"
export GIT_AUTHOR_EMAIL="$cm"'
Above Code Taken from https://help.github.com/articles/changing-author-info
10. How to clone a repository into a specific folder
To clone a git repositor to a specific folder, you have two option
Solution 1 : Either go into that folder and type git clone URL
Solution 2 : git clone URL yourFolderName
Please Like and Share the Blog, if you find it interesting and helpful.
2. How to undo the last Git commit
ReplyDeleteSuppose you accidently committed some files and you want to undo that commit. At first discard the previous commit using,
git reset --hard HEAD~1
This command will take your HEAD pointer to the 2nd last commit. After this check all the files using 'git status'. It will show you the status of the files, then you can again add the files which you want to add using git add ...
Suggesting `--hard` as a solution is not always a good idea - and not always what someone will want to do.
If I want to undo a commit, what does that mean? Maybe I just want to add more files to this commit. Or maybe I need to make 1-2 changes to my existing commit. When someone asks me this, they usually don't want to change their working directory.
I would say that the answer would be:
git reset --mixed HEAD~1
Which keeps your working directory the same, but leaves the staging area empty. But
git reset --soft HEAD~1
is often a better solution if you just need to make 1-2 changes to your commit.
What I wanted to say is that lets say you accidentally committed some files which you do not want to commit and you do not have any other changes in your repository then you can go through this approach. By the way thanks for the information, its very helpful.
DeleteAfter reading your post again, I am pretty sure you meant --mixed, and not --hard. If you did a `git reset --hard HEAD~1`, the working directory would be clean and there would be nothing to add.
ReplyDelete