Git Essentials - rawatrs/rawatrs.github.io GitHub Wiki

Setting up Git

  • Create a github account.
  • Create a git repository.
  • Get that repo into your local system using command like:

git clone https://github.com/rawatrs/rawatrs.github.io

then you need to use ssl authentication, create ssh keys and add it to your profile page - >account settings ->ssh keys by following instructions given here: https://gitlab.itrsgroup.com/help/ssh

  • Then, try

git clone [email protected]:rawatrs/rawatrs.github.io

and hopefully, things will work.

  • Now, you need to start working...

cd rawatrs.github.io

echo "Hello World" > index.html

git add --all

git commit -m "Initial commit"

git push

git fetch

git checkout -b origin/master --track

git push origin/

  • Now, Add / edit files

git add #git add stages the files you wish to commit. You add files that have been changed as well as new files.

git rm

git commit

Git Notes

  • origin — that is the default name Git gives to the server you cloned from.

  • Git files have 3 stages: "modified", "staged", "committed".

  • git add command is a multipurpose command — you use it to begin tracking new files, to stage files, and to do other things like marking merge-conflicted files as resolved.

  • If you modify a file after you run git add, you have to run git add again to stage the latest version of the file. Otherwise, if you commit now, the version of file as it was when you last ran the git add command is how it will go into the commit, not the version of the file as it looks in your working directory when you run git commit.

  • "git diff" shows what you have changed but not yet staged.

  • "git diff --cached" shows what you’ve staged that will go into your next commit.

  • "git commit -a": Providing the -a option to the git commit command makes Git automatically stage every file that is already tracked before doing the commit, letting you skip the git add part. To rename a file in git, use : "git mv <new_file_name>"; e.g "git mv README README.txt" This is equivalent to doing,

mv README README.txt

git rm README

git add README.txt

  • git log lists the commits made in that repository in reverse chronological order. That is, the most recent commits show up first.

  • "git commit --amend" amends any mistakes made during last commit (e.g. forgot to add a file, incorrect commit msg, etc). This command takes your staging area and uses it for the commit. If you have made no changes since your last commit, then your snapshot will look exactly the same and all you’ll change is your commit message. E.g. $ git commit -m 'initial commit' $ git add forgotten_file $ git commit --amend

After these three commands, you end up with a single commit — the second commit replaces the results of the first.

  • git push [remote-name] [branch-name]

  • To inspect a remote repository, use "git show remote <remote_repo>"

  • To show log from another branch, use "git log --branches=< branch_name >

  • Tracking branches are local branches that have a direct relationship to a remote branch. E.g.

git checkout -b my_branch remote_server/my_branch

or simply

git checkout --track remote_server/my_branch

To give the local branch a different name to the remote branch, use a different name for local_branch as:

git checkout -b my_personal_branch remote_server/my_branch

Commands Syntax

  • git checkout -b local_branch remote_server/remote_branch (Usually local_branch & remote_branch have same name)

  • git push [remote_server] [local_branch]:[remote_branch]

  • git remote add remote_server_alias remote_server_link

  • git push [remote_server] :[remote_branch] //this deletes remote_branch on remote_server

Common Questions

Q: How to switch branch in git?

A: > git checkout branch

Q1: What does "git push origin master" mean?

A: origin is the remote (aka GitHub) "git push origin master" is the same as git push origin <local_master>:<remote_master_on_github>

Q2: I want to do is only type git push and git pull when: I'm on a master branch I want to push and pull from a my_test branch on github, only typing git push and git pull.

A: Git has concept of "local" & "remote" branches. We need to tell git to link the local & remote branches so that when you do git pull, git push, it knows where you want to get/put data. For this, issue command:

git push -u origin <local_branch>:<remote_branch>

This will link the remote & local branch. It will push the commits from your local branch to a (possibly new) remote branch and set up local branch to track origin/<remote_branch>.

Another way to do this is:

git checkout master

git branch --track my_test origin/my_test #This will create a my_test branch and map it to origin/my_test remote branch

git checkout my_test

if you've already created the branch then you can use the -u switch to tell git's push and pull you'd like to use the specified local and remote branches from now on, like so:

git pull local_branch remote_server/remote_branch

git pull -u my_test origin/my_test

git push -u my_test origin/my_test

Q3: What is the difference between git pull and fetch ?

A: There are two ways to get commits from a remote repository or branch: git fetch and git pull. "git pull" gets changes from remote branch and merges them into your local branch (without intimating the user). "git fetch" only gets changes from remote branch, but does not merge into your local branch. You need to then type "git merge <remote_branch>/<local_branch>" to actually merge the changes.

E:g

git pull upstream master # Pulls commits from 'remote_branch' and stores them in the local repository

git fetch upstream # Fetches any new commits from the original repository

git merge upstream/master # Merges any fetched commits into your working files

Q4: Diff between "git checkout mybranch" and "git checkout -b mybranch" ?

A: "git checkout mybranch" checks out an existing branch already created using "git branch mybranch" , it also does not switch to mybranch. "git checkout -b mybranch" creates a new branch called "mybranch" and makes it the active branch.

Q5: How to unstage a file (accidently staged)?

A:

git reset HEAD < file_name >

Q6: How to unmodify a Modified File / to revert back to last committed stage.

A:

git checkout -- < file_name >

Q7: What does "git rm --cached readme.txt" do?

A: TBD

⚠️ **GitHub.com Fallback** ⚠️