- Git Initialization
git init
- Git commit
git commit -m "Intial commit"
- Add Remote repository
git remote add <shortname> <url>
Example:
git remote add origin https://[email protected]/worksmit/demo.git
- Check name of all remote reposiotries that Git has stored
git remote
- Check name as well as url of all remote repositories that git has stored for the shortname to be used when reading and writing to that remote
git remote -v
- If you want to fetch all the information that Paul has but that you don’t yet have in your repository, you can run
git fetch <remote-name>
Example:
git fetch origin
*Note: It’s important to note that the git fetch command only downloads the data to your local repository – it doesn’t automatically merge it with any of your work or modify what you’re currently working on. You have to merge it manually into your work when you’re ready.
- If you want to fetch only one benrach of a remote repo
git fetch <remote-name> <brahnch-name>
Example:
git fetch origin git-course
- Fetch all brnaches from origin
git fetch --all
*Note: Fetch will not update local branches. If you want to update local branches, you still need to pull every branch. 'Fetch' will not crate local branch. You have to do this manually.
- Command to fetch and then automatically merge that remote branch into your current branch
git pull <remote-name> <brahnch-name>
*Note: Running git pull generally fetches data from the server you originally cloned from and automatically tries to merge it into the code you’re currently working on.
- When you want to push your project to upstream
git push <remote-name> <branch-name>
- You may just want to undo everything you did since last point you committed
git checkout .
-
You may just want to undo the changes in only one file that you did since last point you committed
git checkout <FILE_NAME>
##Git Branching
-
Creating a New Branch
git branch testing
*Note: Note that This creates a new pointer to the same commit you’re currently on. The git branch <BRANCH_NAME>
command only creates a new branch – it does not switch to that branch. So if you are on master branch and now if you run this command then new branch will be created. But you will be still on master branch. You can check that using command git branch
And you will see that asterisk is still pointing to master branch. Now if you want to switch to this brnach then you have to run command git checkout <BRANCH_NAME>
. This command will change the pointer to thus new branch <BRANCH_NAME> and you will be on this branch.
Now in case if you want to avoid these two steps, you can do this in just one step by using -b
flag as ````git checkout -b <BRANCH_NAME>```. This command will create new branch <BRANCH_NAME> and it will also switch the pointer to this new branch.
- View list of all brnaches and check where HEAD is pointing
git branch
*Note: This will list all the brnaches and branch name with asterisk symbol indicates that currently HEAD is pointing to that branch. That means cureently you are working on that branch.
git checkout <BRANCH_NAME>
- Creating a New Branch and switching to that branch in just one step
git checkout -b <BRANCH_NAME>
*Note: Note the -b flag