This is a set of recipes to use Git, partly based on a very good tutorial in 'Real Python' called Introduction to Git and GitHub for Python Developers
- Initialize a new local repository (repo) from scratch: Create a directory, get into it, and use
git init
mkdir my_new_repo
cd my_new_repo
git init
- See current status. Very useful instruction, and used very often
git status
- Create new file in repo. Now file exists, but it is untracked by Git
echo "print('Hello World')" > hello.py
git status
- Add new file to Git tracking. Now it is tracked but it is not yet part of the repo. It is in the staging area
git add hello.py
git status
- Commit a change, adding a comment. Now
hello.pybelongs to repo
git commit -m "Creating hello.py"
git status
- Create another file and add it to repo. Original
hello.pywas also modified, so it must be again added and commited
vim myname.py
vim hello.py
git add hello.py myname.py
git status
git commit -m "Added myname module. Minor modification to hello.py"
- The
.gitignorefile is special, because it contains the names of the files which should be ignored by Git, like for instance, python.pycfiles. It is Ok to use wildcards inside it
vim .gitignore
git add .gitignore
git commit -m "Created .gitignore"
- See a log of the changes. This can be verbose when you have commited many changes
git log
- You made a change in
.gitignore, but then changed your mind and dropped it. This instruction changes file back to where it was at last commit
vim .gitignore
git checkout -- .gitignore
- You decided to apply and commit other change
vim .gitignore
git status
git add .gitignore
git commit -m "Modifying .gitignore to exclude all .pyc files"
- Take a look at what is different from our last commit. In this case we want the diff of our most recent commit, and we can refer to it using HEAD
git diff HEAD
- We can unstage files by using the
git resetcommand
git reset octofamily/octodog.txt
- To see all branches
git branch
- To create a new branch AND MOVE IN THERE
git checkout -b my_new_feature
- To see all branches again (current branch is marked with '*')
git branch
- Back to the top of the main branch (master)
git checkout master
- Change back to the new branch
git checkout my_new_feature
- We are inside the NEW
my_new_featurebranch, so further changes will go in there
git add hello.py
git commit -m "Added code for feature x"
- Get back to the top of the
masterbranch
git checkout master
- Compare the state of two branches
git show-branch my_new_feature master
git show-branch my_new_feature
git show-branch master
HEADis where the repository is currently pointing to (in this case, the last commit done inmy_new_feature)
git show-branch HEAD
- This is like regular 'show-branch' but using SHA codes instead of names
git show-branch --sha1-name my_new_feature master
- Go back to
masterand MERGE changes FROMmy_new_featureTOmaster
git checkout master
git merge my_new_feature
- Removing a branch which is no longer needed
git branch -d my_new_feature
- This is for checking out (use) the repository at an specific point in time (98011e69...). The long number is the (unique) SHA code
git checkout 98011e69fda0df3937e99e2d7ac11ca3a1e37959
- Rename a file
git mv <old-file> <new-file>
git commit -m "Renaming file"
- Removing files (wildcards are also valid)
git rm <target-file>
- Removing directories
git rm -r folder_of_cats
- Set user email and name for every repository in your computer
git config --global user.email "user@example.com"
git config --global user.name "User Surname"
- Set user email and name for a single repository
git config user.email "user@example.com"
git config user.name "User Surname"
- Download a full repository
git clone https://github.com/jima80525/github-playground.git
- Download (pull) to the current computer the changes in an already present repository
git pull https://github.com/architest/git-example
- Upload (push) the local changes to GitHub:
git push https://github.com/architest/git-example master
- 'origin' is an alias for the remote server repository (if we started by cloning a repo), so the former instruction can be abbreviated as:
git push origin master
- The same, but the
-uoption tell Git to remember the parameters, so afterwards we only need to writegit push(some conditions may apply)
git push -u origin master
- Pulling, but in a more compact way
git pull origin master
- Add a remote repository to push our local repo to the GitHub server (i.e.: We created out repository from scratch and we are setting
originin order to be able to push it to a remote server)
git remote add origin https://github.com/try-git/try_git.git
-
First, fork the repository using GitHub facilities for that
- Original repository is: https://github.com/jima80525/github-playground.git
- The forked repository is: https://github.com/architest/github-playground.git
-
Then, clone the repository to the local machine
git clone https://github.com/architest/github-playground.git
- Get into the cloned repository. It is good to check the currently configured remote repository
git remote -v
The result should be something like:
origin https://github.com/architest/github-playground.git (fetch)
origin https://github.com/architest/github-playground.git (push)
- Add the path to the original repository
git remote add upstream https://github.com/jima80525/github-playground.git
- Verify that it worked:
git remote -v
origin https://github.com/architest/github-playground.git (fetch)
origin https://github.com/architest/github-playground.git (push)
upstream https://github.com/jima80525/github-playground.git (fetch)
upstream https://github.com/jima80525/github-playground.git (push)
- Now, in order to fetch changes from the
upstreamrepository you do:
git fetch upstream
- Possible changes are in
upstream/master. Make sure you're inmaster:
git checkout master
- Merge changes from
upstream/masterinto localmasterbranch
git merge upstream/master
- Updated!!!
git status– Make sure your current area is clean.git pull– Get the latest version from the remote. This saves merging issues later.- Edit your files and make your changes. Remember to run your linter and do unit tests!
git status– Find all files that are changed. Make sure to watch untracked files too!git add [files]– Add the changed files to the staging area.git commit -m "message"– Make your new commit.git push origin [branch-name]– Push your changes up to the remote.