GIT/ GITHUB Cheatsheet with startup commands.
From some time now, i have started using GIT and GITHUB now since in my organization we are working on GITHUB.
Hence i preferred to create a cheatsheet kind of table that contains all these GIT commands that i am using and will use for reference.
Hence here comes the list of commands with an example.
Get online repository to our system
$ git clone https://github.com/ngelinux/hello-world.git
Cloning into ‘hello-world’…
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 7 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (7/7), done.
$The command git clone will clone or copy the online repository files/data to your local system.You will see hello-world directory in your CWD(current working directory)
GIT Command | Short Description |
---|---|
$ touch testfile $ git status On branch master Your branch is up-to-date with ‘origin/master’. Untracked files: (use “git add …” to include in what will be committed) testfile nothing added to commit but untracked files present (use “git add” to track) $ |
git status will show the current status of repository of your local disk. |
$ git add testfile $ |
git add will add the file in the current repository. |
$ git add -A $ git add . |
git add command options . and “-A”, both will sync all files from the current directory contents to the online repository. |
$ git status On branch master Your branch is up-to-date with ‘origin/master’. Changes to be committed: (use “git reset HEAD …” to unstage) new file: testfile |
git status to see the status of file after git add command. |
$git commit | To commit the changes to the local repo |
$ git push Username for ‘https://github.com’: ngelinux Password for ‘https://ngelinux@github.com’: Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 296 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/ngelinux/hello-world.git 9ef40f0..4685233 master -> master $ |
Send the contents to the online portal |
Initializing the local Data to an Online Repository
1. Initialize the repository.
$ git init Initialized empty Git repository in /Users/saket1447583/github/newproject1/.git/
2. Check the git repo status
We can see file “abc” as untracked file.
$ git status On branch master Initial commit Untracked files: (use "git add ..." to include in what will be committed) abc
3. Now add all files from the current directory to the repository.
user@ngelinux$ git add . user@ngelinux$ user@ngelinux$ git status On branch master Initial commit Changes to be committed: (use "git rm --cached ..." to unstage) new file: .function.py.swp new file: bash.sh new file: eigth.py new file: example.py new file: file1 new file: function.py new file: loop.py new file: seventh.py new file: sixth.py
4. Commit the changes to local repo.
user@ngelinux$ git commit -m "First commit" [master (root-commit) 52841b4] First commit 9 files changed, 142 insertions(+) create mode 100644 .function.py.swp create mode 100755 bash.sh create mode 100755 eigth.py create mode 100755 example.py create mode 100644 file1 create mode 100755 function.py create mode 100755 loop.py create mode 100755 seventh.py create mode 100755 sixth.py user@ngelinux$ git status On branch master nothing to commit, working tree clean user@ngelinux$
5.Set the remote origin GIT repo.
We need to create repo online and copy its url and mention that below as origin.
user@ngelinux$ git remote add origin https://github.com/ngelinux/python.git user@ngelinux$
6. Check remote status
user@ngelinux$ git remote -v origin https://github.com/ngelinux/python.git (fetch) origin https://github.com/ngelinux/python.git (push) user@ngelinux$
7. Finally push the changes to master branch of our repository.
user@ngelinux$ git push -u origin master To https://github.com/ngelinux/python.git ! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://github.com/ngelinux/python.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
If the above doesn’t work, we can use force command.
user@ngelinux$ git push -u origin master --force Counting objects: 11, done. Delta compression using up to 4 threads. Compressing objects: 100% (10/10), done. Writing objects: 100% (11/11), 1.83 KiB | 0 bytes/s, done. Total 11 (delta 0), reused 0 (delta 0) To https://github.com/ngelinux/python.git + 541e6de...52841b4 master -> master (forced update) Branch master set up to track remote branch master from origin. user@ngelinux$
II. Create Branch and merging
### Create branch $ git branch “new_branch_name” ### Checkout i.e. go into the branch $ git checkout “new_branch_name” ### Merge new branch in master branch $ git merge “new_branch_name” ### Delete the new branch from local machine $ git branch -d “new_branch_name” ### Delete the new branch from github i.e. remote location. $ git push origin —delete “new_branch_name” ### Note :- we can enable services like email, etc by going on path "Github - Repository - Settings - integration & services - Email".
III. Add color and autocompletion in our bash shell for GIT commands.
### Check current color scheme $ git config color.ui false ### Enable color scheme. $ git config --global color.ui true If above command does not work, download below file: https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash And load it in your .bash_profile file.
I will keep this page updated regularly as soon as i learn about GIT more to have a complete cheatsheet on a single page.
Post your useful comments below so that we can learn more about GIT.