Home

Awesome

<div align="center"> <h1>Practical Git Guide</h1> <h3>Practical guide to everyday Git commands</h3> <p> <a name="stars"><img src="https://img.shields.io/github/stars/sadanandpai/git-lean-guide?style=for-the-badge"></a> <a name="forks"><img src="https://img.shields.io/github/forks/sadanandpai/git-lean-guide?logoColor=green&style=for-the-badge"></a> <a name="license"><img src="https://img.shields.io/github/license/sadanandpai/git-lean-guide?style=for-the-badge"></a> </p> <img width="654" alt="image" src="https://user-images.githubusercontent.com/12962887/170864903-b331fc72-71f6-429b-81d4-b5cceb9d4b9f.png" /> <h4>Solutions to 90% of your daily git challenges</h4>

Show your support by giving a ⭐ to this repo

</div> <br>

Contents

  1. I want to clone a project and start going through the code.
  2. I have cloned the repo. But I am not able to see the proper code!!!
  3. Someone has made a few changes in the code and asked me to pull those changes. What should I do?
  4. I have modified the code and added changes. How do I commit my changes?
  5. What if I want only some of my files added and pushed instead of all the changes?
  6. I have modified/formatted some code while going through it. Now I want the code to be back to the state as it was.
  7. I have committed my changes. How can I undo my change?
  8. I have made some changes to the branch. Also, I wanted to pull the new changes. But it is not working.
  9. After applying the stash, I am getting a lot of conflicts in the code.
  10. I have made some code changes. But I want to commit to a new separate branch.
  11. I just committed but forgot to add a few files to the commit. Is there a way to update the same commit with some modifications?
  12. I am asked to raise a Pull Request (PR) to a branch. What am I supposed to do?
  13. I do not want a branch anymore. How can I delete the branch?
  14. I want to rename my local and remote branches. How can I do it?
  15. I have made some changes to the code on the branch on which all of the developers are working. How can I publish my changes?
  16. I created a commit and also pushed it. Is it possible to update that commit now?
  17. I have created single/multiple commits. When I am trying to push my changes, getting a rejected message. I am stuck!!!
  18. I followed the above steps but got conflicts after git pull.
  19. I have created single/multiple commits. When I am trying to push my changes, getting a rejected message. Can I pull the new changes without merging the commit (Rebase)?
  20. I have raised a PR (Pull Request). But it is showing conflicts.
  21. I have many commits. How can I transform them into a single commit (squash)?
  22. I have many commits. How can I transform them into a single commit (squash) with just commands?
  23. I am trying to rebase my branch with the same or another branch. As I have many commits, I am getting a lot of conflicts on every commit to rebase.
  24. I have made changes and committed to a branch. I want to copy the same changes to another branch.
  25. I have tried to cherry-pick as shown in [24]. But I am getting conflicts.
  26. I have multiple commits which I want to move to a different branch.
  27. I have pushed my changes and got it merged. I want to revert it immediately.
  28. How do I reset the code of my branch to the code of a different branch?
  29. I want to delete/undo the previous commit from my branch which I have already pushed. I am not looking for a revert. I just want to delete it.
<br>

1. I want to clone a project and start going through the code.

The repo URL can be found inside the repo.

<img width="850" alt="repo clone" src="https://user-images.githubusercontent.com/12962887/218247065-a1b39df3-172a-49ff-9cd0-3cc79f6fcf19.png">

<b>Github repo link example</b> image

<b>Bitbucket repo link example</b> image

<br> <br>
<br>

2. I have cloned the repo. But I am not able to see the proper code!!!

By default the main/master branch is active. Ask which branch has the relevant code

<img width="619" alt="checkout" src="https://user-images.githubusercontent.com/12962887/218248101-8253501b-4537-4b5c-98f3-71ae6f84c417.png">

Note: The checkout command can also be used to switch the branch. git checkout <branch-name> <br> <br>


<br>

3. Someone has made a few changes in the code and asked me to pull those changes. What should I do?

<img width="587" alt="pull" src="https://user-images.githubusercontent.com/12962887/173235993-7f2ba191-d9bb-4a78-aec3-35f326e46325.png"> <br> <br>
<br>

4. I have modified the code and added changes. How do I commit my changes?

First stage all the files and then commit your changes. You can create multiple commits.

<img width="608" alt="commit" src="https://user-images.githubusercontent.com/12962887/180645381-c77af0e5-6d4c-4991-84d7-1572636e34a3.png"> <br> <br>
<br>

5. What if I want only some of my files added and pushed instead of all the changes?

You can add files by mentioning the file/files with relative or full path. You can add files one by one or multiple files at a time using the commands

<img width="619" alt="add" src="https://user-images.githubusercontent.com/12962887/180643543-7ca529df-5126-4a73-9dd1-703af2f03fba.png">

Similarly, to unstage a file use the command git reset <file>

<br> <br>
<br>

6. I have modified/formatted some code while going through it. Now I want the code to be back to the state as it was.

<img width="719" alt="reset" src="https://user-images.githubusercontent.com/12962887/180649258-1dddabf1-bdc2-4706-a537-26eaad4ba99e.png"> <br> <br>
<br>

7. I have committed my changes. How can I undo my change?

You can undo the commit by resetting the HEAD. If you just want to undo the commit but let the changes be present then use the soft attribute else if you do want the commit along with the changes then use the hard attribute

<br> <br>
<br>

8. I have made some changes to the branch. Also, I wanted to pull the new changes. But it is not working.

The command git pull may not work if the changes are done by someone else to the same files which you have also modified.

where n is the stash number. To get the list of stashes git stash list

<img width="918" alt="stash" src="https://user-images.githubusercontent.com/12962887/181293105-77ec0867-0d95-4f44-99e4-0b1003a6310b.png">

Note: You can stash multiple changes and bring them back as and when you like to

<br> <br>
<br>

9. After applying the stash, I am getting a lot of conflicts in the code.

If there are changes in the code on the region of the stashed code, it is expected to get conflicts. You will have to manually resolve all the conflicts. (Do it carefully)

<img width="596" alt="stash conflicts" src="https://user-images.githubusercontent.com/12962887/180613095-a8e08426-1d15-4a31-956b-55bfdfbdb84d.png">

Below is the view of the VSCode, which helps in easily resolving the conflicts. VSCode conflict

<br> <br>
<br>

10. I have made some code changes. But I want to commit to a new separate branch.

You can create a separate branch out of the current branch and commit it. This works both if you have already made changes or are yet to start making changes.

To switch between the branches use the command git checkout <original-branch-name>

<img width="631" alt="image" src="https://user-images.githubusercontent.com/12962887/218248599-d0f3e478-3684-4f29-8fb4-2eb492975109.png">

Alternatively, checkout command can also be used to create the branch. git checkout -b <my-branch-name>

<br>

Note: my-branch-name is your local branch and not available for anyone else unless you push it

<br>
<br>

11. I just committed but forgot to add a few files to the commit. Is there a way to update the same commit with some modifications?

Yes. You can update the commit by amending your changes.

To update with new commit message git commit --amend -m 'My new commit message' (This command can also be used to simply update the previous commit message without any code modifications)

--no-edit is used to avoid the prompt to edit the commit message. If you want to modify the commit message as well during the update of the commit, then do not include -m along with your commit message

<img width="587" alt="amend" src="https://user-images.githubusercontent.com/12962887/180615625-baa2b6f5-2ca5-454a-bfbf-d7115537cdbf.png">

Note: The amend updates the previous commit without creating a new one on top of the previous. (in reality, Git discards the previous commit and replaces it with a new commit) <br> <br>


<br>

12. I am asked to raise a Pull Request (PR) to a branch. What am I supposed to do?

You can follow the same steps as given in the previous question. Once done you will push the code and raise a PR. It's that simple. Here we assume you are on the 'develop' branch and raising PR to the 'main' branch.

<img width="872" alt="" src="https://user-images.githubusercontent.com/12962887/180644568-b2fc1d78-399c-443d-b714-3ab1cafb83bd.png">

The URL to raise the PR will be automatically available as shown above. Use the link and open it in the browser.

image

Now select the base branch to which you want to raise a PR and click on 'Create a PR'

<br> <br>
<br>

13. I do not want a branch anymore. How can I delete the branch?

To delete a branch locally, check out a different branch than the one you want to delete. Here we will delete the branch named 'develop'

To delete the branch from remote as well

<img width="808" alt="image" src="https://user-images.githubusercontent.com/12962887/181604592-a08d6cd4-105e-460b-b694-558f4fcf9680.png">

Note: If -d does not allow to delete a branch, use the -D. Example: git branch -D <branch-name>

<br> <br>
<br>

14. I want to rename my local and remote branches. How can I do it?

To rename a branch, checkout to the branch and rename it.

<remote> is usually origin

<img width="752" alt="image" src="https://user-images.githubusercontent.com/12962887/181599454-22970ea6-8ee4-424c-acfb-5c0c17179c4a.png"> <br> <br>
<br>

15. I have made some changes to the code on the branch on which all of the developers are working. How can I publish my changes?

To move the changes from your local machine to remote (called origin), follow the below steps in your terminal

<img width="828" alt="image" src="https://user-images.githubusercontent.com/12962887/170865776-dea5f82e-6ae6-4f60-b70a-0fc8d37fd741.png"> <br> <br>
<br>

16. I created a commit and also pushed it. Is it possible to update that commit now?

Yes. You can update the commit even after it is pushed. Everything will follow as mentioned in the previous question, but you will have to force push.

<img width="783" alt="image" src="https://user-images.githubusercontent.com/12962887/180615816-743abb81-765c-4723-b44f-faf00da3a820.png">

Note: You need to be very careful while pushing forcefully, as it may eliminate other commits if someone has done in between. Make sure you are working on the branch and no one else is simultaneously working on the same or branching out from the branch at your commit.

<br> <br>
<br>

17. I have created single/multiple commits. When I am trying to push my changes, getting a rejected message. I am stuck!!!

The rejection could be because the remote branch might be ahead of the local branch. Different techniques can be used here to achieve sync.

image

If there are conflicts, then resolve them manually to proceed ahead as shown below.

<br> <br>
<br>

18. I followed the above steps but got conflicts after git pull.

If there are code changes on the same region from multiple commits, conflicts will occur. You need to resolve all the conflicts and proceed.

pull conflict

Note: If something goes wrong, in any of the above steps, then there is nothing to panic about. Just run git merge --abort and redo the steps.

<br> <br>
<br>

19. I have created single/multiple commits. When I am trying to push my changes, getting a rejected message. Can I pull the new changes without merging the commit (Rebase)?

Yes. You can pull the changes without a merge. This is called Rebase. I know you have heard it a lot. It is very simple though.

<img width="982" alt="rebase" src="https://user-images.githubusercontent.com/12962887/181307242-3b2ff96a-1896-43b2-814c-e22cfbc50320.png">

If you get conflicts, then solve all the conflicts. Then

image

Note: If something goes wrong, in any of the above steps, then there is nothing to panic about. Just run git rebase --abort and redo the steps.

<br> <br>
<br>

20. I have raised a PR (Pull Request). But it is showing conflicts.

<img height="600px" src="https://user-images.githubusercontent.com/12962887/181507186-92a6c427-553c-4c1e-b87a-0394d23a1bad.png" alt="pr conflict" />

PR will show conflicts if the new changes added to the source branch are conflicting with your changes or your branch is lagging.

There are 2 main approaches to solve this.

Follow any one of the approaches. Don't try both of them.

Assuming that your branch is develop and the source branch is main

Merge approach

merge

Rebase approach

rebase

Note: You may have to run git rebase --continue multiple times if there are multiple conflicts on your multiple commits.

<br> <br>
<br>

21. I have many commits. How can I transform them into a single commit (squash)?

Converting multiple commits into one is known as Squashing. We will achieve this by rebasing with the help of the interactive feature of the editor (VSCode). This will be both easier and simple.

<img width="654" alt="squash" src="https://user-images.githubusercontent.com/12962887/181516582-3493d359-0e04-4679-9b9b-2c120aa1adf9.png">

image

image

Note: If you had already pushed the commits, then you will have to use the command git push -f to reflect squash on the remote branch as well.

<br> <br>
<br>

22. I have many commits. How can I transform them into a single commit (squash) with just commands?

You can use the technique of undoing to achieve this easily.

Note: If you had already pushed the commits, then you will have to use the command git push -f to reflect squash on the remote branch as well.

<br> <br>
<br>

23. I am trying to rebase my branch with the same or another branch. As I have many commits, I am getting a lot of conflicts on every commit to rebase.

<br> <br>
<br>

24. I have made changes and committed to a branch. I want to copy the same changes to another branch.

To copy the changes of a commit from one branch to another, you can use cherry pick. First obtain the commit id of the commit, which you want to copy.

<img width="607" alt="cherry pick" src="https://user-images.githubusercontent.com/12962887/181609051-5ced1192-0372-45f9-97bf-3491ce070fbf.png"> <br> <br>
<br>

25. I have tried to cherry-pick as shown in 24. But I am getting conflicts.

If you get any conflicts, resolve the conflicts first. Once all the conflicts are resolved, they continue the cherry-pick.

<img width="719" alt="cherry pick conflict" src="https://user-images.githubusercontent.com/12962887/181591132-b43e8c0c-609b-4a25-9150-58d7e66b2d5e.png">

Note: If something goes wrong in between, reset the process by using the command git cherry-pick --abort (You can start the process again)

<br> <br>
<br>

26. I have multiple commits which I want to move to a different branch.

To copy a range of commits from one branch to another, you can note down the older commit id from history and the newer commit id.

<img width="719" alt="cherry pick multiple" src="https://user-images.githubusercontent.com/12962887/181591920-d5c07c1e-3347-4ec5-8352-b3dd7561e109.png"> <br> <br>
<br>

27. I have pushed my changes and got it merged. I want to revert it immediately.

Revert creates a reverse commit where the changes made will reverse and is created as a new commit. To revert a particular commit first, obtain its commit id.

<img width="808" alt="revert" src="https://user-images.githubusercontent.com/12962887/181597059-5d121194-9f29-425a-bcc1-7cb466856187.png"> <br> <br>
<br>

28. How do I reset the code of my branch to the code of a different branch?

When resetting a branch to a different branch, the codebase on your branch will become the same as the other branch. To achieve this, you can use the 'reset' command.

In this case, 'your-branch-name' will match the codebase of 'source-branch'

<img width="588" alt="reset" src="https://user-images.githubusercontent.com/12962887/181597315-05e8bd39-f9af-40f3-a104-8a11b8ffb52b.png"> <br> <br>
<br>

29. I want to delete/undo the previous commit from my branch which I have already pushed. I am not looking for a revert. I just want to delete it.

Revert will reverse the changes creating a new commit. If you want to remove the previous commit, then you can undo and force push the branch.

<img width="707" alt="undo" src="https://user-images.githubusercontent.com/12962887/181600183-a5fb2095-d0cc-4c4e-ae68-3f4eea209979.png">

Note: Force push is needed as the history of the branch is changing in this. If the commit is not pushed then it is not needed.

<br> <br>

License

This repository is MIT licensed. Read more