Push code to github

This dish is learning the course content of djangogirls and summarizes some content about push code

Execute in order from top to bottom.

 

Pull code from GitHub: git clone "github code address"

GitHub code address is similar to

ssh: git@github:username/project.git 

http:https://github.com/username/project.git

 

 

Just finished web page code:

Git is a "version control system" used by a large number of programmers. This software keeps track of file changes at any time, so you can recall a specific version at any time later. Kind of like Microsoft Word's "track changes" feature, but more powerful.

 

In linux if git is not already installed, you can download git from your package manager, try the following command:

sudo apt-get install git

# or

sudo yum install git

# or

sudo zypper install git

 

Create your own Git repository

Git tracks changes to a specific set of files in a code repository (or "repo" for short). Let's start managing our own projects with git.

Open your terminal, go to the project root folder and run the following command:

Note that before initializing the repository, use the pwd command (OSX/Linux) or the cd (Windows) command to check your current working directory. You should run the command in the project root directory

$ git init

Initialized empty Git repository in ~/Project root/.git/

$ git config --global user.name "Your Name"

$ git config --global user.email [email protected]

Each project only needs to initialize the Git repository once, so there is no need to re-enter the username and email.

Git will track changes to all files and folders in this directory, but there are some files that we want Git to ignore. To do this, we can create a file named .gitignore.txt in the root of the project. Open an editor, create a new file and write the following:

*.pyc

__pycache__

myvenv

db.sqlite3

.DS_Store

Then save it as a .gitignore file in the project root directory.

Note: The "." at the beginning of the filename is important.

Before performing a git operation, it is a good idea to use the git status command to check the current status, especially when performing git add or if you are not sure which files have been changed. This helps prevent all kinds of surprises, such as the wrong file being added or committed. The git status command will return all untracked/modified/staged files, as well as branch status and other information. The output will be like this:

$ git status

On branch master

 

Initial commit

 

Untracked files:

  (use "git add <file>..." to include in what will be committed)

 

        .gitignore

        blog/

        manage.py

        mysite/

 

nothing added to commit but untracked files present (use "git add" to track)

Finally save the changes, go to the console and run these commands:

$ git add --all .

$ git commit -m "My Django Girls app, first commit"

[...]

13 files changed, 200 insertions(+)

create mode 100644 .gitignore

[...]

create mode 100644 mysite/wsgi.py

 

 

 

 

Push our code to Github

Now, create a new repository and name it "my-first-blog". Leave the "initialise with a README" checkbox unchecked, the .gitignore option to none (already created manually), and leave the License set to none.

 

On the next screen, you will see your repository clone URL. Select the "HTTPS" version, copy the address and paste it into the terminal

 

Now we need to connect the Git repository on the computer with the Github one.

Enter the following in the console (replace <your-github-username> with your github username)

$ git remote add origin [Just change this URL for different codebases https://github.com/<your-github-username>/my-first-blog.git ]

$ git push -u origin master

Enter your Github account name and password:

Username for 'https://github.com': 

Password for 'https://[email protected]':

Counting objects: 6, done.

Writing objects: 100% (6/6), 200 bytes | 0 bytes/s, done.

Total 3 (delta 0), reused 0 (delta 0)

To https://github.com/hjwp/my-first-blog.git

 * [new branch]      master -> master

Branch master set up to track remote branch master from origin.

 

 

 

Changed code:

Commit and push code to Github

First confirm what files have changed since the last deployment:

$ git status

 

Make sure to be in the project root directory and tell git to include all changes in this directory:

$ git add --all .

Note: --all means that git will also recognize if you have deleted the file. Also . means the current directory.

 

Check what git will upload before uploading all files (all files git will upload should now be displayed in green):

$ git status

 

Attach a "commit message" to it describing what changes were made. You can type anything you want at this stage, but it's more useful to write something descriptive that will remind you of what you did in the future.

$ git commit -m "Changed the HTML for the site."

Note: Make sure to use double quotes to commit messages.

 

After doing this, upload the changes to Github:

git push

 

When we need to delete 暂存区or 分支upload the file, and the workspace does not need this file, we can use

git rm file_name_or_path

 

When we need to delete 暂存区or 分支upload a file, but we need to use it locally, but we just don't want this file to be version controlled, we can use

git rm --cache file_name_or_path

 

Suppose after a while. We invite some people to join our project on github, and they pull your code and make changes and commits.

To check and pull changes in the remote Git repository, run:

git pull origin master

 

The documentation on octocat seems to have made some changes. To see what has changed since the last commit, use the git diff command.

In the general case we want to get the diff from the most recent commit, reference uses the HEAD pointer.

git diff HEAD

 

Another important use of diff is to see changes to files in the staging area. It tells git which files need to be added. Use git add to store the file octofamily/octodog.txt to the staging area.

git add octofamily/octodog.txt

 

Run git diff --staged to see the changes in the staging area. You can see that octodog.txt has been created.

git diff --staged

 

octodog is part of octofamily file, octocat file is better than octodog file, we will delete octodog.txt.

Use the git reset command to remove the changes, i.e. delete the file octofamily/octodog.txt.

git reset octofamily/octodog.txt

 

octodog is part of octofamily file, octocat file is better than octodog file, we will delete octodog.txt.

Use the git reset command to remove the changes, i.e. delete the file octofamily/octodog.txt.

git reset octofamily/octodog.txt

 

Use git reset to remove octodog.txt from the staging area, but you can see it is still in the same folder locally. You just delete it from the staging area. It would be nice if we could go back to where it was before octodog was created.

The file can be restored to the state at the time of the most recent commit by using the following command: git checkout -- <target>. Remove all changes from the last commit in the file octocat.txt

git checkout -- octocat.txt

 

When developers are working on a feature or fixing a bug, they usually create a copy of the code (aka. branch) that can be committed separately. Then, when features are complete or bugs are fixed, they can merge that branch back into the master branch.

We're going to delete all those pesky octocat files, create a branch called clean_up, and continue all the work there:

git branch clean_up

 

Type git branch and you will see two local branches: a master branch named master and a new branch named clean_up.

You can switch branches with git checkout <branch>. To switch to branch clean_up use:

git checkout clean_up

 

Currently in the clean_up branch. You can use the git rm command to delete all octocats files, it will delete the local files and delete the files in the staging area.

You'll want to use wildcards again to get all octocats, clear them, and keep running:

git rm '*.txt'

 

Now you have removed all the cats you need to commit the changes to.

Run git status to check the changes you are about to commit.

git commit -m "Remove all the cats"

 

Nice, you've pretty much done cating... and fixing bugs, switching to master branch, copying (or merging) changes on clean_up branch to master branch.

Switch to master branch:

git checkout master

 

Merge the changes from the clean_up branch into the master branch. It's very simple.

Now that you are on the master branch, merge the clean_up branch into the master branch:

git merge clean_up

 

congratulations! The first time you successfully resolve the conflict and merge the code, the next step is to delete the clean_up branch.

Use git branch -d <branch name> to delete branches. Immediately delete the clean_up branch:

git branch -d clean_up

 

This is the last step. Now all that's left is to push everything to the remote repository.

git push

 

Collect static files.

Have you ever wondered, what is "whitenoise"? It is a tool for serving so-called "static files" static files. Static files are those files that rarely change or are not executable program code, such as HTML or CSS files. On our computers, they work in different ways, and we need tools like "whitenoise" to serve them.

For now we just need to run one additional command on the server, collectstatic. It tells Django to collect all required static files on the server. For now, it's mostly files that make the admin interface look nicer.

(myvenv) $ python manage.py collectstatic

 

You have requested to collect static files at the destination

location as specified in your settings:

 

/home/edith/my-first-blog/static

 

This will overwrite existing files!

Are you sure you want to do this?

 

Type 'yes' to continue, or 'no' to cancel: yes

Type "yes" and it will work on its own!

 

 

 

Error while pushing:

To https://git.coding.net/sunpeng95/pos--web.git

 ! [rejected]        master -> master (fetch first)

error: Could not push some refs to 'https://git.coding.net/sunpeng95/pos--web.git'

Hint: The update was rejected because the remote repository contains commits that do not yet exist locally. This is usually because another

Hint: A repository has already pushed to this reference. You may need to integrate remote changes before pushing again

Hint: (like 'git pull...').

Tip: See the 'Note about fast-forwards' section in 'git push --help' for details.

Solution:

Add the content that needs to be ignored in the .gitignore file, such as: .idea

git push -u origin +master

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326342009&siteId=291194637