SPACE
for next slideShift+SPACE
for previous slideBasic terminal commands
Git is the most popular version control system started by Linus Torvalds as a replacement for BitKeeper.
Git commands only work when you are currently inside a directory
having the .git/
subdirectory.
git
commands work
If a .git/
dir is at:
~/projects/favproject/.git/
then you can run git
commands from:
git
commands don’t work
If a .git/
dir is at:
~/projects/favproject/.git/
then you can not run git
commands from:
Open terminal and create a new Git repository
git init ~/newproject # make .git/ cd ~/newproject # Now we can run git commands
One time setup:
git config --global user.name 'Ratan Tata' git config --global user.email 'ratantata@tata.com'
On a per repository basis one can do:
git config user.name 'Secret Org' git config user.email 'secretname@secret.org'
$EDITOR
EDITOR=emacs
You can use the following in your ~/.profile
or ~/.bashrc
export EDITOR=emacs
Hello, World!
in Python
hello.py
:
print("Hello, World!")
Running the script:
python3 hello.py
A state comprises of:
git status
Check if files were changed since the last commit. It shows:
To ignore some files we need to add a file called .gitignore
with the following
contents:
*.pyc __pycache__/
git add hello.py # add to staging area
git reset HEAD -- hello.c
Add the state to the history:
git commit # Opens your $EDITOR
Then type in an appropriate message. Save and exit.
Keep your changes short and commit messages descriptive.
It is hard to achieve but start from now and hopefully you will start to embrace it and make others happy.
Commit often
This will help you achieve the above.
git branch -a
git branch test # branch from current commit
git checkout test
Now the commits will be added to test
branch.
HEAD
def greetings(name): print(f"Hello, {name}!") if __name__ == '__main__': import sys greetings(sys.argv[1])
Running the program:
python3 hello.py "Ratan Tata"
Hello, Ratan Tata!
git log --all --graph \ --decorate \ --pretty=oneline \ --abbrev-commit
It is recommended that you alias this long command in your ~/.bashrc
:
alias gitlog='git log --all --graph \ --decorate \ --pretty=oneline \ --abbrev-commit'
You can also run the above command in your current bash session for it to take effect.
Lets say our feature is complete and we want to merge it into master
.
Here master
is our destination branch and test
is our source branch.
So, first we switch to destination branch using git checkout master
.
Then we use git merge test
to merge the source branch.
git stash
git checkout
git remote
git fetch
git pull
git commit --amend
You can start learning more using the following ways: